13 Shiny
Lets you present tables and charts that will react to inputs.
13.1 Basic Shiny Structure
13.2 User Interface
13.2.1 Layout
Various layouts can be used however the most common is a fluid layout. The page can be divided into rows and columns. Fixed text and styling can also be added.
library(shiny)
ui <- fluidPage(
fluidRow(
column(width=4, "row1 column1", style = "background-color:red;"),
column(width=4, "row1 column2", style = "background-color:orange;"),
column(width=4, "row1 column3", style = "background-color:yellow;")
),
fluidRow(
column(width=4, "row2 column1", style = "background-color:green;"),
column(width=4, "row2 column2", style = "background-color:blue;"),
column(width=4, "row2 column3", style = "background-color:purple;")
)
)
server <- function(input, output) {
}
shinyApp(ui, server)13.2.2 Inputs
There are various Inputs that can be added to the UI. Behind the scenes these generate HTML code: * sliderInput() # Adds an input slider * fileInput() # Adds a file selector * textInput() # Adds a text input box * selectInput() # Adds a dropdown list * actionButton() # Adds an action button * dateRangeInput() # Adds a date selector The first argument of all the inputs/ouputs is a unique id.
ui <- fluidPage(
fluidRow(
column(width=4,
sliderInput("slider", "Generate n randoms", min = 0, max = 1000, value = 500, step = 100)
),
column(width=4,
fileInput("loadfile", "Choose file")),
column(width=4,
selectInput("dropdown", "Select an option", c("Rare" = "1",
"Medium" = "2",
"Well Done" = "3")))
)
)
server <- function(input, output) {
}
shinyApp(ui, server)13.2.3 Outputs
There are various Outputs that can be added to the UI: * htmlOutput() # Adds space for text * tableOutput() # Adds space for a table * plotOutput() # Adds space for a chart
Nb. these will be empty until a function is added to the server that adds content.
ui <- fluidPage(
fluidRow(
htmlOutput("text1", width="75%", height="50px"),
tableOutput("table1"),
plotOutput("chart1", width="75%", height="100px")
)
)
server <- function(input, output) {
}
shinyApp(ui, server)Other packages extend shiny ui outputs. * plotlyOutput # Adds space for a plotly chart [plotly] * ggiraphOutput # Adds space for a ggiraph chart [ggiraph] * DTOutput # Adds space for a data table [DT]
13.3 Server
UI inputs and outputs wont work unless they link to a server process/function.
13.3.1 Linking to UI outputs
- renderPlot() # creates reactive ggplot output
- renderText() # creates reactive tetx output
- renderTable() # creates reactive table output
- renderDataTable() # creates reactive DT output The UI output id and the server output name must match.
ui <- fluidPage(
fluidRow(
plotOutput("chart1")
)
)
server <- function(input, output) {
# This output links to the ui plotOutput via the id "chart1"
output$chart1 <- renderPlot({
plot(mtcars$wt, mtcars$mpg)
})
}
shinyApp(ui, server)Other packages extend shiny server outputs. * renderPlotly # creates reactive plotly chart [plotly] * renderggiraph # creates reactive ggiraph chart [ggiraph]
13.3.2 Linking to UI inputs
The UI input id and the server input name must match.
ui <- fluidPage(
fluidRow(
selectInput("dropdown", "Select an option", c("Weight" = "wt",
"Horespower" = "hp",
"Cylinders" = "cyl"))
),
fluidRow(
plotOutput("chart1")
)
)
server <- function(input, output) {
# This output links to the ui plotOutput via the id "chart1"
output$chart1 <- renderPlot({
plot(mtcars[[input$dropdown]], mtcars$mpg)
})
}
shinyApp(ui, server)13.4 Shiny Dashboard
Provides a dashboard template for Shiny Apps.
library(shiny) library(shinydashboard)