Sei sulla pagina 1di 2

Building an App - Complete the template by adding arguments to fluidPage() and a body to the server function.

Interactive Web Apps library(shiny) Inputs - collect values from the user
with shiny Cheat Sheet Add inputs to the UI with *Input() functions
Add outputs with *Output() functions
ui <- fluidPage( Access the current value of an input object with input
numericInput(inputId = "n", $<inputId>. Input values are reactive.
learn more at shiny.rstudio.com "Sample size", value = 25),
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, )
3. Wrap code in a render*() function before }
Basics saving to output shinyApp(ui = ui, server = server)
checkboxGroupInput(inputId, label,
A Shiny app is a web page (UI) connected to a choices, selected, inline)
computer running a live R session (Server) Save your template as app.R. Alternatively, split your template into two files named ui.R and server.R.
library(shiny) # ui.R
fluidPage(
ui.R contains everything
ui <- fluidPage( you would save to ui.
numericInput(inputId = "n", numericInput(inputId = "n", checkboxInput(inputId, label, value)
"Sample size", value = 25), "Sample size", value = 25),
plotOutput(outputId = "hist") plotOutput(outputId = "hist")
) ) server.R ends with the
function you would save dateInput(inputId, label, value, min,
Users can manipulate the UI, which will cause the server <- function(input, output) { # server.R max, format, startview, weekstart,
server to update the UIs displays (by running R code). output$hist <- renderPlot({ to server.
hist(rnorm(input$n)) function(input, output) { language)
}) output$hist <- renderPlot({
App template } hist(rnorm(input$n)) No need to call
})
Begin writing a new app with this template. Preview shinyApp(ui = ui, server = server) } shinyApp().
dateRangeInput(inputId, label, start,
the app by running the code at the R command line. end, min, max, format, startview,
Save each app as a directory that contains an app.R file (or a server.R file and a ui.R file) plus optional extra files. weekstart, language, separator)
library(shiny) app-name The directory name is the name of the app
ui <- fluidPage() $
.r app.R (optional) defines objects available to both
server <- function(input, output){} $ global.R ui.R and server.R Launch apps with fileInput(inputId, label, multiple,
DESCRIPTION
runApp(<path to accept)
shinyApp(ui = ui, server = server) $ (optional) used in showcase mode directory>)
$ README (optional) data, scripts, etc.
ui - nested R functions that assemble an HTML user
interface for your app
$ <other files>
www
(optional) directory of files to share with web numericInput(inputId, label, value,
# browsers (images, CSS, .js, etc.) Must be named "www" min, max, step)
server - a function with instructions on how to
Outputs - render*() and *Output() functions work together to add R output to the UI
build and rebuild the R objects displayed in the UI
shinyApp - combines ui and server into a
DT::renderDataTable(expr, works dataTableOutput(outputId, icon, )
passwordInput(inputId, label, value)
functioning app. Wrap with runApp() if calling from with
a sourced script or inside a function. options, callback, escape,
env, quoted)
radioButtons(inputId, label, choices,
selected, inline)
Share your app renderImage(expr, env, quoted, deleteFile) imageOutput(outputId, width, height, click,
dblclick, hover, hoverDelay, hoverDelayType,
The easiest way to share your app brush, clickId, hoverId, inline)
is to host it on shinyapps.io, a selectInput(inputId, label, choices,
renderPlot(expr, width, height, res, , env, plotOutput(outputId, width, height, click,
cloud based service from RStudio selected, multiple, selectize, width,
quoted, func) dblclick, hover, hoverDelay, hoverDelayType, size) (also selectizeInput())
brush, clickId, hoverId, inline)
1. Create a free or professional account at renderPrint(expr, env, quoted, func, verbatimTextOutput(outputId)
http://shinyapps.io width) sliderInput(inputId, label, min, max,
value, step, round, format, locale,
2. Click the Publish icon in the RStudio IDE renderTable(expr,, env, quoted, func) tableOutput(outputId) ticks, animate, width, sep, pre, post)
(>=0.99) or run:
rsconnect::deployApp("<path to directory>")
renderText(expr, env, quoted, func) textOutput(outputId, container, inline) submitButton(text, icon)
(Prevents reactions across entire app)
Build or purchase your own Shiny Server

! at www.rstudio.com/products/shiny-server/ renderUI(expr, env, quoted, func) uiOutput(outputId, inline, container, )


" & htmlOutput(outputId, inline, container, ) textInput(inputId, label, value)

RStudio is a trademark of RStudio, Inc. CC BY RStudio info@rstudio.com 844-448-1212 rstudio.com More cheat sheets at http://www.rstudio.com/resources/cheatsheets/ Learn more at shiny.rstudio.com/tutorial shiny 0.12.0 Updated: 01/16
Reactivity UI Layouts
Reactive values work together with reactive functions. Call a reactive value from within the arguments of one of An apps UI is an HTML document. Use Shinys Combine multiple elements into a "single element"
these functions to avoid the error Operation not allowed without an active reactive context. functions to assemble this HTML with R. that has its own properties with a panel function, e.g.
fluidPage(
Returns
Trigger Modularize
textInput("a","")
HTML
wellPanel(
) dateInput("a", ""),
arbitrary code run(this) reactions ## <div class="container-fluid"> submitButton()
observeEvent()
reactive() Prevent reactions ## <div class="form-group shiny-input-container"> )
observe() isolate() ## <label for="a"></label>
## <input id="a" type="text"
## class="form-control" value=""/>
absolutePanel() inputPanel() tabPanel()
## </div> conditionalPanel() mainPanel() tabsetPanel()
## </div> fixedPanel() navlistPanel() titlePanel()
headerPanel() sidebarPanel() wellPanel()
input$x expression() output$y Add static HTML elements with tags, a list of
functions that parallel common HTML tags, e.g.
Organize panels and elements into a layout with a
tags$a(). Unnamed arguments will be passed layout function. Add elements as arguments of the
into the tag; named arguments will become tag layout functions.
Create your own Update Render fluidRow()
attributes.
reactive values reactive output
tags$a tags$data tags$h6 tags$nav tags$span ui <- fluidPage(
reactiveValues()
Delay reactions render*() tags$abbr tags$datalist tags$head tags$noscript tags$strong column row col fluidRow(column(width = 4),
*Input() eventReactive() tags$address tags$dd tags$header tags$object tags$style column(width = 2, oset = 3)),
tags$area tags$del tags$hgroup tags$ol tags$sub fluidRow(column(width = 12))
tags$article tags$details tags$hr tags$optgroup tags$summary column
tags$aside tags$dfn tags$HTML tags$option tags$sup )
tags$audio tags$div tags$i tags$output tags$table
Create your own reactive values Render reactive output tags$b tags$dl tags$iframe tags$p tags$tbody flowLayout()
CC 2015 RStudio, Inc. tags$base tags$dt tags$img tags$param tags$td ui <- fluidPage(
# example snippets *Input() functions library(shiny) render*() functions tags$bdi
tags$bdo
tags$em
tags$embed
tags$input
tags$ins
tags$pre
tags$progress
tags$textarea
tags$tfoot
object object object
3
flowLayout( # object 1,
1 2 # object 2,
(see front page) ui <- fluidPage( (see front page) tags$blockquote tags$eventsource tags$kbd tags$q tags$th
ui <- fluidPage( textInput("a","","A"), tags$body tags$fieldset tags$keygen tags$ruby tags$thead object # object 3
textInput("a","","A") reactiveValues() textOutput("b") tags$br tags$figcaption tags$label tags$rp tags$time 3 )
) Builds an object to
) tags$button tags$figure tags$legend tags$rt tags$title )
Each input function server <- display. Will rerun code in tags$canvas tags$footer tags$li tags$s tags$tr
sidebarLayout()
server <- function(input,output){ body to rebuild the object tags$caption tags$form tags$link tags$samp tags$track ui <- fluidPage(
creates a reactive value output$b <- tags$cite tags$h1 tags$mark tags$script tags$u sidebarLayout(
function(input,output){
stored as input$<inputId> renderText({ whenever a reactive value tags$code tags$h2 tags$map tags$section tags$ul
rv <- reactiveValues()
in the code changes. sidebarPanel(),
rv$number <- 5 input$a
})
tags$col tags$h3 tags$menu tags$select tags$var side main mainPanel()
} reactiveValues() creates a tags$colgroup tags$h4 tags$meta tags$small tags$video panel
list of reactive values
} Save the results to tags$command tags$h5 tags$meter tags$source tags$wbr panel )
)
whose values you can set. shinyApp(ui, server) output$<outputId>
The most common tags have wrapper functions. You
do not need to prefix their names with tags$ splitLayout()
Prevent reactions Trigger arbitrary code ui <- fluidPage( ui <- fluidPage(
h1("Header 1"), object object splitLayout( # object 1,
library(shiny) isolate(expr) library(shiny) observeEvent(eventExpr hr(), # object 2
ui <- fluidPage(
Runs a code block.
ui <- fluidPage(
textInput("a","","A"),
, handlerExpr, event.env, br(), 1 2 )
textInput("a","","A"), event.quoted, handler.env, p(strong("bold")), )
textOutput("b") Returns a non-reactive actionButton("go","Go")
handler.quoted, labe, p(em("italic")),
) )
copy of the results. suspended, priority, domain,
server <-
p(code("code")), verticalLayout() ui <- fluidPage(
server <- autoDestroy, ignoreNULL) a(href="", "link"), verticalLayout( # object 1,
function(input,output){ function(input,output){ HTML("<p>Raw html</p>") object 1 # object 2,
output$b <- observeEvent(input$go,{
)
renderText({ print(input$a) Runs code in 2nd object 2
# object 3
isolate({input$a}) })
} argument when reactive )
})
values in 1st argument object 3 )
} To include a CSS file, use includeCSS(), or
change. See observe() for
shinyApp(ui, server) shinyApp(ui, server) 1. Place the file in the www subdirectory
alternative.
2. Link to it with Layer tabPanels on top of each other,
Modularize reactions Delay reactions tags$head(tags$link(rel = "stylesheet", and navigate between them, with:
type = "text/css", href = "<file name>"))
library(shiny)
reactive(x, env, quoted, library(shiny) eventReactive(eventExpr, ui <- fluidPage( tabsetPanel(
ui <- fluidPage(
valueExpr, event.env, tabPanel("tab 1", "contents"),
textInput("a","","A"), label, domain) ui <- fluidPage(
textInput("a","","A"), tabPanel("tab 2", "contents"),
textInput("z","","Z"), Creates a reactive expression actionButton("go","Go"), event.quoted, value.env, To include JavaScript, use includeScript() or
textOutput("b") textOutput("b") tabPanel("tab 3", "contents")))
) that ) value.quoted, label, 1. Place the file in the www subdirectory
server <- caches its value to reduce server <-
domain, ignoreNULL) 2. Link to it with ui <- fluidPage( navlistPanel(
function(input,output){ computation function(input,output){ tabPanel("tab 1", "contents"),
re <- reactive({ re <- eventReactive( Creates reactive tags$head(tags$script(src = "<file name>")) tabPanel("tab 2", "contents"),
paste(input$a,input can be called by other code input$go,{input$a}) expression with code in
$z)}) output$b <- renderText({ tabPanel("tab 3", "contents")))
output$b <- renderText({ notifies its dependencies re() 2nd argument that only
re() when it ha been invalidated }) invalidates when reactive IMAGES To include an image ui <- navbarPage(title = "Page",
}) }
} Call the expression with values in 1st argument 1. Place the file in the www subdirectory tabPanel("tab 1", "contents"),
shinyApp(ui, server) shinyApp(ui, server) tabPanel("tab 2", "contents"),
function syntax, e.g. re() change. 2. Link to it with img(src="<file name>") tabPanel("tab 3", "contents"))

RStudio is a trademark of RStudio, Inc. CC BY RStudio info@rstudio.com 844-448-1212 rstudio.com More cheat sheets at http://www.rstudio.com/resources/cheatsheets/ Learn more at shiny.rstudio.com/tutorial shiny 0.12.0 Updated: 01/16

Potrebbero piacerti anche