2 Lesson 1

How to build an RShiny app!

2.1 My first app

#L01_Script01_MyFirstApp
library(shiny)
ui <- fluidPage(
  "Hello, world!"
)
server <- function(input, output, session) {
}
shinyApp(ui, server)

2.2 Input example

# L01_Script02_InputExample
library(shiny)
ui <- fluidPage(
  sliderInput(inputId = "num",
              label = "Choose a number",
              value = 25, min = 1, max = 100),
  plotOutput("hist")
)
server <- function(input, output, session) {
  output$hist <- renderPlot({
    hist(rnorm(100))
  })
}
shinyApp(ui, server)

2.3 Practice Problem #1

2.3.1 Question

# L01_Script03_PPQuestion01
tableOutput("mortgage")

output$greeting <- renderText({
  paste0("Hello ", input$name)
})

numericInput("age", "How old are you?", value = NA)

textInput("name", "What's your name?")

textOutput("greeting")

output$histogram <- renderPlot({
  hist(rnorm(1000))}, res = 96)

2.3.2 Solution

# L01_Script04_PPSolution01

library(shiny)
ui <- fluidPage(
  textInput("name", "What's your name?"),
  textOutput("greeting")
)
server <- function(input, output, session) {
  output$greeting <- renderText({
    paste0("Hello ", input$name)
  })
}
shinyApp(ui, server)

2.4 Practice Problem #2

2.4.1 Question

# L01_Script05_PPQuestion02
library(shiny)

ui <- fluidPage(
  sliderInput("x", label = "If x is", min = 1, max = 50, value = 30),
  sliderInput("y", label = "If y is", min = 1, max = 25, value = 8),
  "then x times y is",
  textOutput("product")
)

server <- function(input, output, session) {
  output$product <- renderText({ 
    x * y
  })
}

shinyApp(ui, server)

2.4.2 Solution

# L01_Script06_PPSolution02
library(shiny)

ui <- fluidPage(
  sliderInput("x", label = "If x is", min = 1, max = 50, value = 30),
  sliderInput("y", label = "If y is", min = 1, max = 25, value = 8),
  "then x times y is",
  textOutput("product")
)
server <- function(input, output, session) {
  output$product <- renderText({ 
    input$x * input$y
  })
}
shinyApp(ui, server)