21 Default Shiny Example
If you select the “Single File” option when creating a shiny application, you will get the example code below. This file contains both the ui and server aspects of our shiny app. This works well if the app is very simple, but I find this becomes very difficult to manage as the app becomes more complex.
The app is rendered below the code chunk; you should be able to interact with it. Let’s walk through the logic of this app.
- ui
- The ui represents a
fluidPage() titlePanel()hads a title to the app- Create an application with a sidebar using
sidebarLayout()- Specify what should be represented in the sidebar within
sidebarPanel()- This app includes a slider using
sliderInput()
- This app includes a slider using
- Specify what should appear outside of the sidebar using
mainPanel()- Plot the output from the object created within the server using
plotOutput(). - “distPlot” within
plotOutput()is in reference to the objectoutput$distPlotcreated in the server withrenderPlot()
- Plot the output from the object created within the server using
- Specify what should be represented in the sidebar within
- The ui represents a
- server
- server represents a function with two arguements
inputandoutputinputrepresents objects from the uioutputrepresents objects that were created in the server but you want to reference in the ui
renderPlot()is used, in this case, to create an histogram for which bin size can be updated- This object is stored using
output$distPlot <- renderPlot({}).- Specifying
outputallows us to reference this object in the ui. distPlotis the name we will use in the ui to reference this object, which can be seen in the ui callplotOutput("distPlot")
- Specifying
renderPlot({})requires curly brackets “{}” within the function call- Within
renderPlot({})is the code that creates the histogramx <- faithful[, 2]- subsets the
faithfuldata set (pre-loaded in base-R) xrepresents the second column offaithfulstored as a vector.
- subsets the
bins <- seq(min(x), max(x), length.out = input$bins + 1)binswill be used to specify the break-points used to create the bin size in the histogram creationseq(from, to, length.out)- sequence
fromone valuetoanother length.outis used to specify how many breakpoints should be between the specifiedfromandtoarguementslength.out = input$bins + 1input$binscomes from thesliderInput()within the ui
input$binsis that part that provides the updates to the histogram, ultimately making this app interactive
- sequence
hist(x, breaks = bins, col = 'darkgray', border = 'white')hist()creates a histogram plotxis thefaithfulvector created abovebreaks = binsspecify the break-points for the histogram barscol = 'darkgray'specify the color of the histogram barsborder = 'white'specify the color of the outline around the histogram bars
- This object is stored using
- server represents a function with two arguements
shinyApp(ui = ui, server = server)- The arguements to
shinyApp()areuiandserver - Renders the shiny app by specifying the appropriate ui and server objects
- In this case, the arguements to
shinyApp()are the same as the object names
- The arguements to
#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com/
#
library(shiny)
## Warning: package 'shiny' was built under R version 3.5.3
# 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")
)
)
)
# 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')
})
}
# Run the application
shinyApp(ui = ui, server = server)
## PhantomJS not found. You can install it with webshot::install_phantomjs(). If it is installed, please make sure the phantomjs executable can be found via the PATH variable.
Shiny applications not supported in static R Markdown documents