A LOT of the content of this lecture (and labs to follow) are based on the following resources:
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 functionThe 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 specified 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: 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).
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Old Faithful Geyser Data"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30)
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot")
)
)
)On the server side, we use render functions, create outputs using the object names from ui side, and use input names from the ui side.
# Define server logic required to draw a histogram
server <- function(input, output) {
output$distPlot <- renderPlot({
# generate bins based on input$bins from ui.R
x <- faithful[, 2]
bins <- seq(min(x),
max(x),
length.out = input$bins + 1)
# draw the histogram with the specified number of bins
hist(x,
breaks = bins,
col = 'darkgray',
border = 'white',
xlab = 'Waiting time to next eruption (in mins)',
main = 'Histogram of waiting times')
})
}One of my first shiny apps was to demonstrate data from GA cotton variety trials. Let’s check it out:
Let’s make our first shiny app.
First, create a GitHub workflow named 14_shiny.
Once you finish cloning it, follow these steps to create your shiny app.
Go on New > Shiny web app
Let’s play around:
When it comes to sharing Shiny apps, you have two basic options:
When it comes to sharing Shiny apps, you have two basic options:
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.
A few tips that will help you succeed when deploying:
data folderIn this exercise, we learned: