2 Lesson 1
How to build an RShiny app!
2.1 My first app
#L01_Script01_MyFirstApp
library(shiny)
<- fluidPage(
ui "Hello, world!"
)<- function(input, output, session) {
server
}shinyApp(ui, server)
2.2 Input example
# L01_Script02_InputExample
library(shiny)
<- fluidPage(
ui sliderInput(inputId = "num",
label = "Choose a number",
value = 25, min = 1, max = 100),
plotOutput("hist")
)<- function(input, output, session) {
server $hist <- renderPlot({
outputhist(rnorm(100))
})
}shinyApp(ui, server)
2.3 Practice Problem #1
2.3.1 Question
# L01_Script03_PPQuestion01
tableOutput("mortgage")
$greeting <- renderText({
outputpaste0("Hello ", input$name)
})
numericInput("age", "How old are you?", value = NA)
textInput("name", "What's your name?")
textOutput("greeting")
$histogram <- renderPlot({
outputhist(rnorm(1000))}, res = 96)
2.3.2 Solution
# L01_Script04_PPSolution01
library(shiny)
<- fluidPage(
ui textInput("name", "What's your name?"),
textOutput("greeting")
)<- function(input, output, session) {
server $greeting <- renderText({
outputpaste0("Hello ", input$name)
})
}shinyApp(ui, server)
2.4 Practice Problem #2
2.4.1 Question
# L01_Script05_PPQuestion02
library(shiny)
<- fluidPage(
ui 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")
)
<- function(input, output, session) {
server $product <- renderText({
output* y
x
})
}
shinyApp(ui, server)
2.4.2 Solution
# L01_Script06_PPSolution02
library(shiny)
<- fluidPage(
ui 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")
)<- function(input, output, session) {
server $product <- renderText({
output$x * input$y
input
})
}shinyApp(ui, server)