Rstudio: change working directory from batch script when calling `rserver` or `rsession`

I want Rstudio to start in a given working directory.

I am launching Rstudio from a batch script. I'm using the Rocker ML Verse 4.1.1 container. Here is some pseudocode of the batch script:

cat HEREDOC > RSESSION_WRAPPER_FILE
exec &> rsession.log
exec rsession $@
HEREDOC

apptainer run \
	"$RSTUDIO_SERVER_IMAGE" 
    /usr/lib/rstudio-server/bin/rserver \
    --rsession-path $RSESSION_WRAPPER_FILE

I have tried many different ways to make the Rstudio session start in a given directory:

cd before calling apptainer:

cd <directory>
apptainer run ...

cd in the rsession wrapper script:

cat HEREDOC > RSESSION_WRAPPER_FILE
cd <directory>
exec rsession $@
HEREDOC

rsession --session-default-working-dir:

cat HEREDOC > RSESSION_WRAPPER_FILE
exec rsession $@ --session-default-working-dir <directory>
HEREDOC

rserver --server-working-dir:

apptainer run --server-working-dir <directory> ...

.Rprofile:

cat HEREDOC > ./.Rprofile
setwd("<directory>")
HEREDOC

.Rprofile and cd in the rsession wrapper:

cat HEREDOC > RSESSION_WRAPPER_FILE
cd <parent of .Rprofile>
exec rsession $@ --session-default-working-dir <directory>
HEREDOC

cat HEREDOC > ./.Rprofile
setwd("<directory>")
HEREDOC

all to no avail.

I checked for interfering Rprofile files as shown on this stackoverflow answer and found two files:

[1] "/usr/local/lib/R/etc/Rprofile.site"  
[2] "/home/simonleary_umass_edu/.Rprofile"

/usr/local/lib/R/etc/* are all stock from the Rocker container.
Here's the one in my home directory:

.libPaths( c( .libPaths(), "/home/simonleary_umass_edu") )

Seems that there are three possibly conflicting environments that could be at fault

  1. The environment from which the container is launched
  2. The environment in which the rserver is running
  3. The internal environment created by rserver

Maybe eliminate the container to test the first possibility?

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.