模块外部变量没有更新 - r shiny

module outer variables aren't updating - r shiny

在闪亮的主服务器中,我有一个 reactivePoll 变量。我正在尝试与闪亮的模块共享这个更新变量。它显示初始值但不更新。我正在尝试让它更新。

这是一个代表:

library(shiny)

# Module ----

# module ui
mod_ui <- function(id){
  ns = NS(id)
  
  verbatimTextOutput(ns("random_num"))
}

# module server
mod <- function(id, number) {
  server <- function(input, output, session) {
    
    output$random_num <- renderPrint(number)
  }
  moduleServer(id, server)
}


# Shiny App ----

ui <- fluidPage(
  tags$span("working random_num"),
  verbatimTextOutput("test"),
  tags$span("not working, module random num"),
  mod_ui("mod_id")
)

server <- function(input, output, session) {
  
  random_num <- reactivePoll(
    intervalMillis = 1000,
    session = NULL,
    checkFunc = function()runif(1),
    valueFunc = function()runif(1)
  )
  
  output$test <- renderPrint(random_num())
  
  mod("mod_id", number = random_num())
}

shinyApp(ui = ui, server = server)

非常感谢任何帮助!

想出了解决办法。将 reactivePoll 变量保存到 reactiveValue 中。观察反应值。很好奇为什么会这样。


library(shiny)

# Module ----

# module ui
mod_ui <- function(id){
  ns = NS(id)
  
  verbatimTextOutput(ns("random_num"))
}

# module server
mod <- function(id, number) {
  server <- function(input, output, session) {
    
    output$random_num <- renderPrint(number$test)
  }
  moduleServer(id, server)
}


# Shiny App ----

ui <- fluidPage(
  tags$span("working random_num"),
  verbatimTextOutput("test"),
  tags$span("not working, module random num"),
  mod_ui("mod_id")
)

server <- function(input, output, session) {
  
  test <- reactiveValues(
    test = runif(1)
  )
  
  random_num <- reactivePoll(
    intervalMillis = 1000,
    session = NULL,
    checkFunc = function()runif(1),
    valueFunc = function()runif(1)
  )
  
  observe(
    test$test <- random_num()
  )
  
  output$test <- renderPrint(random_num())
  
  mod("mod_id", number = test)
}

shinyApp(ui = ui, server = server)