Professional Documents
Culture Documents
Basics Building an App Complete the template by adding arguments to fluidPage() and a
body to the server function. Inputs
A Shiny app is a web page (UI) connected to a Add inputs to the UI with *Input() functions
library(shiny) collect values from the user
computer running a live R session (Server)
Add outputs with *Output() functions
ui <- fluidPage( Access the current value of an input object with
numericInput(inputId = "n",
"Sample size", value = 25), input$<inputId>. Input values are reactive.
Tell server how to render outputs with R in plotOutput(outputId = "hist")
the server function. To do this: ) actionButton(inputId, label, icon,
…)
1. Refer to outputs with output$<id> server <- function(input, output) {
output$hist <- renderPlot({
2. Refer to inputs with input$<id> hist(rnorm(input$n))
actionLink(inputId, label, icon, …)
Users can manipulate the UI, which will cause })
the server to update the UI’s displays (by 3. Wrap code in a render*() function before }
running R code). saving to output shinyApp(ui = ui, server = server) checkboxGroupInput(inputId, label,
Save your template as app.R. Alternatively, split your template into two files named ui.R and server.R. choices, selected, inline)
APP TEMPLATE library(shiny) # ui.R
fluidPage(
ui.R contains everything checkboxInput(inputId, label,
Begin writing a new app with this template. ui <- fluidPage(
numericInput(inputId = "n", you would save to ui. value)
numericInput(inputId = "n",
Preview the app by running the code at the R "Sample size", value = 25), "Sample size", value = 25),
command line. plotOutput(outputId = "hist") plotOutput(outputId = "hist")
) ) server.R ends with the dateInput(inputId, label, value, min,
function you would save max, format, startview, weekstart,
server <- function(input, output) { # server.R
library(shiny) output$hist <- renderPlot({ to server. language)
ui <- fluidPage() hist(rnorm(input$n)) function(input, output) {
}) output$hist <- renderPlot({
server <- function(input, output){} } hist(rnorm(input$n)) No need to call
}) dateRangeInput(inputId, label,
shinyApp(ui = ui, server = server) shinyApp(ui = ui, server = server) } shinyApp(). start, end, min, max, format,
Save each app as a directory that holds an app.R file (or a server.R file and a ui.R file) plus optional extra files. startview, weekstart, language,
• uiHTML
- nested R functions that assemble an
user interface for your app
app-name The directory name is the name of the app
separator)
"
.r app.R (optional) defines objects available to both
Launch apps with fileInput(inputId, label, multiple,
• server - a function with instructions on how
to build and rebuild the R objects displayed
"
"
global.R
DESCRIPTION
ui.R and server.R
(optional) used in showcase mode
runApp(<path to
directory>)
accept)
in the UI " README (optional) data, scripts, etc.
" <other files> (optional) directory of files to share with web numericInput(inputId, label, value,
• shinyApp - combines ui and server into
an app. Wrap with runApp() if calling from a
! www browsers (images, CSS, .js, etc.) Must be named "www" min, max, step)