In this post, we’ll create a simple Shiny web app in Dataiku DSS. It’s a simple scatterplot on Haiku T-shirt sales data, related to the data used in the Tutorials.
You will need:
Create a new empty Shiny web app:
You will be redirected to the web app editor.
The Shiny web app editor is divided into two panes.
The left pane allows you to see and edit the R code underlying the web app, and contains two tabs:
The right pane gives you several views on the web app.
While the code is split across two files, and there is no explicit code needed to listen for changes to the widget values, the concepts behind Shiny and Bokeh are very similar.
Let’s build the code behind the R Shiny web app.
In the UI tab, insert the following code to define the layout for the web app.
shinyUI()function indicates we’re building the UI for the web app
fluidPage()function allows us to define a flexible layout for the page. It accepts several arguments that are the components of the page layout.
titlePanel() creates a title at the top of the web app
sidebarLayout() defines a layout where widgets are defined in a sidebar panel, and the visualization is displayed in a main panel. For now, we’ll leave
sidebarPanel() empty and just include the definition for the
mainPanel(), which will show
plotOutput() as defined by the
scatterPlot element of the output object. We’ll add the interactivity later.
Now, in the Server tab, insert the following code to define the output visualization.
dkuReadDatasetfunction to pull the dataset as an R dataframe, as you would in an R recipe or notebook.
shinyServer()function indicates we’re building the server logic for the web app.
renderPlot()function contains the code to generate the output visualization. It can also accept input from widgets in the UI code; we will add the interactivity in a moment. The results of
renderPlot()are saved to the
scatterPlotelement of the
outputobject, which is then displayed in the Preview.
Save your work, and the preview should show the current (non-interactive) scatterplot.
The current scatterplot includes all orders from 2013-2017, across all types of t-shirts sold. Now let’s add the ability to select a subset of years, and a specific category of t-shirt. To do this, we need to make changes to the UI and Server code.
In the UI tab, change
sidebarPanel() to the following.
sliderInput()function creates a slider widget labeled “Order year”, that has values ranging from 2013 to 2017, and an initial state where all years are selected. The widget selection is saved to the
yearelement of the
selectInput()function creates a dropdown selection widget labeled “T-shirt category”, that has values for each of the t-shirt categories plus “All”. The widget selection is saved to the
categoryelement of the
In the Server tab, change
renderPlot() as follows.
This takes the input dataframe
df and uses the widget selections to filter the dataframe to only use records with the correct order year and t-shirt category. It then defines the x and y axes of the scatterplot to be the age and order total from the filtered dataframe.
Save your work, refresh the preview, and it should now show the current interactive scatterplot.
When you are done with editing, you can easily publish your web app on a dashboard from the Actions dropdown at the top-right corner of the screen.
Using Dataiku DSS, you have created an interactive Shiny web app and published it to a dashboard.
You can examine a version of this sample web app on the Dataiku gallery.
For further inspiration on what is possible in Shiny web apps, see the Shiny gallery (external).