r - 将对象传递给 Shiny 的应用程序并使用 runApp 启动

标签 r scope shiny

我正在创建一个包含一些交互式 Shiny 应用程序的包。这些应用程序的目的是促进内存中对象的 GUI 探索。例如,我有一个由离散变量组成的对象,我想将其传递给 Shiny 的应用程序,然后通过 GUI 界面进行调整。

但是,当我尝试从 Shiny 应用程序访问此内存中对象时遇到了麻烦。

相关代码如下:

首先,我将 shinyServer 函数包装在另一个函数中。我的想法是让 Shiny 的服务器访问传递的对象。

    #' @export
    appServer <- function(bins) {
      su <- summary(bins)
      shinyServer(function(input, output) {

        ## values that should trigger updates when changed
        values <- reactiveValues(summary=su, i=1, bins=bins)

    # excluded rest of body for brevity ...

    }

在此函数中,我创建一个 shinyApp 对象并传入 ui(在另一个文件中)以及上面定义的 appServer 函数的结果。

makeApp <- function(bins) {
  shiny::shinyApp(
    ui = appUI,
    server = appServer(bins))
}

在此函数中调用前面的函数,该函数包装对 runApp 的调用并从用户处获取参数。

#' @export
adjust <- function(bins) {
  ## access data from the app?

  app <- makeApp(bins)
  shiny::runApp(app)
}

如何将内存中的对象传递给从另一个包导入的shinyApp?

当我执行上面的代码时,我收到以下错误:

ERROR: path[1]="C:\Users\myusername\AppData\Local\Temp\RtmpWMpvHT\widgetbinding23e8333e5298": The system cannot find the path specified

最佳答案

在下面的示例中,我演示了如何将对象x从全局环境或任何其他环境传递到 Shiny 的应用程序并更改其值。我不确定这是否能回答你的问题。无论如何,它也许有用:)

library(shiny)

x <- 5
x
deparse(substitute(x)) # is going to do the trick

fun <- function(obj) {

  # get the name of the passed object
  object_to_change <- deparse(substitute(obj)) 

  # get the object from a given environment
  val <- get(object_to_change, envir = .GlobalEnv) 
  # ?environment

  # Save the object as a reactive value
  values <- reactiveValues(x = val)                                   

  # Now define the app that is going to change the value of x
  ui <- shinyUI(fluidPage(
    br(),
    actionButton("quit", "Apply changes and quit"),
    textInput("new", "", value = NULL, placeholder = "Set new value of x"),
    textOutput("out")
  ))

  server <- function(input, output) {

    output$out <- renderPrint({ 
      values$x        
    })

    # change the value of x
    observe({
      req(input$new)
      values$x <- as.numeric(input$new)
    })

    # Apply changes and quit     
    observe({
      if (input$quit == 1) {
        assign(x = object_to_change, value = values$x, envir = .GlobalEnv)
        stopApp()
      } 
    })
  }
  # Run the app  
  shiny::shinyApp(ui, server)
}

fun(x)

# Check the new value of x in the .GlobalEnv
x

关于r - 将对象传递给 Shiny 的应用程序并使用 runApp 启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38336468/

相关文章:

javascript - 点符号、窗口对象及其属性

react 物: invalid (NULL) left side of assignment

c# - 使用 Dispose 注入(inject) transient 作用域

r - 需要一种方法来立即重新加载 session ,而无需完成 Shiny 中的代码序列

返回在绘图散点图中选择的数据点

r - 使用 for 循环整理工作 R 代码

r - 读取 png、添加网格和坐标以及输出的快速方法

r - 如何将 R 包(已在 CRAN 中)添加到 CRAN 任务 View ?

r - 为什么更新数据表后数据表中的 R/Shiny 输入无法正常工作?

javascript - 使用从函数返回的字符串选择预定义变量