在我的应用程序中,我将使用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)
Run Code Online (Sandbox Code Playgroud)
这里的问题很简单:您需要明确地将值作为反应式返回。 return(reactive(input$text))
通过返回 just 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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
694 次 |
| 最近记录: |