【问题标题】:Getting inputs from modules created with insertUI从使用 insertUI 创建的模块中获取输入
【发布时间】:2019-08-08 16:04:38
【问题描述】:

在我的应用程序中,我将使用insertUI 动态创建一个模块。我正在尝试找到一种方法来捕获此模块的输出。在下面的应用程序中,有一个由textInput 元素组成的简单模块。服务器部分返回此textInput 元素的值并报告它处于活动状态。我正在尝试捕获此返回值。

我试图创建一个reactiveValues 对象来获取模块的输出,但是当输入改变时这个对象似乎没有更新。模块代码本身正在运行,因为我收到来自模块内部的消息,但检查reactiveValues 的外部观察命令仅在重新创建时执行。如何从模块外部获取这些值

library(shiny)

# a module that only has a text input
moduleUI = function(id){
    ns = NS(id)
    tagList(
        # the module is enclosed within a div with it's own id to allow it's removal
        div(id = id,
            textInput(ns('text'),label = 'text'))
    )
}

# the module simply returns it's input and reports when there is a change
module = function(input,output,session){
    observeEvent(input$text,{
        print('module working')
    })
    return(input$text)
}


# ui is iniated with add/remove buttons
ui = fluidPage(
    actionButton('add','+'),
    actionButton('remove','-')
)

server = function(input, output,session) {
    # number of ui elements already created
    uiCount = reactiveVal(0)

    moduleOuts = reactiveValues()

    observeEvent(input$add,{
        # count the UI elements created to assign new IDs
        uiCount(uiCount()+1)
        # insert module UI before the add/remove buttons
        insertUI('#add',
                 'beforeBegin',
                 moduleUI(paste0("module",uiCount())))

        # failed attempt to get the output
        moduleOuts[[as.character(uiCount())]] = callModule(module,id = paste0("module",uiCount()))
    })

    observeEvent(input$remove,{
        # if remove button is pressed, remove the UI and reduce the UI count
        removeUI(
            selector = paste0("#module",uiCount())
        )
        uiCount(uiCount()-1)
    })

    # simple test
    observe({
        print(moduleOuts[['1']])
    })

    # test to get all values
    observe({
        seq_len(uiCount()) %>% sapply(function(i){
            print(moduleOuts[[as.character(i)]])
        })
    })

}


shinyApp(ui = ui, server = server)

【问题讨论】:

    标签: r module shiny


    【解决方案1】:

    这里的问题很简单:您需要显式地将值作为响应式返回。 return(reactive(input$text))

    通过只返回input$text,您实际上只是在某个时间点返回它,因为input$text 只是一个值。通过将其包装在 reactive() 中,您现在将返回一个反应式表达式。服务器代码必须与之匹配,所以我在访问该值的代码中添加了()

    我删除了“简单测试”,因为它导致错误,因为该模块最初不存在。

    library(shiny)
    
    # a module that only has a text input
    moduleUI = function(id){
      ns = NS(id)
      tagList(
        # the module is enclosed within a div with it's own id to allow it's removal
        div(id = id,
            textInput(ns('text'),label = 'text'))
      )
    }
    
    # the module simply returns it's input and reports when there is a change
    module = function(input,output,session){
      observeEvent(input$text,{
        print('module working')
      })
      return(reactive(input$text))
    }
    
    
    # ui is iniated with add/remove buttons
    ui = fluidPage(
      actionButton('add','+'),
      actionButton('remove','-')
    )
    
    server = function(input, output,session) {
      # number of ui elements already created
      uiCount = reactiveVal(0)
    
      moduleOuts = reactiveValues()
    
      observeEvent(input$add,{
        # count the UI elements created to assign new IDs
        uiCount(uiCount()+1)
        # insert module UI before the add/remove buttons
        insertUI('#add',
                 'beforeBegin',
                 moduleUI(paste0("module",uiCount())))
    
        # failed attempt to get the output
        moduleOuts[[as.character(uiCount())]] = callModule(module,id = paste0("module",uiCount()))
      })
    
      observeEvent(input$remove,{
        # if remove button is pressed, remove the UI and reduce the UI count
        removeUI(
          selector = paste0("#module",uiCount())
        )
        uiCount(uiCount()-1)
      })
    
      # test to get all values
      observe({
        seq_len(uiCount()) %>% sapply(function(i){
          print(moduleOuts[[as.character(i)]]())
        })
      })
    
    }
    
    
    shinyApp(ui = ui, server = server)
    

    【讨论】:

      猜你喜欢
      • 2021-11-06
      • 2018-02-13
      • 2020-07-21
      • 2018-08-21
      • 2020-09-10
      • 2021-11-11
      • 1970-01-01
      • 2016-12-23
      • 1970-01-01
      相关资源
      最近更新 更多