You are on page 1of 2

1.

Structure Each app is a directory that render* functions

Shiny Cheat Sheet


contains a server.R file and usually a ui.R file function expects !
creates
(plus optional extra files) renderDataTable any table-like object DataTables.js table
renderImage list of image attributes HTML image
learn more at shiny.rstudio.com renderPlot plot plot
Shiny 0.10.0 Updated: 6/14 The directory name is renderPrint
app-name the name of the app
any printed output text
renderTable any table-like object plain table
!
.r server.R renderText character string text
!
.r ui.R (optional) used in
renderUI Shiny tag object or UI element (HTML)
! DESCRIPTION showcase mode
! README input values are reactive.
(optional) data,
! <other files> scripts, etc. They must be surrounded with one of:
2. server.R A set of instructions that build " www render* - creates a shiny UI component
the R components of your app. To write server.R: reactive - creates a reactive expression
(optional) directory of files to share with
observe - creates a reactive observer
web browsers (images, CSS, .js, etc.)
Must be named "www"
isolate - creates a non-reactive copy of a reactive object

•A Provide server.R with the minimum necessary code,


shinyServer(function(input, output) {}) server.R
•B Define the R components for your app between the
3. Execution Place code where it will be
# load libraries, scripts, data
braces that follow function(input, output) ! run the minimum necessary number of times
A shinyServer(function(input, output) { B
•C Save each R component in your UI as ! Run once - code placed outside of shinyServer will be run
output$<component name> # make user specific variables once, when you first launch your app. Use this code to set
up the tools that your server will only need one copy of.
•D Create each output component with a render* output$text <- renderText({
function. input$title
})
Run once per user - code placed inside shinyServer will
•E Give each render* function the R code the server ! D
be run once each time a user visits your app (or refreshes
his or her browser). Use this code to set up the tools that
needs to build the component. The server will note C output$plot <- renderPlot({
any reactive values that appear in the code and will x <- mtcars[ , input$x] F your server will need a unique copy of for each user.
E
rebuild the component whenever these values y <- mtcars[ , input$y]
change. plot(x, y, pch = 16) Run often - code placed within a render*, reactive, or
}) observe function will be run many times. Place here only the
•F Refer to widget values with input$<widget name> ! code that the server needs to rebuild a UI component after a
}) widget changes.

render* - An output will automatically Reactive expression - use reactive to isolate - use use isolate to use an input observe - use observe to create code
4. Reactivity When an update whenever an input in its create objects that will be used in without depending on it. Shiny will not
rebuild the output when the isolated
that runs when an input changes, but
does not create an output object.
input changes, the server render* function changes. multiple outputs. input changes.
will rebuild each output that output$y input$a
depends on it (even if the input$a output$z input$a x output$z input$a observer
output$z
dependence is indirect). You input$b

can control this behavior by x <- reactive({


input$a
shaping the chain of output$z <- renderText({ }) output$z <- renderText({ observe({
dependence. })
input$a !
output$y <- renderText({
paste(
isolate(input$a),
input$a
# code to run
x() input$b })
}) )
RStudio® and Shiny™ are trademarks of RStudio, Inc.
All rights reserved info@rstudio.com output$z <- renderText({ )
844-448-1212 rstudio.com x()
})
ui.R
5. ui.R A description of your app’s User Interface (UI), the web page that displays your app.
A shinyUI(fluidPage( To write ui.R:
! • Include the minimum necessary code for ui.R, shinyUI(fluidPage())
A
titlePanel("mtcars data"),
* note: use navbarPage instead of fluidPage if you’d like your app to have multiple pages connected by a navbar
B sidebarLayout(
sidebarPanel(
! • Build a layout for your UI. sidebarLayout provides a default layout when used with sidebarPanel and mainPanel.
B
C textInput("title", "Plot title:", splitLayout, flowLayout, and inputLayout divide the page into equally spaced regions. fluidRow and column work
value = "x v y"), together to create a grid-based layout, which you can use to layout a page or a panel. creates
! sidebarLayout splitLayout flowLayout/inputPanel fluidRow
selectInput("x", "Choose an x var:",
choices = names(mtcars), object object object
selected = "disp"), 1 2 3
column row col
! side main object 1 object 2
selectInput("y", "Choose a y var:", panel panel
choices = names(mtcars), object
3 column
selected = "mpg")
),
! shinyUI(fluidPage(
mainPanel( fluidRow(
h3(textOutput("text")), shinyUI(fluidPage( shinyUI(fluidPage( column(width = 4, …),
shinyUI(fluidPage(
splitLayout( flowLayout( column(width = 2,
plotOutput("plot") sidebarLayout(
numericInput(…),
sidebarPanel(…), numericInput(…), offset = 3, …),
) selectInput(…) selectInput(…), ),
mainPanel(…)
) ) ) sliderInput(…) fluidRow(
)) )) ) column(width = 12, …)
))
)) )
C In each panel or column, place… ))

R components - These are the output objects that • Widgets - The first argument of each widget function HTML elements - Add html elements with shiny
you defined in server.R. To place a component: is the <name> for the widget. You can access a functions that parallel common HTML tags.
widget’s current value in server.R with input$<name>
1. Select the *Output function that builds the type of object you a tags$col
tags$colgroup h1
tags$form tags$input
tags$ins
tags$output
p
tags$sub
tags$summary
tags$abbr
want to place in the UI. widget function common arguments tags$address tags$command h2 tags$kbd tags$param tags$sup
Action button actionButton inputId, label tags$data h3 tags$keygen pre tags$table
2. Pass the *Output function a character string that corresponds checkbox inputId, label, value
tags$area
tags$article tags$datalist h4 tags$label tags$progress tags$tbody
checkboxInput
to the name you assigned the object in server.R, e.g. checkbox group checkboxGroupInput inputId, label, choices, selected tags$aside tags$dd h5 tags$legend tags$q tags$td
tags$audio tags$del h6 tags$li tags$ruby tags$textarea
date selector dateInput inputId, label, value, min, max, format tags$b tags$details tags$head tags$link tags$rp tags$tfoot
output$plot <- renderPlot({ ... }) plotOutput("plot") date range selector dateRangeInput inputId, label, start, end, min, max, format tags$base tags$dfn tags$header tags$mark tags$rt tags$th
tags$bdi div tags$hgroup tags$map tags$s tags$thead
file uploader fileInput inputId, label, multiple tags$samp tags$time
tags$dl hr tags$menu
*Output functions Number field numericInput inputId, label, value, min, max, step
tags$bdo
tags$blockquote tags$dt HTML tags$meta tags$script tags$title
Radio buttons em tags$i tags$meter tags$section tags$tr
dataTableOutput tableOutput radioButtons inputId, label, choices, selected tags$body
tags$embed tags$iframe tags$nav tags$select tags$track
select box inputId, label, choices, selected, multiple br
htmlOutput textOutput selectInput
tags$button tags$eventsource img tags$noscript tags$small tags$u
slider sliderInput inputId, label, min, max, value, step tags$fieldset includeCSS tags$object tags$source tags$ul
imageOutput uiOutput submit button
tags$canvas
tags$figcaption includeMarkdo tags$ol span tags$var
submitButton text tags$caption
plotOutput verbatimTextOutput text field textInput inputId, label, value tags$cite tags$figure wn tags$optgroup strong tags$video
code tags$footer includeScript tags$option tags$style tags$wbr

6. Run your app 7. Share your app Launch your app as a live web page that users can visit online.

runApp - run from local files


runGitHub - run from files hosted on www.GitHub.com ShinyApps.io Shiny Server Shiny Server Pro
runGist - run from files saved as a gist (gist.github.com) Host your apps on RStudio’s Build your own linux server to Build a commercial server
runURL - run from files saved at any URL server. Free and paid options host apps. Free and open source. with authentication, resource
www.shinyapps.io shiny.rstudio.com/deploy management, and more.
RStudio® and Shiny™ are trademarks of RStudio, Inc.
All rights reserved info@rstudio.com
844-448-1212 rstudio.com
shiny.rstudio.com/deploy

You might also like