如何在反应值中存储Shiny模块的返回值?

Cec*_*Lee 6 r shiny

下面的版本1是一个玩具模块,它要求用户输入txt,并将输入返回到主Shiny应用程序.然后,主Shiny应用程序渲染文本并将其输出到屏幕.

在这里,我将模块的返回值存储在一个名为的变量中,mytxt并将其调用renderText({ mytxt() }).


但是,我真正想要做的是将返回的值存储在主Shiny应用程序中的reactiveValues中.(如果我输出它是否无关紧要,因为我想对该值进行进一步的评估.)但遗憾的是我发现没有办法让它起作用.我在下面的第2版​​中显示我的失败代码.


版本1(正确)

app.R

library(shiny)
source("module_1.R")

ui <- fluidPage(

  returnUI("returntxt"),
  textOutput("mytxt")

)

server <- function(input, output, session) {

  mytxt <- callModule(returnServer, "returntxt")

  output$mytxt <- renderText({ mytxt() })

}

shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)

module_1.R

returnUI = function(id) {
  ns <- NS(id)

  tagList(
    textInput(ns("txt"), "Write something")
  )
}


returnServer = function(input, output, session) {
  mytxt <- reactive({
    input$txt
  })

  return(mytxt)
}
Run Code Online (Sandbox Code Playgroud)

版本2(需要帮助!)

app.R

library(shiny)
source("modules/module_1.R")

ui <- fluidPage(

  returnUI("returntxt"),
  textOutput("mytxt")

)

server <- function(input, output, session) {

  myvals <- reactiveValues(
    txt = NULL
  )

  mytxt <- callModule(returnServer, "returntxt")

  myvals$txt <- isolate(mytxt())

  output$mytxt <- renderText({ myvals$txt })

}

shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)

module.R与版本1相同.

Cec*_*Lee 9

我刚刚reactiveValues从模块返回并找到答案并使用observe:)哇哦!

app.R

library(shiny)
source("modules/module_1.R")

ui <- fluidPage(

  returnUI("returntxt"),
  textOutput("mytxt")

)

server <- function(input, output, session) {

  myvals <- reactiveValues(
    txt = NULL
  )

  mytxt <- callModule(returnServer, "returntxt")

  observe({ 
    myvals$txt <- mytxt$txt 
    print(myvals$txt)
  })

  output$mytxt <- renderText({ myvals$txt })

}

shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)

module_1.R

returnUI = function(id) {
  ns <- NS(id)

  tagList(
    textInput(ns("txt"), "Write something")
  )
}

returnServer = function(input, output, session) {
  myreturn <- reactiveValues()

  observe({ myreturn$txt <- input$txt })

  return(myreturn)
}
Run Code Online (Sandbox Code Playgroud)