What I want to do
I am trying to host a shiny app on shiny-server (Ubuntu). I have set up everything as shown in the official docs (using nginx
).
Upon starting, the app needs to load a workspace (which can be updated in the background). In RStudio the app works fine and loads the workspace as intended.
The Problem
Running on shiny-server, the app shows up at its target url and the base-functionality is working as intended, but the data is missing. The errors in the log-files show that the workspace variables can't be accessed, i.e. the workspace is either not loaded or there is no access to the environment.
I gave the user full access to read, write and execute files, so that the workspace can be loaded correctly, but that does not work.
My question
What is the correct way of loading a workspace into a shiny app on shiny-server to access its contents in the app?
Thank you in advance!
EDIT
Example
Try to first generate the data and then to start the app with a fresh empty environment. It can't find the data (not only on shiny-server, but in general).
Create some data and safe it. Then, clear the .GlobalEnv
and start the app.
x <- iris
y <- mtcars
save.image(file=paste0(getwd(),"/workspace.RData"))
Here's the shiny-app:
library(shiny)
# Loading workspace prior to starting the app
setwd(getwd())
load("workspace.RData")
# UI
ui <- fluidPage(
sidebarLayout(
sidebarPanel(sliderInput("in_slider",label="input",min=0,max=1,step=1,value=0)),
mainPanel(plotOutput("out_plot"))
)
)
# Server
server <- function(session,input,output) {
output$out_plot <- renderPlot({plot(if(input$in_slider==0){x}else{y})})
}
shinyApp(ui=ui,server=server)
The goal is to correctly load the workspace into the app so that the .GlobalEnv
is not empty when running the app.
PS: the calls to getwd() are meant as placeholders for the dirname of a custom path.
I managed to solve my problem by building a global.R
file and sourcing it before starting the ui. The global.R
file contains the call of load("workspace.RData")
. In the docs I found that a server.R
+ ui.R
structure will load the global.R
file everytime, but using an app.R
structure may require the call to source()
at the beginning.
This worked for me (app.R
):
source("global.R")
ui <- ...
The global.R
file contains:
load("workspace.RData")