Session - Adding dynamic elements
The real magic of Shiny happens when you have an app with inputs and outputs
textInput()
is providing the information for the server input$name
output$greeting
in the server that updates the textOutput("greeting")
in the uiUnlike with R where updates like this occur when we run the code, here Shiny is being told how it could create the string if it needs to. The possibilities are:
Tip!
Think of a Shiny app as providing recipes and not giving commands!
Four inputs were mentioned previously:
sliderInput()
selectInput()
textInput()
numericInput()
And there are more! We are going to use checkboxGroupInput()
in a new app.
Starting again with a new script, typing shinyapp
and then Shift + Tab add the code to ui
, save and Run App
Next we’ll add an action button so, after making a selection, the user presses a button for the next thing to happen:
Don’t forget the comma!
checkboxGroupInput(...)
, actionButton(...)
Before we move to the interactive part of shiny we’ve currently got the layout where everything is appearing on the page as if it were a document.
To give the app more structure that are more familiar we need to introduce three functions:
ui <- fluidPage(titlePanel("Basic Demo"),
sidebarLayout(
sidebarPanel(
checkboxGroupInput(
inputId = "name1",
label = "What are your favourite languages?",
choices = c("SQL", "Python", "R")
),
actionButton(
inputId = "name2",
label = "Selected",
icon = icon("question")
)
),
mainPanel(textOutput(outputId = "name3"))
)
)
sidebarLayout()
, sidebarPanel()
and mainPanel()
are needed to run the appCtrl+i
for indentationoutput$
and input$
in the servermainPanel()
the function textOutput()
is used and that has a partnering function: renderText()
in the serverCopy and paste this, save and run
Replace the names name1
, name2
, name3
with more descriptive names, paying attention to where the repetitions occur
10:00
observeEvent()
bindEvent()
bindEvent()
can be used to execute only when a button is pressed. We’ll replace the code
with
Remember your code will be different now
name1
and so on to something more readable!Mastering Shiny explains this really well:
Although it’s a strength of Shiny that declarative programming is lazy it does mean that code can have bugs but no errors.
If you recall when we changed names1
to other names there was no error if these did not appear in both the ui and server.
This applies to functions so if don’t pair them correctly:
The familiar analysis scripts are run in the order of lines.
Quarto reports for example, need to particularly have everything in the right order to run.
This isn’t the case for Shiny!
Given this UI:
Fix the simple errors found in each of the three server functions below. First try spotting the problem just by reading the code; then run the code to make sure you’ve fixed it.
server1 <- function(input, output, server) {
input$greeting <- renderText(paste0("Hello ", name))
}
server2 <- function(input, output, server) {
greeting <- paste0("Hello ", input$name)
output$greeting <- renderText(greeting)
}
server3 <- function(input, output, server) {
output$greting <- paste0("Hello", input$name)
}
08:00
server1 <- function(input, output, session) {
output$greeting <- renderText(paste0("Hello ", input$name))
}
server2 <- function(input, output, server) {
greeting <- reactive(paste0("Hello ", input$name))
output$greeting <- renderText(greeting())
}
server3 <- function(input, output, session) {
output$greeting <- renderText({
paste0("Hello ", input$name)
})
}
Mastering Shiny by Hadley Wickham
Building Web Apps with R Shiny from PsyTeachR