【问题标题】:Include Shiny app in R package: Transfer an input parameter在 R 包中包含 Shiny 应用程序:传输输入参数
【发布时间】:2018-10-03 06:45:01
【问题描述】:

我正在尝试将 Shiny 应用程序作为 R 包的一部分运行。我按照 Dean Attali 网站 (https://deanattali.com/2015/04/21/r-package-shiny-app/) 上列出的说明进行操作。

如该网站所示,我在我的 R 包中创建了如下目录:

- mypackage
  |- inst
     |- shiny-examples
        |- myapp
           |- ui.R
           |- server.R
  |- R
     |- runExample.R
     |- ...
  |- DESCRIPTION
  |- ...

然后,在我的 runExample.R 文件中,我使用了:

#' @export
runExample <- function() {
  appDir <- system.file("shiny-examples", "myapp", package = "mypackage")
  if (appDir == "") {
    stop("Could not find example directory. Try re-installing `mypackage`.", call. = FALSE)
  }

  shiny::runApp(appDir, display.mode = "normal")
}

唯一的区别是我试图在上面的 function() 中输入一个参数。它被称为数据,所以我使用函数(数据=数据)。

但是,如果我运行 runExample(data=myData),这会导致与数据相关的错误(“'closure' 类型的对象不是子集”)。我相信这是因为 data=myData 没有成功传输到 Shiny 应用程序。任何有关如何解决此问题的想法将不胜感激!

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    您可以使用环境在 R 中几乎任何地方移动数据。我认为这可能可行:

    ### runExample.R
    
    PKGENVIR <- new.env(parent=emptyenv()) # package level envir
    
    #' @export
    runExample <- function(data) {
      appDir <- system.file("shiny-examples", "myapp", package = "mypackage")
      if (appDir == "") {
        stop("Could not find example directory. Try re-installing `mypackage`.", call. = FALSE)
      }
      PKGENVIR$DATA <- data # put the data into envir
      shiny::runApp(appDir, display.mode = "normal")
    }
    

    然后在server.R中:

    ### inside shiny app
    data <- PACKAGE_NAME:::PKGENVIR$DATA ## read the data from envir
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-12
      • 2017-01-04
      • 1970-01-01
      • 2021-08-14
      • 2014-06-29
      • 1970-01-01
      相关资源
      最近更新 更多