Chapter 8 Shiny
8.1 Introduction
Shiny is an R package that makes it easy to build interactive web applications (apps) straight from R. Shiny apps are contained in a single script called app.R
. The script app.R
lives in a directory (for example, newdir/
) and the app can be run with runApp("newdir")
.
app.R
has three components:
a user interface object (ui)
a server function (server)
a call to the shinyApp function
The user interface (ui) object controls the layout and appearance of your app. The server function contains the instructions that your computer needs to build your app. Finally the shinyApp function creates Shiny app objects from an explicit UI/server pair.
8.2 Cheatsheet
8.2.1 ui: Layout
8.2.2 ui: Input
Input widgets are used to accept content interactively from the user. These widgets usually end in Input like selectInput()
. Below are usage examples of several of shiny’s built-in widgets. Every widget has a variable name which is accessible through input$
in the server function. For example, the value of a variable named text-input
would be accessed through input$text-input
.
library(shiny)
shinyApp(
ui=fluidPage(
fluidRow(
column(6,
fileInput("file-input","fileInput:"),
selectInput("select-input",label="selectInput",choices=c("A","B","C")),
numericInput("numeric-input",label="numericInput",value=5,min=1,max=10),
sliderInput("slider-input",label="sliderInput",value=5,min=1,max=10),
textInput("text-input",label="textInput"),
textAreaInput("text-area-input",label="textAreaInput"),
dateInput("date-input",label="dateInput"),
dateRangeInput("date-range-input",label="dateRangeInput"),
radioButtons("radio-button",label="radioButtons",choices=c("A","B","C"),inline=T),
checkboxInput("checkbox","checkboxInput",value=FALSE),
actionButton("action-button","Action"),
hr(),
submitButton()
)
)
),server=function(input,output){},
options=list(height=900))
8.2.3 ui: output
Output widgets are used to display information to the user on the webpage. These widgets usually end in Output like textOutput()
. Every widget has a variable name accessible under output$
to which content is written in the server function. Render functions are used to write content to output widgets. For example renderText()
is used to write text data to textOutput()
widget.