Shiny is an R package that makes it easy to build interactive web apps straight from R. You can host standalone apps on a webpage or embed them in R Markdown documents or build dashboards. You can also extend your Shiny apps with CSS themes, htmlwidgets, and JavaScript actions.
Some examples of well-polished Shiny apps are available at https://shiny.rstudio.com/gallery/.
Today you will get familiar with the work flow involved in developing and publishing a Shiny app, modifying the user interface, modifying the server function, and displaying reactive output.
To get started, follow the steps below.
Run install.packages("shiny")
in your R Console to install package shiny
.
Go to File > New File > Shiny Web App
Enter your application’s name
Keep option Single File (app.R) selected
Enter the directory of where the application should be saved
File app.R should open, click Run App to see the result
You should not change the file name app.R
. To stay organized, keep each shiny app in its own directory.
The template app uses a data set faithful
that is available in base R. Data set faithful
contains waiting times between eruptions and the duration of the eruption for the Old Faithful geyser in Yellowstone National Park, Wyoming, USA. Variables eruptions
and waiting
are both in minutes. A preview of the data set is given below. Type faithful
in your Console to see the full set of data.
In the following tasks you will tidy this template app that uses faithful
and add inputs and outputs. To remind yourself of the various input, output, and render functions, reference https://www.rstudio.com/resources/cheatsheets/#shiny.
After library(shiny)
, add library(tidyverse)
. Modify the code inside function renderPlot()
so the histogram that is created is with ggplot()
and geom_histogram()
. Include labelling to match the image below.
Plot details:
fill = "red"
color = "black"
alpha = .3
bins
controls the number of bins in geom_histogram()
After you make the changes, run your app. It should look like the image below.
Inside function sidebarPanel()
and after sliderInput()
add a select box to your user interface. Include colors “Red”, “Blue”, “Green”, “Grey”, with “Red” as the default color. Look at the help for function selectInput()
. Include labelling to match the image below.
Don’t forget a comma between your inputs.
After you make the changes, run your app. It should look like the image below.
Update function server()
so the histogram’s fill will change when a new color is selected.
After you make the changes, run your app. It should look like the image below.
Update the user interface to include a check box that reads “Preview data”. Place this below the select box. Set the default for the check box to be unchecked. Type ?checkboxInput
in your Console for reference.
After you make the changes, run your app. It should look like the image below.
Update the user interface to include space for a data table to be output. Use function dataTableOutput()
inside function mainPanel()
. You will place a data table below the plot. Separate functions plotOutput()
and dataTableOutput()
with a comma.
After you make the changes, run your app. You should see no changes in the user interface. All you have done is allocate space in your user interface for a data table to be displayed.
Update function server()
so 6 random rows of faithful
will be displayed below the plot when option “Preview data” is checked. Use function renderDataTable()
.
After you make the changes, run your app. It should look like the image below.
Include a text input in the user interface that will allow the user to create their own title for the plot. Update object ui
and function server()
to make this possible.
After you make the changes, run your app. It should look like the image below. To see the full app in action, visit https://shawn-santo.shinyapps.io/ica-04-09-19/.