library(shiny)ui <- fluidPage()server <- function(input, output) {}shinyApp(ui = ui, server = server)
Important: Do not place any code after shinyApp()
Save file as app.R → "Run" button turns to "Run App"
Good for creating Shiny apps quickly, all code in one file
Save UI as ui.R and server as server.R in same directory
Good for complex Shiny apps, separates view vs logic
If using this method, do not include a call to shinyApp(...)
File > New File > Shiny Web App...
Generates the template for you
Press "Esc" or click the Stop icon
usethis::use_course("cis-ds/shiny-demo")
fluidPage()
library(shiny)ui <- fluidPage("Hello CFSS")server <- function(input, output) {}shinyApp(ui = ui, server = server)
fluidPage()
fluidPage( h1("My Shiny app"), "Hello CFSS")
fluidPage()
h1()
= header1br()
= line breakstrong()
= bold texttags
objecth1
= tags$h1()
, br
= tags$br()
tags
fluidPage()
fluidPage( h1("My Shiny app"), h3("Subtitle"), "Hello", "CFSS", br(), strong("bold text"))
sidebarLayout()
sidebarLayout()
fluidPage( titlePanel("My Shiny app"), sidebarLayout( sidebarPanel( "This is a side panel" ), mainPanel( "And this is the main stuff" ) ))
sidebarLayout()
library(shiny)ui <- fluidPage( sliderInput( "num", "Choose a number", min = 0, max = 100, value = 20))server <- function(input, output) {}shinyApp(ui = ui, server = server)
sliderInput("num", "Choose a number", min = 0, max = 100, value = 20)
## <div class="form-group shiny-input-container">## <label class="control-label" id="num-label" for="num">Choose a number</label>## <input class="js-range-slider" id="num" data-skin="shiny" data-min="0" data-max="100" data-from="20" data-step="1" data-grid="true" data-grid-num="10" data-grid-snap="false" data-prettify-separator="," data-prettify-enabled="true" data-keyboard="true" data-data-type="number"/>## </div>
sliderInput("num", "Choose a number", min = 0, max = 100, value = 20)
Function | Outputs |
---|---|
plotOutput() |
plot |
tableOutput() |
table |
uiOutput() |
Shiny UI element |
textOutput() |
text |
library(shiny)ui <- fluidPage( sliderInput("num", "Choose a number", 0, 100, 20), plotOutput("myplot"))server <- function(input, output) {}shinyApp(ui = ui, server = server)
server <- function(input, output) { output$myplot <- renderPlot({ plot(rnorm(input$num)) })}
output$
render*()
Output()
→ render*()
Output function | Render function |
---|---|
plotOutput() |
renderPlot({}) |
tableOutput() |
renderTable({}) |
uiOutput() |
renderUI({}) |
textOutput() |
renderText({}) |
render*()
functionsrenderPlot({ plot(rnorm(100))})
server <- function(input, output) { output$myplot <- renderPlot({ plot(rnorm(input$num)) # in UI:sliderInput("num", ...) })}
output$
render*()
input$
x
changes, anything that relies on x
is re-evaluatedx <- 5y <- x + 1x <- 10
input$num
is a reactive valueoutput$myplot <- renderPlot({ plot(rnorm(input$num))})
output$myplot
depends on input$num
input$num
changes → output$myplot
reactsrender*
function, the output will re-render any time input changesrender*
function is a reactive contextreactive({...})
to assign a reactive variableobserve({...})
to access a reactive variableserver <- function(input, output) { x <- input$num + 1}# error
server <- function(input, output) { x <- reactive({ input$num + 1 })}# OK
server <- function(input, output) { print(input$num)}# error
server <- function(input, output) { observe({ print(input$num) })}# OK
library(shiny)ui <- fluidPage( sliderInput("num", "Choose a number", 0, 100, 20), plotOutput("myplot"))server <- function(input, output) { output$myplot <- renderPlot({ plot(seq(input$num)) }) x <- reactive({ input$num + 1 }) observe({ print(x()) })}shinyApp(ui = ui, server = server)
employ_filter
that creates the filtered data frameemploy_filter
to create the histogram and tableuiOutput()
library(shiny)ui <- fluidPage( numericInput("num", "Maximum slider value", 5), uiOutput("slider"))server <- function(input, output) { output$slider <- renderUI({ sliderInput("slider", "Slider", min = 0, max = input$num, value = 0) })}shinyApp(ui = ui, server = server)
uiOutput()
in our app to populate the job titles inputKeyboard shortcuts
↑, ←, Pg Up, k | Go to previous slide |
↓, →, Pg Dn, Space, j | Go to next slide |
Home | Go to first slide |
End | Go to last slide |
Number + Return | Go to specific slide |
b / m / f | Toggle blackout / mirrored / fullscreen mode |
c | Clone slideshow |
p | Toggle presenter mode |
t | Restart the presentation timer |
?, h | Toggle this help |
Esc | Back to slideshow |