Session - First Shiny App
app.R
shinyapp
in the R file and the use Shift + Tab"Hello, world!"
between the brackets after the function fluidPage
app.r
but it is possible to use (or see) two files being used for ui
and server
.{golem}
Replace the text after the function fluidPage()
with:
Save and Run
fluidPage()
sets up the visual structure of the pageselectInput()
lets the user interact with the app by providing a valueverbatimTextOutput()
and tableOutput()
are output controls that tell Shiny where to put rendered outputWhat’s in the functions?
shiny::fluidPage()
We have the code saying what’s possible to select (UI) but not what is going to appear (Server)
Take the following code fix the error
How to create a new app
10:00
No matter what you code in, duplication will eventually cause more work:
In R code we avoid duplication using variables or functions but these don’t work or aren’t efficient in Shiny apps
Uses a function and new code structure with round and curly brackets
We can update the code in the shiny app by adding into the server object
Error: object of type 'closure' is not subsettable
Error: cannot coerce class ‘c("reactiveExpr", "reactive", "function")’ to a data.frame
Although data
is referred to it needs to be the reactive form so replace dataset
with dataset()
And try running again…
Let’s make the following code reactive and remove the repetition
library(shiny)
ui <- fluidPage(
sliderInput("x", "If x is", min = 1, max = 50, value = 30),
sliderInput("y", "and y is", min = 1, max = 50, value = 5),
"then, (x * y) is", textOutput("product"),
"and, (x * y) + 5 is", textOutput("product_plus5"),
"and (x * y) + 10 is", textOutput("product_plus10")
)
server <- function(input, output, session) {
output$product <- renderText({
product <- input$x * input$y
product
})
output$product_plus5 <- renderText({
product <- input$x * input$y
product + 5
})
output$product_plus10 <- renderText({
product <- input$x * input$y
product + 10
})
}
shinyApp(ui, server)
10:00
library(shiny)
ui <- fluidPage(
sliderInput("x", "If x is", min = 1, max = 50, value = 30),
sliderInput("y", "and y is", min = 1, max = 50, value = 5),
"then, (x * y) is", textOutput("product"),
"and, (x * y) + 5 is", textOutput("product_plus5"),
"and (x * y) + 10 is", textOutput("product_plus10")
)
server <- function(input, output, session) {
product <- reactive({input$x * input$y})
output$product <- renderText({
product()
})
output$product_plus5 <- renderText({
product() + 5
})
output$product_plus10 <- renderText({
product() + 10
})
}
shinyApp(ui, server)
Cheat sheets are a great way of seeing the possible functions in a package like {shiny}
But are also built into RStudio in Help > Cheat Sheets > Web applications with Shiny
Where to find Cheat sheets
Take the following code and move the lines you think you’ll need to the right places in a shiny app
10:00
Mastering Shiny by Hadley Wickham
Beginners course by Chris Beeley aimed at relatively novice R users
Shiny workshop for NHS-R Community
Shiny workshop materials on the NHS-R Community GitHub.