Cheatsheet For R Programming
Cheatsheet For R Programming
Building an App
App template
Begin writing a new app with this template. Preview
the app by running the code at the R command line.
library(shiny)
ui <- fluidPage()
server <- function(input, output){}
shinyApp(ui = ui, server = server)
(>=0.99) or run:
rsconnect::deployApp("<path to directory>")
Build or purchase your own Shiny Server
"
!
at www.rstudio.com/products/shiny-server/
Save your template as app.R. Alternatively, split your template into two files named ui.R and server.R.
# ui.R
library(shiny)
ui <- fluidPage(
numericInput(inputId = "n",
"Sample size", value = 25),
plotOutput(outputId = "hist")
)
library(shiny)
Basics
- Complete the template by adding arguments to fluidPage() and a body to the server function.
ui <- fluidPage(
numericInput(inputId = "n",
"Sample size", value = 25),
plotOutput(outputId = "hist")
)
fluidPage(
numericInput(inputId = "n",
"Sample size", value = 25),
plotOutput(outputId = "hist")
)
# server.R
function(input, output) {
output$hist <- renderPlot({
hist(rnorm(input$n))
})
}
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.
app-name
.r
$
$
$
$
$
#
app.R
global.R
DESCRIPTION
README
<other files>
www
checkboxGroupInput(inputId, label,
choices, selected, inline)
checkboxInput(inputId, label, value)
dateInput(inputId, label, value, min,
max, format, startview, weekstart,
language)
dateRangeInput(inputId, label, start,
end, min, max, format, startview,
weekstart, language, separator)
fileInput(inputId, label, multiple,
accept)
numericInput(inputId, label, value,
min, max, step)
Outputs - render*() and *Output() functions work together to add R output to the UI
DT::renderDataTable(expr,
options, callback, escape,
env, quoted)
works
with
dataTableOutput(outputId, icon, )
verbatimTextOutput(outputId)
quoted, func)
width)
tableOutput(outputId)
submitButton(text, icon)
Reactivity
Reactive values work together with reactive functions. Call a reactive value from within the arguments of one of
these functions to avoid the error Operation not allowed without an active reactive context.
Trigger
arbitrary code
observeEvent()
observe()
Modularize
reactions
run(this)
input$x
shinyApp(ui, server)
*Input() functions
reactiveValues()
Each input function
creates a reactive value
stored as input$<inputId>
reactiveValues() creates a
list of reactive values
whose values you can set.
Prevent reactions
library(shiny)
ui <- fluidPage(
textInput("a",""),
textOutput("b")
)
render*() functions
library(shiny)
ui <- fluidPage(
textInput("a","")
)
server <function(input,output){
output$b <renderText({
input$a
})
}
shinyApp(ui, server)
library(shiny)
ui <- fluidPage(
textInput("a",""),
actionButton("go", "")
)
shinyApp(ui, server)
shinyApp(ui, server)
Modularize reactions
server <function(input,output){
re <- reactive({
paste(input$a,input$b})
output$b <- renderText({
re()
})
}
shinyApp(ui, server)
Builds an object to
display. Will rerun code in
body to rebuild the object
whenever a reactive value
in the code changes.
Save the results to
output$<outputId>
observeEvent(eventExpr
isolate(expr)
server <function(input,output){
observeEvent(input$go,
print(input$a)
})
}
ui <- fluidPage(
textInput("a",""),
textInput("z", "")
)
server <function(input,output){
output$b <renderText({
isolate({input$a})
})
}
library(shiny)
render*()
eventReactive()
server <function(input,output){
rv <- reactiveValues()
rv$number <- 5
}
Render
reactive output
Delay reactions
Layouts
fluidPage(
Returns
textInput("a","")
HTML
)
## <div class="container-fluid">
## <div class="form-group shiny-input-container">
##
<label for="a"></label>
##
<input id="a" type="text"
##
class="form-control" value=""/>
##
</div>
## </div>
output$y
Update
reactiveValues()
*Input()
ui <- fluidPage(
textInput("a","")
)
isolate()
expression()
library(shiny)
Prevent reactions
reactive()
UI
, handlerExpr, event.env,
event.quoted, handler.env,
handler.quoted, labe,
suspended, priority, domain,
autoDestroy, ignoreNULL)
Delay reactions
library(shiny)
ui <- fluidPage(
textInput("a",""),
actionButton("go", "")
)
server <function(input,output){
re <- eventReactive(
input$go,{input$a})
output$b <- renderText({
re()
})
}
shinyApp(ui, server)
eventReactive(eventExpr,
valueExpr, event.env,
event.quoted, value.env,
value.quoted, label,
domain, ignoreNULL)
Creates reactive
expression with code in
2nd argument that only
invalidates when reactive
values in 1st argument
change.
wellPanel(
dateInput("a", ""),
submitButton()
)
absolutePanel()
conditionalPanel()
fixedPanel()
headerPanel()
tags$data
tags$datalist
tags$dd
tags$del
tags$details
tags$dfn
tags$div
tags$dl
tags$dt
tags$em
tags$embed
tags$h6
tags$head
tags$header
tags$hgroup
tags$hr
tags$HTML
tags$i
tags$iframe
tags$img
tags$input
tags$ins
tags$blockquote tags$eventsource tags$kbd
tags$keygen
tags$body
tags$fieldset
tags$br
tags$figcaption tags$label
tags$legend
tags$button
tags$figure
tags$li
tags$canvas
tags$footer
tags$link
tags$caption tags$form
tags$mark
tags$cite
tags$h1
tags$map
tags$code
tags$h2
tags$menu
tags$col
tags$h3
tags$meta
tags$colgroup tags$h4
tags$meter
tags$command tags$h5
tags$nav
tags$noscript
tags$object
tags$ol
tags$span
tags$strong
tags$style
tags$sub
tags$option
tags$output
tags$p
tags$param
tags$pre
tags$progress
tags$q
tags$ruby
tags$rp
tags$rt
tags$s
tags$samp
tags$script
tags$section
tags$select
tags$small
tags$source
tags$sup
tags$table
tags$tbody
tags$td
tags$textarea
tags$tfoot
tags$th
tags$thead
tags$time
tags$title
tags$tr
tags$track
tags$u
tags$ul
tags$var
tags$video
tags$wbr
fluidRow()
column
row
col
column
tags$optgroup tags$summary
flowLayout()
object object object
3
1
2
object
3
sidebarLayout()
side
panel
main
panel
ui <- fluidPage(
fluidRow(column(width = 4),
column(width = 2, oset = 3)),
fluidRow(column(width = 12))
)
ui <- fluidPage(
flowLayout( # object 1,
# object 2,
# object 3
)
)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(),
mainPanel()
)
)
splitLayout()
ui <- fluidPage(
h1("Header 1"),
hr(),
br(),
p(strong("bold")),
p(em("italic")),
p(code("code")),
a(href="", "link"),
HTML("<p>Raw html</p>")
)
object object
1
2
verticalLayout()
object 1
object 2
object 3
ui <- fluidPage(
splitLayout( # object 1,
# object 2
)
)
ui <- fluidPage(
verticalLayout( # object 1,
# object 2,
# object 3
)
)
tags$head(tags$link(rel = "stylesheet",
type = "text/css", href = "<file name>"))
tabPanel()
tabsetPanel()
titlePanel()
wellPanel()
inputPanel()
mainPanel()
navlistPanel()
sidebarPanel()