Set www location in shiny::shinyApp

1.3k views Asked by At

I am currently creating a shiny app that gets invoked with shiny::shinyApp via a wrapper function.

startApp <- function(param1, param2, ...){
  # in fact, ui and server change based on the parameters
  ui <- fluidPage()
  server <- function(...){}
  runApp(shinyApp(ui, server))
}

When I include resources (like images, videos etc.), I currently use the addResourcePath command and include the resources with a prefix. However, I would like to add a "default resource path" (appDir/www in usual apps). There seems to be no suitable parameter in shinyApp or runApp. Setting the working directory to the resource folder or one level above does not work either.

Here is a short MWE.

## ~/myApp/app.R
library(shiny)
shinyApp(
  fluidPage(tags$img(src = "image.gif")),
  server <- function(...){}
)

## ~/myApp/www/image.gif
# binary file

If I run the app via RunApp("~/myApp") everything works, but

setwd("~/myApp")
myApp <- shinyApp(source("app.R")$value)
runApp(myApp)

will fail to display the image. Any suggestions are appreciated.

Context

The reason I want to start the app based on an shiny.appobj (an object that represents the app) rather than a file path is, that the latter approach does not work well with passing parameters to an app. Here is a discussion about this topic.

The recommended way of passing parameters to an app that gets invoked by runApp("some/path") is as follows:

startApp <- function(param1, param2, ...) {
  .GlobalEnv$.param1 <- param1
  .GlobalEnv$.param2 <- param2
  .GlobalEnv$.ellipsis <- as.list(...)
  on.exit(rm(.param1, .param2, .ellipsis, envir = .GlobalEnv))
  runApp("~/myApp")
}

This approach is just ugly IMO and I get warnings when I build the package that contains the app together with the startApp function. Those warnings occur because the package then breaks the recommended scoping model for package development.

1

There are 1 answers

3
shaojl7 On

In the help documentation in shiny::runApp, it says appDir could be either of the below:

A directory containing server.R, plus, either ui.R or a www directory that contains the file index.html.

A directory containing app.R.

An .R file containing a Shiny application, ending with an expression that produces a Shiny app object.

A list with ui and server components.

A Shiny app object created by shinyApp.

When you run via RunApp("~/myApp"), it is a directory containing app.R If you want to run via a shiny app object created by shinyApp

you can try things like

myapp_obj <- shinyApp(
  fluidPage(tags$img(src = "image.gif")),
  server <- function(...){}
)
runApp(myapp_obj)

Update
create a script myapp_script.R with

shinyApp(
  fluidPage(tags$img(src='image.gif')),
  server <- function(...){}
)

and then call runApp("myapp_script.R")