如何在R Shiny应用程序中将变量从模块返回到服务器? [英] How to return a variable from a module to the server in an R Shiny app?

查看:70
本文介绍了如何在R Shiny应用程序中将变量从模块返回到服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在R Shiny应用程序中,从模块将变量返回到服务器时,我遇到了令人惊讶的困难.在模块中,我想在观察到按钮按下时返回一个值,因此我将return()语句包装在observeEvent()内的一个块中.但是,未返回所需的值,整个observeEvent()块似乎都存在.

I'm having a surprising amount of difficulty returning a variable from a module to the server in an R Shiny app. In a module, I'd like to return a value when a button press is observed, so I've wrapped the return() statement in a block inside observeEvent(). However, the desired value is not returned, the entire observeEvent() block appears to be.

我试图创建一个最小的工作示例,概述以下问题:

I've attempted to create a minimal working example outlining the problem below:

# ui.R

fluidPage(
  input_module_ui("input"),
  actionButton("print_input_button",
               label = "Print Input")
)

server.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")

input.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"并提交了我的输入.但是,当我尝试打印我的输入时,而不是按照我期望的那样打印测试",而是打印了以下内容:

When testing this app, I entered "test" in the text input box and submitted my input. However, when I attempted to print my input, instead of printing "test" as I'd expect, the following was printed:

<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中的最后一个块:

I believe that this corresponds to the last block in input.R:

observeEvent(input$submit_input, {
    print("Return input")
    return(input$text_input)
  })

如何使该应用程序按预期工作并在观察到input$submit_input时返回input$text_input?

How can I get this app to work as intended and return input$text_input when input$submit_input is observed?

推荐答案

您已经很接近将其开始工作了.闪亮模块的诀窍在于,将变量传入和传出它们需要将变量作为反应值进行传递.我对您的代码做了两个小改动,以获得我认为您希望看到的内容.

You were quite close to getting this to work. The trick with shiny modules is that passing variables into and out of them requires passing the variables as reactive values. I made two small changes to your code to get what I think you're hoping to see.

首先是从服务器模块(而不是观察者本身,应仅告诉应用程序您想发生的事情)返回input$text_input的响应版本:

First was to return a reactive version of input$text_input from the server module (rather than from the observer itself, which should just tell the app what you want to happen):

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

  print("I should only print once")

  observeEvent(input$submit_input, {
    print("Return input")
  })

  return(reactive({input$text_input}))

}

第二个更改是,现在input_module的输出是反应性的.如果要使用值而不是函数内容,则需要使用()解析对象.因此,在您的服务器功能中:

The second change is that now the output from input_module is reactive. If you want the values rather than the function contents, you need to resolve the object using (). So, in your server function:

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

  # Calling input module.
  input_module_return <- callModule(input_module, "input")

  observeEvent(input$print_input_button, {
    print(input_module_return())
  })

}

输出:

Listening on http://127.0.0.1:6796
[1] "I should only print once"
[1] "Return input"
[1] "test"

这篇关于如何在R Shiny应用程序中将变量从模块返回到服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆