I now think that it makes a difference. If your module function outputs do not depend on any reactives they will never get updated (only once when the app starts).
I have made a reprex because I was curious and felt like trying this out again (it has been a while...).
We have three instances of a sub component displaying a tbl.
one
The module server function get's the static tbl diamonds
two
Here, the module server function get's the evaluated reactive r_filtered_data()
which is static in the very moment the module is called. At startup, this happens but at that point in time, no columns have been selected. That's why it is empty.
The crucial point is, that the output in two is not dependent on any reactive!
three
This is a module taking a reactive expression as argument. And here, the output$text_output
depends on another reactive expression, r_filtered_data
, which itself depends on the input selections (and the static diamonds
data).
You always have to think of a (directed) graph of dependencies from input to output. Your output will only change if it depends on anything (which is eventually an input).
library(shiny)
library(tidyverse)
sub_component_UI <- function(id) {
ns <- NS(id)
tagList(
h3(id),
verbatimTextOutput(outputId = ns("text_output"))
)
}
sub_component_static_data_server <- function(input, output, session, data) {
output$text_output <- renderPrint(data)
}
sub_component_r_data_server <- function(input, output, session, r_data) {
# This will cause the output to be re-rendered if r_data changes
output$text_output <- renderPrint(r_data())
}
main_UI <- function() {
fluidPage(
tagList(
# static choices here (for simplicity)
selectInput("column_names", label = "Select columns",
choices = names(diamonds),
multiple = TRUE),
sub_component_UI("one"),
sub_component_UI("two"),
sub_component_UI("three")
)
)
}
main_server <- function(input, output, session) {
r_filtered_data <- reactive({
diamonds %>%
# we can do that because select accepts string vectors
select(input$column_names)
})
callModule(sub_component_static_data_server, id = "one", data = diamonds)
callModule(sub_component_static_data_server, id = "two", data = r_filtered_data())
# we hand over a reactive here
callModule(sub_component_r_data_server, id = "three", r_data = r_filtered_data)
}
shinyApp(ui = main_UI, server = main_server)
Created on 2020-09-25 by the reprex package (v0.3.0)TV