Consider this Shiny app code snippet. What will be displayed in the app's main panel when the slider is moved to 5?
library(shiny) ui <- fluidPage( sliderInput("num", "Choose a number", min = 1, max = 10, value = 1), textOutput("result") ) server <- function(input, output) { output$result <- renderText({ paste("You selected", input$num * 2) }) } shinyApp(ui, server)
Remember the slider value is multiplied by 2 before displaying.
The slider input value is 5, and the server multiplies it by 2, so the text output shows "You selected 10".
In Shiny, which function is used to create a reactive expression that updates automatically when its inputs change?
Think about a function that returns a value and updates reactively.
The reactive() function creates reactive expressions that update automatically when their inputs change. observeEvent() is for side effects, renderUI() creates UI outputs, and isolate() prevents reactive updates.
Look at this server code snippet. The plot does not update when the slider input changes. What is the cause?
server <- function(input, output) { output$plot <- renderPlot({ x <- 1:10 y <- x * 2 plot(x, y) }) }
Check if the reactive input is used inside renderPlot.
The plot code does not use input$num or any reactive input, so Shiny does not know to update the plot when the slider changes.
Which of these UI code snippets will cause a syntax error?
Look for misplaced commas inside function calls.
Option A has an extra comma after the sliderInput call inside sidebarPanel, causing a syntax error.
Given this server code, how many items does vals contain after the app starts?
server <- function(input, output, session) { vals <- reactiveValues() vals$a <- 10 vals$b <- 20 observe({ vals$c <- vals$a + vals$b }) }
Consider when the observe block runs and what it adds to vals.
Initially, vals has 'a' and 'b'. The observe block runs immediately and adds 'c', so vals has 3 items.