How to change RStudio-Server working directory in Jupyterhub?

Hello,

I have installed RStudio server in a profile for Jupyterhub.
My problem is when I click on the RStudio button and land on the RStudio page, the Working Directory is always ‘/home/jovyan/’. But this is supposed to be ‘/home/username’.
I already tried with ‘setwd(“/home/username”)’ to change this. But although I get the correct working directory with ‘getwd()’, RStudio does not notice this.
I hope that you can help me further.

~Sincerely

Are you using jupyter-server-proxy to launch RStudio server? If so, depending on which directory, the single user server is launched, that would be the launch directory of the RStudio server as well. If you would like to change it, you will need to change the config for the RStudio server proxy.

Yes, I use jupyter-server-proxy.
What config files would I need to modify to make it work for RStudio Server?

Is it just rstudio that shows /home/jovyan, or does jupyter notebook/lab also default to that directory?

How have you configured JupyterHub- can you ideally give us enough information for us to setup an environment to reproduce your problem?

Only RStudio shows /home/jovyan respectively ~.

In JupyterHub (Notebook/Lab), the working directory is set to /home/<username>.

JupyterHub is configured through a config.yaml since it’s running on Kubernetes. The working directory is set using the extraConfig in the config.yaml, specifically by setting the working directory through the Kubespawner:
spawner.working_dir = spawner._expand_user_properties("/home/{username}")

When I use getwd() in the JupyterHub terminal, it gives me the correct working directory. However, in RStudio, when I use getwd(), it returns the wrong directory.

Informationen from the System:
JUPYTERHUB_K8S_VERSION=1.1.3-n721.h0acbcf43

I also tried adjusting the rserver.conf and rsession.conf files, but it didn’t work either. When I modified the rserver.conf, I couldn’t even access RStudio anymore.

Dockerfile:

ARG BASE_IMAGE=jupyter/minimal-notebook:hub-2.3.1
FROM ${BASE_IMAGE}


RUN pip install jupyter-rsession-proxy
RUN pip install jupyter-server-proxy
RUN jupyter labextension install @jupyterlab/server-proxy


USER root

# Specify RStudio version
ENV RSTUDIO_VERSION "2023.06.1-524"
# Make sure RStudio run when booting the image
ENV RSESSION_PROXY_RSTUDIO_1_4=yes

# Install OS dependencies of RStudio
RUN apt-get update
RUN apt-get install -y gdebi-core

RUN curl --silent -L --fail https://download2.rstudio.org/server/jammy/amd64/rstudio-server-${RSTUDIO_VERSION}-amd64.deb > /tmp/rstudio.deb
RUN apt-get install -y /tmp/rstudio.deb
RUN rm /tmp/rstudio.deb
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
    
# RStudio needs to run as the notebook user
RUN chown -R ${NB_USER}:rstudio-server /var/lib/rstudio-server
RUN chmod -R g=u /var/lib/rstudio-server

ENV PATH=$PATH:/usr/lib/rstudio-server/bin


RUN curl -LO https://quarto.org/download/latest/quarto-linux-amd64.deb
RUN gdebi -n quarto-linux-amd64.deb
RUN rm quarto-linux-amd64.deb


#RUN chmod 777 /etc/rstudio/rserver.conf
#RUN echo '# Server Configuration File' > /etc/rstudio/rserver.conf;
#RUN echo "server-working-dir=\"/home/$JUPYTERHUB_USER\"" >> /etc/rstudio/rserver.conf;

#RUN chmod 777 /etc/rstudio/rsession.conf
#RUN echo '# R Session Configuration File' > /etc/rstudio/rsession.conf;
#RUN echo "session-default-working-dir=\"/home/$JUPYTERHUB_USER\"" >> /etc/rstudio/rsession.conf;

If you look here in the jupyter-rsession-proxy, we are executing some R command to set R home directory. I dont have a lot of experience with R and I am not sure what that command supposed to return. Maybe that can give you some idea on why R home directory is different from that of single user server.

What’s the value of $HOME? It might be helpful to see your full environment:

env | sort

I have looked at the env for jupyter-rsession-proxy. Also don’t really know anything about R and just have the job of getting it to work. But looking at the env and the descriptions for it, unfortunately none should match.

Yes, home is indeed still HOME=/home/jovyan. What is the best way to customize this?

I dont have a lot of experience in using jupyter containers. But looking at the start.sh, it is clear that if NB_USER env var is properly configured, the single server should be started in the home directory of the NB_USER. So, if your HOME is still pointing to /home/jovyan, it means that NB_USER is not properly configured?

Also, you mentioned config spawner.working_dir = spawner._expand_user_properties("/home/{username}"), but there is no working_dir traitlet on spawner object. I assume you must be setting up spawner.notebook_dir to user home as well? That explains why your notebook has correct user home and not R studio.

The Kubespawner has working_dir, otherwise the working directory in the notebook would be /home/jovyan.
https://jupyterhub-kubespawner.readthedocs.io/en/latest/spawner.html

I have tested spawner.notebook_dir, unfortunately nothing has changed.

I now set the HOME env in the dockerfile directly to /home/<username>.
When I look at the Env (HOME, PWD) in the terminal in Jupyterhub, they are /home/<username>.
If I start RStudio and use the terminal there, then both variables are /home/jovian again.

I am slowly running out of ideas