【发布时间】:2018-10-26 21:45:06
【问题描述】:
在 R Shiny 应用程序中,我在将变量从模块返回到服务器时遇到了惊人的困难。在模块中,我想在观察到按钮按下时返回一个值,因此我将return() 语句包装在observeEvent() 内的一个块中。但是,没有返回所需的值,整个observeEvent() 块似乎是。
我试图创建一个最小的工作示例来概述以下问题:
ui.R
# ui.R
fluidPage(
input_module_ui("input"),
actionButton("print_input_button",
label = "Print Input")
)
服务器.R
# server.R
function(input, output, session) {
# Calling input module.
input_module_return <- callModule(input_module, "input")
observeEvent(input$print_input_button, {
print(input_module_return)
})
}
global.R
# global.R
source("modules/input.R")
输入.R
# input.R
input_module_ui <- function(id) {
ns <- NS(id)
tagList(
textInput(ns("text_input"),
label = h2("Input Text:")),
actionButton(ns("submit_input"),
label = "Submit Input")
)
}
input_module <- function(input, output, session) {
print("I should only print once")
observeEvent(input$submit_input, {
print("Return input")
return(input$text_input)
})
}
在测试这个应用程序时,我在文本输入框中输入了“test”并提交了我的输入。但是,当我尝试打印我的输入,而不是像我期望的那样打印“测试”时,打印了以下内容:
<Observer>
Public:
.autoDestroy: TRUE
.autoDestroyHandle: function ()
clone: function (deep = FALSE)
.createContext: function ()
.ctx: environment
destroy: function ()
.destroyed: FALSE
.domain: session_proxy
.execCount: 3
.func: function ()
initialize: function (observerFunc, label, suspended = FALSE, priority = 0,
.invalidateCallbacks: list
.label: observeEvent(input$submit_input)
.onDomainEnded: function ()
onInvalidate: function (callback)
.onResume: function ()
.prevId: 1896
.priority: 0
resume: function ()
run: function ()
self: Observer, R6
setAutoDestroy: function (autoDestroy)
setPriority: function (priority = 0)
suspend: function ()
.suspended: FALSE
我相信这对应于input.R中的最后一个块:
observeEvent(input$submit_input, {
print("Return input")
return(input$text_input)
})
当观察到input$submit_input 时,如何让此应用按预期工作并返回input$text_input?
【问题讨论】: