0% found this document useful (0 votes)
139 views2 pages

Shiny::: Cheat Sheet

The document discusses building a Shiny app in R. It explains that a Shiny app connects a web page interface to an R session on a server. It provides information on adding inputs and outputs to collect values from users and display results. It also describes saving the app code in an app.R file and launching it with runApp.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
139 views2 pages

Shiny::: Cheat Sheet

The document discusses building a Shiny app in R. It explains that a Shiny app connects a web page interface to an R session on a server. It provides information on adding inputs and outputs to collect values from users and display results. It also describes saving the app code in an app.R file and launching it with runApp.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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 <- fluidPage(
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 server <- function(input, output, session) { checkboxGroupInput(inputId,
update the UI’s displays (by Tell the server output$hist <- renderPlot({ label, choices, selected, inline, width,
how to render Wrap code in render*() functions
running R code). hist(rnorm(input$n)) choiceNames, choiceValues)
outputs and before saving to output
})
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)
.r app.R
(optional) used in showcase mode
DESCRIPTION
README (optional) directory of supplemental .R files that are sourced dateRangeInput(inputId, label, start,
automatically, must be named "R" See annotated examples of Shiny apps by running end, min, max, format, startview,
R/ runExample(<example name>). Run runExample() weekstart, language, separator, width,
www/ (optional) directory of files to share with web browsers (images, autoclose)
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)
digits, na, …, env, quoted, outputArgs)
3. Build your own Shiny Server
rstudio.com/products/shiny/shiny-server/ renderText(expr, env, quoted, outputArgs, sep) textOutput(outputId, container, inline) submitButton(text, icon, width)
(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.
RStudio® is a trademark of RStudio, PBC • CC BY SA RStudio • info@rstudio.com • 844-448-1212 • rstudio.com • Learn more at shiny.rstudio.com • Font Awesome 5.15.3 • shiny 1.6.0 • Updated: 2021-07


f
ffff




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. fluidPage( into a "single element" that
textInput("a","") Returns has its own properties with a
) HTML panel function, e.g.
## <div class="container-fluid"> 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$ panel
panel mainPanel()
CREATE YOUR OWN REACTIVE VALUES RENDER REACTIVE OUTPUT )
*Input() functions library(shiny) render*() functions ui <- fluidPage( )
# *Input() example
(see front page) ui <- fluidPage(
(see front page) h1("Header 1"),
fluidRow()
ui <- fluidPage( hr(),
textInput("a","","A"),
textInput("a","","A") Each input function creates textOutput("b") br(), ui <- fluidPage(
)
a reactive value stored as ) Builds an object to p(strong("bold")), row
column col fluidRow(column(width = 4),
input$<inputId>. display. Will rerun code in p(em("italic")), column(width = 2, o set = 3)),
server <- body to rebuild the object p(code("code")), fluidRow(column(width = 12))
#reactiveValues example
function(input,output){
whenever a reactive value a(href="", "link"), column
reactiveValues(…) output$b <- )
server <- renderText({ in the code changes. HTML("<p>Raw html</p>")
input$a )
function(input,output){ Creates a list of reactive }) Also flowLayout(), splitLayout(), verticalLayout(),
rv <- reactiveValues() Save the results to
rv$number <- 5 values whose values you } fixedPage(), and fixedRow().
} can set. output$<outputId>.
shinyApp(ui, server) 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, library(shiny)
observeEvent(eventExpr, ui <- fluidPage( tabsetPanel(
label, domain) handlerExpr, event.env, tags$head(tags$link(rel = "stylesheet", tabPanel("tab 1", "contents"),
ui <- fluidPage( ui <- fluidPage(
textInput("a","","A"), event.quoted, handler.env, type = "text/css", href = "<file name>")) tabPanel("tab 2", "contents"),
textInput("a","","A"),
textInput("z","","Z"), Reactive expressions: actionButton("go","Go") handler.quoted, ..., label, tabPanel("tab 3", "contents")))
textOutput("b"))
• cache their value to ) suspended, priority, domain, To include JavaScript, use includeScript() or
server <-
function(input,output){
reduce computation server <- autoDestroy, ignoreNULL, 1. Place the file in the www subdirectory ui <- fluidPage( navlistPanel(
re <- reactive({ • can be called elsewhere function(input,output){ ignoreInit, once) 2. Link to it with:
tabPanel("tab 1", "contents"),
• notify dependencies observeEvent(input$go,{ tabPanel("tab 2", "contents"),
paste(input$a,input$z)}) print(input$a) Runs code in 2nd tabPanel("tab 3", "contents")))
output$b <- renderText({
re()
when invalidated }) argument when reactive tags$head(tags$script(src = "<file 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) tabPanel("tab 1", "contents"),
shinyApp(ui, server)
alternative. 1. Place the file in the www subdirectory tabPanel("tab 2", "contents"),
2. Link to it with img(src="<file name>") tabPanel("tab 3", "contents"))
REACT BASED ON EVENT REMOVE REACTIVITY
eventReactive(eventExpr, library(shiny) isolate(expr)
Themes
library(shiny)
ui <- fluidPage( valueExpr, event.env, ui <- fluidPage( Runs a code block.
textInput("a","","A"), event.quoted, value.env, textInput("a","","A"),
actionButton("go","Go"),
value.quoted, ..., label, textOutput("b")
Returns a non-reactive Use the bslib package to add existing themes to Build your own theme by customizing individual
textOutput("b")
) domain, ignoreNULL, ) copy of the results. your Shiny app ui, or make your own. arguments.
server <- ignoreInit) server <- bs_theme(bg = "#558AC5",
function(input,output){ function(input,output){ library(bslib)
re <- eventReactive( Creates reactive output$b <- fg = "#F9B02D",
ui <- fluidPage(
input$go,{input$a})
output$b <- renderText({
expression with code in renderText({
theme = bs_theme( ...)
isolate({input$a})
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.
shinyApp(ui, server)
change. )
bs_themer() Place within the server function to
bootswatch_themes() Get a list of themes. use the interactive theming widget.
RStudio® is a trademark of RStudio, PBC • CC BY SA RStudio • info@rstudio.com • 844-448-1212 • rstudio.com • Learn more at shiny.rstudio.com • Font Awesome 5.15.3 • shiny 1.6.0 • Updated: 2021-07


ff


You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy