Pass reactive or static value to the same shiny module

've got the same module called 2 time within my application. In one of the call I've a "static" data.frame while in the second call I would like to pass a reactive value.

Is it possible to pass reactive and static value to the same shiny module? If this is not possible should be better to develop 2 version of the same module or convert to reactive also the other data frame?

#--- Reactive Values  ----
reactive_selected_account  <- reactive({ input$budget_slider })
reactive_monthly_total     <- reactive({
    monthly_total %>%
        filter( account==reactive_selected_account() ) %>%
        group_by(dt) %>%
        select(-account) %>%
        function_do_monthly_total()
})

#--- Components Calls ----
callModule(component_srv, "istance1", data=monthly_total)
callModule(component_srv, "istance1", data=reactive_monthly_total() )

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 :slight_smile: 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

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.