A LOT of the content of this lecture (and labs to follow) are based on two open-source e-books:
Learn the concepts of
Shiny is a framework for creating web applications using R code. It is designed primarily with data scientists in mind, and to that end, you can create pretty complicated Shiny apps with no knowledge of HTML, CSS, or JavaScript.
shinyApp function
The user interface (ui) object controls the layout and appearance of your app.
Cards are a common organizing unit for modern user interfaces. You can use the function card()
to create a card in your Shiny app.
You can also use functions like card_header()
, card_footer()
, and card_image()
to add card elements to a card. For example, card_header() adds a header.
The server function contains the instructions that your computer needs to build your app.
This is where we add reactive outputs, and will contain parts of R code we are familiar with, like ggplots.
Widgets are functions that allow users to interact with the dashboard. Widgets are specific in the user interface level.
Reactivity happens using the inputs selected on the widget to create a certain output. Reactivity is specified at the user interface and server levels.
Shiny comes with a family of pre-built widgets, each created with a transparently named R function.
For example, Shiny provides a function named actionButton
that creates an Action Button and a function named sliderInput
that creates a slider bar.
Each widget function requires several arguments. The first two arguments for each widget are
a name for the widget: The user will not see this name, but you can use it to access the widget’s value. The name should be a character string.
a label: This label will appear with the widget in your app. It should be a character string, but it can be an empty string ““.
In this example, the name is “action” and the label is “Action”:
actionButton("action", label = "Action")
Shiny provides a family of functions that turn R objects into output for your user interface. Each function creates a specific type of output.
Use the render* function that corrresponds to the type of reactive object you are making.
On the ui
side, we use widgets (to allow user to interact with options) and output functions (to create a ui output into the server based on the widget selection).
ui <- page_sidebar(
title = "censusVis",
sidebar = sidebar(
helpText(
"Create demographic maps with information from the 2010 US Census."
),
selectInput(
"var",
label = "Choose a variable to display",
choices =
c("Percent White",
"Percent Black",
"Percent Hispanic",
"Percent Asian"),
selected = "Percent White"
),
sliderInput(
"range",
label = "Range of interest:",
min = 0,
max = 100,
value = c(0, 100)
)
),
textOutput("selected_var")
)
On the server side, we use render functions.
Let’s make our first shiny app.
First, create a GitHub workflow named 15_shiny
.
Once you finish closing it, follow these steps to create your shiny app.
Go on New
> Shiny web app
When it comes to sharing Shiny apps, you have two basic options:
Share your Shiny app as R scripts. This is the simplest way to share an app, but it works only if your users have R on their own computer (and know how to use it). Users can use these scripts to launch the app from their own R session, just like you’ve been launching the apps so far in this tutorial.
Share your Shiny app as a web page. This is definitely the most user friendly way to share a Shiny app. Your users can navigate to your app through the internet with a web browser. They will find your app fully rendered, up to date, and ready to go.
Posit (formerly RStudio) offers three ways to host your Shiny app as a web page:
shinyapps.io lets you upload your app straight from your R session to a server hosted by Posit. You have complete control over your app including server administration tools.
You’ll need to sign up for a free account.
Then, you can deploy from within RStudio using the Publish
button.
In this exercise, we learned: