【问题标题】:Pass reactive or static value to the same shiny module将反应或静态值传递给同一个闪亮的模块
【发布时间】:2020-09-28 05:20:13
【问题描述】:

我的应用程序中有两次调用相同的模块。在其中一个调用中,我有一个“静态”data.frame,而在第二个调用中,我想传递一个反应值。

是否可以将反应和静态值传递给同一个闪亮模块? 如果这不可能,最好开发同一模块的 2 个版本或将其他数据框也转换为反应式?

#--- 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() )

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    您可以以预期反应值的方式编写模块

    component_srv <- function(input, output, session, reactive_parameter) {
      observeEvent(reactive_parameter(), {
        # ...
      }
      # ...
    }
    

    然后您可以选择传递一个静态值,只需将reactive() 包裹起来。

    static_object <- iris
    callModule(component_srv, "instance", 
               reactive_parameter = reactive(static_object))
    
    reactive_object = reactive({
      get_data(input$dataset)
    })
    callModule(component_srv, "instance2",
               reactive_parameter = reactive_object)
    

    请注意,您需要在callModule() 中使用reactive_object 而不是reactive_object() 才能完成这项工作。

    【讨论】:

    • 很好,所以基本上您的建议是将所有模块实例转换为反应值。我认为这是合理的,这或多或少是我也提出的解决方案:) ...在性能方面有什么缺点,我需要担心吗?
    • 我不知道在静态对象周围使用 reactive() 时的性能影响。当然会有一些开销,但我建议您进行一些基准测试以确定差异是否显着。另请注意,reactive() 会创建一个缓存,因此监控 RAM 使用情况也是一个好主意。
    猜你喜欢
    • 2014-01-23
    • 1970-01-01
    • 1970-01-01
    • 2014-02-23
    • 2019-09-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-08
    相关资源
    最近更新 更多