MACS 30500
University of Chicago
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 \(\rightarrow\) “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
Work through the tutorial until the end of Section 4
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
object
h1
= 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()
Work through the tutorial until the end of Section 5
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)
print(sliderInput("num", "Choose a number",
min = 0, max = 100, value = 20))
## <div class="form-group shiny-input-container">
## <label class="control-label" for="num">Choose a number</label>
## <input class="js-range-slider" id="num" data-min="0" data-max="100" data-from="20" data-step="1" data-grid="true" data-grid-num="10" data-grid-snap="false" data-keyboard="true" data-keyboard-step="1" data-drag-interval="true" data-data-type="number" data-prettify-separator=","/>
## </div>
sliderInput("num",
"Choose a number",
min = 0,
max = 0,
value = 20)
Work through the tutorial until the end of Section 6
Function | Outputs |
---|---|
plotOutput() |
plot |
tableOutput() |
table |
uiOutput() |
Shiny UI element |
textOutput() |
text |
sliderInput("num",
"Choose a number",
min = 0,
max = 0,
value = 20)
library(shiny)
ui <- fluidPage(
sliderInput("num", "Choose a number",
0, 100, 20),
plotOutput("myplot")
)
server <- function(input, output) {}
shinyApp(ui = ui, server = server)
fluidPage()
*Input()
functions*Output()
functionsserver
to assemble inputs into outputsRemember to:
Work through the tutorial until the end of Section 8
server <- function(input, output) {
output$myplot <- renderPlot({
plot(rnorm(input$num))
})
}
output$
render*()
Output()
\(\rightarrow\) render*()
Output function | Render function |
---|---|
plotOutput() |
renderPlot({}) |
tableOutput() |
renderTable({}) |
uiOutput() |
renderUI({}) |
textOutput() |
renderText({}) |
render*()
functions build reactive output to display in UIrenderPlot({
plot(rnorm(100))
})
server <- function(input, output) {
output$myplot <- renderPlot({
plot(rnorm(input$num))
# in UI:sliderInput("num", ...)
})
}
output$
render*()
input$
Work through the tutorial until the end of Section 9
x
changes, anything that relies on x
is re-evaluatedContrast with regular R:
x <- 5
y <- x + 1
x <- 10
# y is still 6
input$num
is a reactive value
output$myplot <- renderPlot({
plot(rnorm(input$num))
})
output$myplot
depends on input$num
input$num
changes \(\rightarrow\) output$myplot
reactsAll inputs are automatically reactive, so if you use any input inside a render*
function, the output will re-render any time input changes
render*
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)
Work through the tutorial until the end of Section 10
Different from other inputs: you usually don’t care about the “value” of the button, you care when it’s clicked
ui <- fluidPage(
actionButton("btn", "Click me")
)
server <- function(input, output, session) {
observe({
cat(input$btn)
})
}
shinyApp(ui = ui, server = server)
Work through the tutorial until the end of Section 12
Work through the tutorial until the end of Section 13