4

I am attempting to run a Shiny app as part of an R package. I followed the instructions listed on Dean Attali's website (https://deanattali.com/2015/04/21/r-package-shiny-app/).

As shown on that website, I created directories as follows in my R package:

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

Then, in my runExample.R file, I used:

#' @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")
}

The only difference is that I am attempting to input one parameter into function() above. It is called data, so I am using function(data=data).

However, if I run runExample(data=myData), this causes an Error related to data ("object of type 'closure' is not subsettable"). I believe this is because data=myData is not successfully transferred to the Shiny app. Any ideas on how to approach this issue would be much appreciated!

1 Answers1

2

You can use environments to move data nearly anywhere in R. I think that this might work:

### 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")
}

Then in server.R:

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