R 是否可以从模块返回反应式表达式?
我尝试从一个模块返回一些反应式表达式,并传递到另一个模块。我知道传递输入时,这样的事情很容易,例如:R 是否可以从模块返回反应式表达式?,r,shiny,reactive-programming,reactive,R,Shiny,Reactive Programming,Reactive,我尝试从一个模块返回一些反应式表达式,并传递到另一个模块。我知道传递输入时,这样的事情很容易,例如: return( list( btn1 = reactive({input$buttonX}), btn2 = reactive({input$buttonY})) ) 但是,我不能以这种方式返回和传递被动表达式,例如: react1 <- reactiveVal() react2 <- reactiveValues(state = TRUE
return(
list(
btn1 = reactive({input$buttonX}),
btn2 = reactive({input$buttonY}))
)
但是,我不能以这种方式返回和传递被动表达式,例如:
react1 <- reactiveVal()
react2 <- reactiveValues(state = TRUE)
return(
list(
x = react1,
y = react2
)
)
如果你说的是这些,我看不到任何与这些相关的代码。你们能解释一下模块是如何使用的吗?当然,我在我的帖子中添加了一个例子。一般问题是如何将现有的反应式从一个模块传递到另一个模块。我尝试了许多解决办法,但都没有成功。
library(shiny)
library(dplyr)
moduleServer <- function(id, module) {
callModule(module, id)
}
# UI 1 #
mod_1st_Nested_UI <- function(id) {
ns <- NS(id)
}
# Server 1 #
mod_1st_Nested_server <- function(id){
moduleServer(id, function(input, output, session) {
# here I have various reactives but want to pass only some of them to parent
btn <- reactive({input$btn})
info <- reactiveValues(logic = TRUE)
other <- reactiveVal()
other("XYZ")
return(list(yyy = info))
})
}
# Parent UI #
mod_Parent_UI <- function(id) {
ns <- NS(id)
tagList(
mod_1st_Nested_UI(ns('first')),
mod_2nd_Nested_UI(ns('second'))
)
}
# Parent Server #
mod_Parent_server <- function(id){
moduleServer(id, function(input, output, session) {
ns <- NS(id)
returnReactive <- mod_1st_Nested_server("first")
mod_2nd_Nested_server('second', returnReactive$yyy) # here I'm passing reactive from module_1st
})
}
# UI 2 #
mod_2nd_Nested_UI <- function(id) {
ns <- NS(id)
tagList(
textOutput(ns("text"))
)
}
# Server 2 #
mod_2nd_Nested_server <- function(id, value){
moduleServer(id, function(input, output, session) {
ns <- NS(id)
output$text <- renderText({ # reactive value from 1st module should be printed here
print(value)
})
})
}
# FINAL App #
ui <- fluidPage(
tagList(
mod_Parent_UI("final")
))
server <- function(input, output, session) {
mod_Parent_server("final")
}
shinyApp(ui = ui, server = server)