If you’re in the business of business analytics, you know what Tableau is. It’s one of the top drag-and-drop BI tools for analyzing and visualizing data. Pretty much anyone can use it to make decent dashboards. But here’s the problem – it’s quite limiting for developers with a background in R and R Shiny.
After all, Tableau was intended for business users, not hardcore developers. If you still like using Tableau, or your client insists on it – fear not. There’s something Shiny over the horizon!
RStudio recently announced
shinytableau – an R package that lets you create Tableau dashboard extensions using R and R Shiny. We’ve taken it for a test ride, and boy is it good. Today’s article will show you how to get started and how to create your first extension in minutes. We assume you have Tableau installed. If not, please download a 14-day trial version.
Looking for an in-depth comparison between Tableau and R Shiny? This article will show you which Excel alternative is right for you.
Table of contents:
- Getting Started With shinytableau – Project Setup
- How To Create a shinytableau Extension with R and R Shiny
- How To Use a shinytableau Extension in Tableau
Getting Started With shinytableau – Project Setup
First things first – you’ll need a dataset. The MTCars dataset is built into R, but not into Tableau. Download it from the given URL and store it someplace safe.
Next, you’ll have to create a new RStudio Project. Simply navigate to File – New Project…, and a window like the one below should appear:
Go with the New Directory option and save the project anywhere on your machine. We’ve named ours
MTCarsShinyTableau, but you’re free to be more creative.
Once you click on Create Project, you’ll see the R console open up. We’ll install the libraries and do the initial setup here. The
shinytableau package isn’t available on CRAN just yet, so you’ll have to install it from GitHub. Doing so requires a
remotes package. Use the following snippet to install both:
Let it do its thing, and once done, proceed to the final setup step – creating a manifest file. It’s required for all R Shiny Tableau extensions and will contain details about your extension, such as the version number, name, description, author, and so on.
To start, run the following line from the R console:
A window like this should pop up:
You can leave everything as-is, but it’s recommended to change the defaults so your extension is somewhat unique. We’ve changed the extension ID, name, description, author, organization, and website:
And that’s it – click on Save and you’re good to go! You’ll create your first extension in the following section.
How To Create a shinytableau Extension With R and R Shiny
shinytableau is just a slightly modified R Shiny application. It still has the same UI and Server functionality but also adds support for configuration UI and configuration Server, which are specific to
shinytableau. We’ll explore them one by one. To start, create a new R file for the extension – we’ve named ours
The first things you should do in any R Shiny
shinytableau extension is declare the file type, import packages, and connect to the previously created manifest file.
The file type is just a hint to RStudio that we’re working with an R Shiny app, even though it won’t follow the structure of a Shiny application.
Next, let’s discuss the UI. We’ll have a single UI element displaying a ggplot-based scatter plot between two variables from the MTCars dataset. The
brush parameter is here to enable creating and moving a selection box on your chart.
Onto the Server now. You’ll need to wrap the data inside the
reactive_tableau_data() function. We’ve gone with the ID of
data, but you can use any, just make sure to change the ID in other calls as well. The Server function renders a ggplot-based visualization based on the data and columns selected. You can access the columns via the
tableau_settings() function by passing in the parameter names. We’ll set these later in the app, but for simplicity, we’ve gone with
colY. Finally, you can render the plot using the reactive dataset and a promise.
Let’s discuss the Config UI now. It’s specific to
shinytableau, and contains a list of elements displayed in Tableau while configuring the extension. We’ll show a UI element for choosing the datasheet, a variable selection element that will contain our two variables for the chart and a table output that lets you preview the columns you’ve selected.
Almost there! What comes next is the Config Server. It handles the logic of the previously discussed UI. First things first, we’ll use the input validator
iv to specify our
colY attributes that are required. We’ll then choose a data sheet and display a preview of the dataset used and the columns selected.
The magic happens inside the
varSelectionUI rendering. There you can specify the dropdown menus that will have the dataset’s column names as possible choices. We’ll have two dropdowns, as we need to choose values for both X and Y axes.
Finally, we’ll declare an inner function that updates the tableau settings with the selected dataset and columns.
It’s a lot to digest at once, so take your time. You could completely remove the input validator rules and the preview data if you aim for less code overall, but doing so would take a toll on the user experience.
Finally, let’s connect everything declared above into a single function. It’s called
tableau_extension() and it’s an alternative to
shinyApp() from your regular R Shiny applications. You’ll have to declare a couple of additional parameters, as there are more elements in the script:
We’ve tried to simplify every step needed to create a
shinytableau extension. If you got stuck in the process, you can simply copy/paste the entire
app.R from below:
And now comes the part you’ve been waiting for. Simply click on the Run App button, and a window like the one below should pop up:
From here, click on the Download button and save the
.trex file somewhere you’ll remember.
And that’s it – the extension was successfully created and saved. Let’s see how to use it in Tableau next.
How To Use a shinytableau Extension in Tableau
With all of the coding out of the way, you can fire up Tableau and connect to the MTCars dataset (Connect menu – To a File – Text File).
Go to Sheet 1 and create a dummy data visualization as you normally would. The one below displays a scatter plot between the horsepower on the X-axis, and miles per gallon on the Y-axis.
Create a new dashboard by clicking on the second icon to the right of Sheet 1 in the bottom tab menu, and drag Sheet 1 to the dashboard view.
Let’s now use our
shinytableau extension to create the same visualization. First, drag the Extension object from the Objects panel just below the visualization. It should automatically create a 50:50 vertical split for you.
A modal window should immediately pop up. It lets you choose from one of the built-in extensions, but it also allows you to use your own. You’ll see the Access Local Extensions option in the bottom left corner. Click on it.
From here, navigate to the
.trex file created earlier and open it.
You’ll see an additional modal window asking if you want to allow the third-party extension to extend the capabilities of Tableau. Click on OK.
The extension will appear in its placeholder now. You can see the title and the version printed on the top, and a warning message just below it telling you the extension isn’t configured yet.
To configure the extension, click on the down arrow button in the top right corner of the placeholder and choose Configure.
This is where the Config UI and Config Server show their purpose. You’re now presented with a configuration window showing you the worksheet selection option, dropdown menus for X and Y axes, and the dataset preview below them. You’ve specified these in the Config UI, and coded the logic in the Config Server.
Select Sheet 1 as the worksheet and Hp/Mpg as the axes values.
And that’s it – click on Apply and OK and you’re set.
You have to admit creating custom extensions is easier than it sounds. It took only 90 lines of code to implement a fully customizable scatter plot. Of course, Tableau can create scatter plots for you, but this was just a single example of what you can do with
shinytableau. The door to more customizable Tableau dashboards is opening with Shiny. You have the ability to do more while giving your clients the comfort and familiarity of Tableau.
You’ve learned a lot today, from the basic intuition behind custom
shinytableau extensions to creating and testing the first one from scratch. It’s a lot for a single article, sure, but we wanted to provide you with a central place for getting started, besides the documentation.
We recommend you work on the following homework challenges to reinforce your knowledge of
- Make an extension that plots a histogram of a single variable
- Tweak the visuals of the plot
- Allow user to tweak title and axis labels
For more FAQ, general advice, and “best practices” we encourage you to go to RStudio’s BI community board. Be sure to report any technical issues to the Github repo. For customer issues, we recommend you reach out to the RStudio or Tableau customer support.
Feel free to share your results with us on Twitter – @appsilon. We’d love to see what you’ve come up with.
Do you find R Shiny inspiring? Here’s everything you need to start a career as an R Shiny developer.