Let's say I have the following UI:
ui <- fluidPage(
checkboxGroupInput("checkbox", "", choices = colnames(mtcars)),
tableOutput("table")
)
I want to render a table of mtcars
once at least one checkbox option has been selected. For this, I came across req()
, but I can't see how is it different from an if
statement, even reading the documentation about this function, it's definition is very close to what an if
statement does:
Ensure that values are available ("truthy"–see Details) before proceeding with a calculation or action. If any of the given values is not truthy, the operation is stopped by raising a "silent" exception (not logged by Shiny, nor displayed in the Shiny app's UI).
So, how is this table rendering:
server <- function(input, output) {
output$table <- renderTable({
req(input$checkbox)
mtcars %>% select(input$checkbox)
})
}
shinyApp(ui, server)
different from this one?:
server <- function(input, output) {
output$table <- renderTable({
if(!is.null(input$checkbox))
mtcars %>% select(input$checkbox)
})
}
shinyApp(ui, server)
TL;DR: how is req()
different from an if
statement other than how you write it?