【问题标题】:Pass objects to shiny app and launch with runApp将对象传递给闪亮的应用程序并使用 runApp 启动
【发布时间】:2016-11-15 03:26:35
【问题描述】:

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

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

以下是相关代码:

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

    #' @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": 系统找不到指定的路径

【问题讨论】:

标签: r scope shiny


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-09
    • 2017-12-25
    • 2015-12-28
    • 1970-01-01
    • 2017-01-27
    • 1970-01-01
    • 2020-05-03
    相关资源
    最近更新 更多