The workshop will run for 1 hour.
Combines slides, live coding examples, and exercises for you to participate in.
Ask questions throughout!
I hope you end up with more questions than answers after this workshop!
Course website: nrennie.rbind.io/training-intro-to-shiny
Shiny is an open source R package that provides a framework for building web applications using R.
Shiny helps you turn your analyses into interactive web applications without requiring HTML, CSS, or JavaScript knowledge.
Interacting with and exploring data
Showcasing how models work under a wide range of parameters
Developing apps as teaching aids
…
Assuming that you already have R installed, you will also need to install the {shiny} R package:
and load it into R:
We’ll also need some data so we’ll install the {palmerpenguins} package:
…which has data about penguins!
# A tibble: 6 × 8
species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g
<fct> <fct> <dbl> <dbl> <int> <int>
1 Adelie Torgersen 39.1 18.7 181 3750
2 Adelie Torgersen 39.5 17.4 186 3800
3 Adelie Torgersen 40.3 18 195 3250
4 Adelie Torgersen NA NA NA NA
5 Adelie Torgersen 36.7 19.3 193 3450
6 Adelie Torgersen 39.3 20.6 190 3650
# ℹ 2 more variables: sex <fct>, year <int>
Shiny apps come in two parts:
Defines the layout and appearance.
Contains elements such as:
Performs calculations.
Contains the logic to respond to user inputs, and update outputs.
Communicates with the UI to dynamically render outputs.
Single file app (called app.R
)
Multiple file app (including reusable modules)
Shiny app as an R package
Creating an app file
Adding UI and Server elements
Running a Shiny app
05:00
Open up your IDE of choice.
Make sure you have the {shiny} package installed.
Create an app.R
file.
Add ui
and server
elements, as well as shiny::shinyApp(ui, server)
.
Check it works.
What’s included in the UI?
Contains elements such as:
There are many layouts within Shiny and its extension packages. We’ve already seen:
Fluid pages follow a structure of rows, which contain columns.
A common approach is a sidebarPanel()
and a mainPanel()
.
Text Input (textInput()
): Allow users to input text data using a text box.
Numeric Input (numericInput()
): Enable users to input numerical values using a slider or numeric input box.
Checkbox Input (checkboxInput()
): Provide binary options for users to select/deselect.
Select Input (selectInput()
): Offer users a dropdown menu to select from multiple options.
File Input (fileInput()
): Enable users to upload files such as CSV, Excel, or text files.
Action Button (actionButton()
): Trigger specific actions or events when clicked by the user.
Plot Output (plotOutput()
): Display interactive plots generated from R code.
Table Output (tableOutput()
): Present data in tabular format, allowing users to explore and interact with the data.
Text Output (textOutput()
): Show text or character strings dynamically generated based on user inputs or reactive expressions.
UI Output (uiOutput()
): Dynamically generate and display UI elements based on user inputs or reactive expressions.
Download Output (downloadButton()
/ downloadHandler()
): Provide functionality to download data, plots, or files generated within the Shiny application.
Add a title
Use a sidebar layout
Add a dropdown menu with selectInput()
05:00
Inside the fluidPage()
function, add a title panel, sidebar, and main panel.
Add a drop down menu with selectInput()
to choose by Island.
What happens if you use checkboxGroupInput()
instead?
Bonus: add additional user options! See: mastering-shiny.org/basic-ui.html
After a user changes an input, we usually want to change something. It might be a plot, data, other UI elements, …
Let’s say that after a user select a specific species, we want to create (and update) a scatterplot of bill length against bill depth for that species.
We need to start by subsetting the data.
We use a reactive()
element which reacts to a user input:
We access the user input using input$inputId
.
We can use this reactive element in other server functions using filter_data()
. Note the brackets.
See also observe()
and observeEvent()
.
We can add outputs to the UI using e.g. plotOutput("outputId")
. But we need to make outputId
…
We need to assign the object to output$
.
The renderX
should match the xOutput
in the UI e.g. renderPlot
and plotOutput
.
There are other arguments to renderPlot()
e.g. height, so we wrap the R expression in {}
.
Add a reactive()
that filters data based on dropdown input
Create a plot using reactive data
Add the plot to the user interface
05:00
Add a reactive()
in the server to subset the data based on the user input you created.
Create a bar chart of number of penguins per species. plotOutput()
also works for {ggplot2} plots!
Check your app runs.
Bonus: the default Shiny app styling is fine. Install and load the {shinythemes} package. Try adding theme = shinytheme("cyborg")
to the UI.
Browse: rstudio.github.io/shinythemes
Getting the Shiny app off your laptop and out into the world!
We need a server to run the R code:
See posit-dev.github.io/r-shinylive.
The {shinylive} package converts a standard shiny app into a shinylive app:
Assuming your app.R
file is in a folder called app
:
You can then use the files in the site
folder to deploy it as a normal website.
*not all R packages are available for shinylive.
**initial load time is still quite slow.
Build tests using shinytest2.
Building apps as packages helps with dependency management.
Learn a little bit of HTML, CSS, and Javascript.
Modules are a good way to reuse bits of UI and server code across the application.
Start with pen and paper… (think about user experience!)