'In R Shiny, if observers cannot return anything, how are render functions able to generate outputs?
I'm trying to wrap my head around the concept of observers. In section 15.3 of the book Mastering Shiny, it says that the value returned by an observer is ignored because they are designed to work with functions called for their side-effects, like cat() or write.csv(). This is also what the observe() function documentation says.
However, Mastering Shiny also says that outputs are a special type of observers. If this is the case, how are outputs able to generate things like plots and tables? For example, in the server, we can use renderPlot() function to store an expression that will return some sort of plot:
ui <- fluidPage(
selectInput("var", "Variable", choices = names(mtcars)),
plotOutput("hist")
)
server <- function(input, output) {
output$hist <- renderPlot({
ggplot(data=mtcars, aes(x = !!sym(input$var))) +
geom_histogram()
})
}
shinyApp(ui = ui, server = server)
Internally, I believe Shiny will create a new observer and pass along the expression inside my renderPlot() function to this observer.
If observers cannot return anything, how is the plot output being generated and displayed on our app? Is there something else going on behind the scenes?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
