Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Shiny : : CHEAT SHEET

Building an App To generate the template, type shinyapp and press Tab in the RStudio IDE
or go to File > New Project > New Directory > Shiny Web Application
Inputs
Collect values from the user.
A Shiny app is a web page (ui)
# app.R
connected to a computer library(shiny) Access the current value of an input object with
running a live R session (server). Customize the UI with Layout Functions input$<inputId>. Input values are reactive.
In ui nest R ui <- uidPage(
functions to numericInput(inputId = "n",
Add Inputs with *Input() functions actionButton(inputId, label, icon,
build an HTML "Sample size", value = 25), width, …)
interface plotOutput(outputId = "hist") Add Outputs with *Output() functions
) actionLink(inputId, label, icon, …)
Users can manipulate the UI,
which will cause the server to checkboxGroupInput(inputId,
Tell the server server <- function(input, output, session) {
update the UI’s displays (by output$hist <- renderPlot({ Wrap code in render*() functions label, choices, selected, inline, width,
how to render choiceNames, choiceValues)
running R code). hist(rnorm(input$n)) before saving to output
outputs and })
respond to } Refer to UI inputs with input$<id> checkboxInput(inputId, label, value,
Save your template as app.R. inputs with R and outputs with output$<id> width)
Keep your app in a directory
along with optional extra files. shinyApp(ui = ui, server = server) dateInput(inputId, label, value, min,
max, format, startview, weekstart,
app-name Call shinyApp() to combine ui and server into an interactive app! language, width, autoclose,
The directory name is the app name datesdisabled, daysofweekdisabled)
le
.r app.R
(optional) used in showcase mode
le DESCRIPTION
README (optional) directory of supplemental .R files that are sourced dateRangeInput(inputId, label, start,
le
automatically, must be named "R" See annotated examples of Shiny apps by running end, min, max, format, startview,
fo R/ runExample(<example name>). Run runExample() weekstart, language, separator, width,
www/ (optional) directory of files to share with web browsers (images, autoclose)
fo CSS, .js, etc.), must be named "www" with no arguments for a list of example names.
Launch apps stored in a directory with runApp(<path to directory>). fileInput(inputId, label, multiple,
accept, width, buttonLabel, placeholder)

Share Outputs render*() and *Output() functions work together to add R output to the UI. numericInput(inputId, label, value,
min, max, step, width)
Share your app in three ways: DT::renderDataTable(expr, options, dataTableOutput(outputId)
searchDelay, callback, escape, env, quoted, passwordInput(inputId, label, value,
1. Host it on shinyapps.io, a cloud based outputArgs) width, placeholder)
service from RStudio. To deploy Shiny apps:
renderImage(expr, env, quoted, deleteFile, imageOutput(outputId, width, height, radioButtons(inputId, label,
Create a free or professional outputArgs) click, dblclick, hover, brush, inline) choices, selected, inline, width,
account at shinyapps.io choiceNames, choiceValues)
renderPlot(expr, width, height, res, …, alt, env, plotOutput(outputId, width, height, click,
Click the Publish icon in RStudio IDE, or run: quoted, execOnResize, outputArgs) dblclick, hover, brush, inline) selectInput(inputId, label, choices,
rsconnect::deployApp("<path to directory>") selected, multiple, selectize, width, size)
Also selectizeInput()
renderPrint(expr, env, quoted, width, verbatimTextOutput(outputId,
2. Purchase RStudio Connect, a outputArgs) placeholder)
publishing platform for R and Python. sliderInput(inputId, label, min, max,
value, step, round, format, locale, ticks,
rstudio.com/products/connect/ renderTable(expr, striped, hover, bordered, tableOutput(outputId) animate, width, sep, pre, post,
spacing, width, align, rownames, colnames, timeFormat, timezone, dragRange)
3. Build your own Shiny Server digits, na, …, env, quoted, outputArgs)
rstudio.com/products/shiny/shiny-server/ textOutput(outputId, container, inline) submitButton(text, icon, width)
renderText(expr, env, quoted, outputArgs, sep) (Prevent reactions for entire app)
renderUI(expr, env, quoted, outputArgs) uiOutput(outputId, inline, container, …)
htmlOutput(outputId, inline, container, …) textInput(inputId, label, value, width,
placeholder) Also textAreaInput()
These are the core output types. See htmlwidgets.org for many more options.
CC BY SA Posit So ware, PBC • info@posit.co • posit.co • Learn more at shiny.rstudio.com • Font Awesome 5.15.3 • shiny 1.6.0 • Updated: 2021-07


fi
fi
fi
fl

ft




Reactivity UI - An app’s UI is an HTML document. Layouts
Reactive values work together with reactive functions. Call a reactive value from within the arguments of one Use Shiny’s functions to assemble this HTML with R. Combine multiple elements
of these functions to avoid the error Operation not allowed without an active reactive context. uidPage( into a "single element" that
textInput("a","") Returns has its own properties with a
) HTML panel function, e.g.
## <div class="container- uid"> wellPanel(

## <div class="form-group shiny-input-container"> dateInput("a", ""),
## <label for="a"></label> submitButton()
## <input id="a" type="text" )
## class="form-control" value=""/>
## </div> absolutePanel() navlistPanel()
## </div> conditionalPanel() sidebarPanel()
fixedPanel() tabPanel()
headerPanel() tabsetPanel()
Add static HTML elements with tags, a list inputPanel() titlePanel()
of functions that parallel common HTML mainPanel() wellPanel()
tags, e.g. tags$a(). Unnamed arguments
will be passed into the tag; named Organize panels and elements into a layout with a
arguments will become tag attributes. layout function. Add elements as arguments of the
layout functions.
Run names(tags) for a complete list. sidebarLayout()
tags$h1("Header") -> <h1>Header</h1> ui <- fluidPage(
sidebarLayout(
The most common tags have wrapper functions. You side main sidebarPanel(),
do not need to prefix their names with tags$ mainPanel()
CREATE YOUR OWN REACTIVE VALUES RENDER REACTIVE OUTPUT
panel
panel )
*Input() functions render*() functions ui <- uidPage( )
# *Input() example library(shiny)
(see front page) (see front page) h1("Header 1"),
fluidRow()
ui <- uidPage( ui <- uidPage( hr(),
textInput("a","","A") Each input function creates textInput("a","","A"), br(), ui <- fluidPage(
) textOutput("b") Builds an object to row
column col
a reactive value stored as p(strong("bold")), fluidRow(column(width = 4),
) display. Will rerun code in
input$<inputId>. p(em("italic")),
column(width = 2, o set = 3)),
server <- function(input,output){ body to rebuild the object p(code("code")),
output$b <- whenever a reactive value a(href="", "link"), column fluidRow(column(width = 12))
#reactiveValues example
reactiveValues(…) renderText({
in the code changes. HTML("<p>Raw html</p>") )
server <- function(input,output){ input$a
rv <- reactiveValues() Creates a list of reactive }) )
Also flowLayout(), splitLayout(), verticalLayout(),
rv$number <- 5 values whose values you } Save the results to fixedPage(), and fixedRow().
}
can set. shinyApp(ui, server)
output$<outputId>.
To include a CSS file, use includeCSS(), or
1. Place the file in the www subdirectory Layer tabPanels on top of each other,
CREATE REACTIVE EXPRESSIONS PERFORM SIDE EFFECTS and navigate between them, with:
2. Link to it with:
library(shiny) reactive(x, env, quoted, observeEvent(eventExpr, ui <- fluidPage( tabsetPanel(
ui <- uidPage( label, domain)
library(shiny)
handlerExpr, event.env, tags$head(tags$link(rel = "stylesheet", tabPanel("tab 1", "contents"),
ui <- uidPage( event.quoted, handler.env, type = "text/css", href = "< le name>")) tabPanel("tab 2", "contents"),
textInput("a","","A"),
textInput("z","","Z"),
Reactive expressions: textInput("a","","A"),
handler.quoted, ..., label, tabPanel("tab 3", "contents")))
textOutput("b")) • cache their value to )
actionButton("go","Go")
suspended, priority, domain, To include JavaScript, use includeScript() or
server <- function(input,output){
reduce computation autoDestroy, ignoreNULL, 1. Place the file in the www subdirectory ui <- fluidPage( navlistPanel(
• can be called elsewhere server <- function(input,output){
ignoreInit, once) tabPanel("tab 1", "contents"),
re <- reactive({
observeEvent(input$go,{ 2. Link to it with: tabPanel("tab 2", "contents"),
paste(input$a,input$z)}) • notify dependencies print(input$a) Runs code in 2nd
output$b <- renderText({
when invalidated }) tabPanel("tab 3", "contents")))
re()
} argument when reactive tags$head(tags$script(src = "< le name>"))
}
}) Call the expression with values in 1st argument ui <- navbarPage(title = "Page",
function syntax, e.g. re(). change. See observe() for IMAGES To include an image:
shinyApp(ui, server)
shinyApp(ui, server) tabPanel("tab 1", "contents"),
alternative. 1. Place the file in the www subdirectory tabPanel("tab 2", "contents"),
2. Link to it with img(src="< le name>") tabPanel("tab 3", "contents"))
REACT BASED ON EVENT REMOVE REACTIVITY
eventReactive(eventExpr, isolate(expr)
library(shiny)
ui <- uidPage( valueExpr, event.env, library(shiny)

event.quoted, value.env, ui <- uidPage( Runs a code block. Themes


textInput("a","","A"),
textInput("a","","A"), Returns a non-reactive Use the bslib package to add existing themes to Build your own theme by customizing individual
actionButton("go","Go"), value.quoted, ..., label,
textOutput("b")
domain, ignoreNULL, )
textOutput("b") copy of the results. your Shiny app ui, or make your own. arguments.
)
ignoreInit) server <- function(input,output){ bs_theme(bg = "#558AC5",
server <- function(input,output){
re <- eventReactive( Creates reactive output$b <- library(bslib)
fg = "#F9B02D",
renderText({
input$go,{input$a}) expression with code in isolate({input$a})
ui <- uidPage(
...)
theme = bs_theme(
output$b <- renderText({
re()
2nd argument that only })
bootswatch = "darkly",
}) invalidates when reactive }
... ?bs_theme for a full list
} values in 1st argument shinyApp(ui, server) ) of arguments.
change. )
bs_themer() Place within the server function to
bootswatch_themes() Get a list of themes. use the interactive theming widget.
CC BY SA Posit So ware, PBC • info@posit.co • posit.co • Learn more at shiny.rstudio.com • Font Awesome 5.15.3 • shiny 1.6.0 • Updated: 2021-07
fl

fl
fl
fl
fl
fl
fl
fl
fl

ft

fl


ff

fi
fi

fi



You might also like