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.
In the help documentation in
shiny::runApp
, it saysappDir
could be either of the below: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 shinyAppyou can try things like
Update
create a script myapp_script.R with
and then call
runApp("myapp_script.R")