0

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.

alex_555
  • 1,092
  • 1
  • 14
  • 27
  • How are you loading the workspace in the code? Do you have an explicit call to `load()` somewhere? Where does the workspace file currently exist on the server? – MrFlick Mar 02 '23 at 15:44
  • I first set the working directory `setwd()` to the according folder of the workspace (which is in a subdirectory on the server). Then I call `load()` on the .RData file and lastly the code is continued by `shinyUI()`. – alex_555 Mar 02 '23 at 15:50
  • Is all your shiny code in one file? Are you sure the code that loads the data is running without error? It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) that can be used to test and verify possible solutions. There's a lot of uncertainty about how your application is actually set up. – MrFlick Mar 02 '23 at 15:57
  • I added an example - it wasn't so easy to reproduce the error, but I realized that starting the app on an empty GlobalEnv is causing the problem. So how can I get the workspace loaded into the GlobalEnv? Thank you for your help! – alex_555 Mar 03 '23 at 09:02
  • 1
    Two general notes: (1) use `file.path` instead of `paste0` when working with paths. (2) you **never** need to prefix a path with `getwd()`. Just using `"workspace.RData"` as the path already does the correct thing (and, importantly, it does the *same* thing: your added code is therefore redundant). – Konrad Rudolph Mar 03 '23 at 09:21
  • Thanks for the notes. I know, I should've removed it here, but getwd() is just meant as a placeholder for the dirname of the path I use on my system. – alex_555 Mar 03 '23 at 09:27

1 Answers1

0

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")
alex_555
  • 1,092
  • 1
  • 14
  • 27