Run RStudio Server From Conda Envs

Article author
Jillian Rowe
  • Updated

 

There are currently two ways to Run RStudio server from the terminal. Which one works will depend upon your setup, but start with Option 1.

Option 1

For this option you shouldn't need to set any R_* variables, just source your correct R env.

source activate bioconductor

Now, RStudio will overwrite some variables and we need to tell it what's up.

env |grep bioconductor

Now you want to get the base of the bioconductor base installation, or the prefix. This might be in the variable CONDA_ENV or CONDA_PREFIX, or you might be able to get it with conda env list. Try all these to get the base - 

echo $CONDA_ENV
echo $CONDA_PREFIX
echo $PATH
# Get the first element of the PATH, which is probably your CONDA_ENV
echo ${PATH%%:*}
conda env list
[jillian@somewhere ~]$ conda env list
# conda environments:
#
base /apps/easybuild/1.0/software/Miniconda3/4.7.10
bioconductor /home/jillian/.conda/envs/bioconductor

In this case the base installation path is /home/jillian/.conda/envs/bioconductor

PORT='3004'
/usr/lib/rstudio-server/bin/rserver --www-port=${PORT} \
--www-frame-origin=same \
--rsession-ld-library-path=${CONDA_PREFIX}/lib/:${CONDA_PREFIX}/lib/

Check your R

Check which R is being used in RStudio with:

system("type R")

Option 2

The short version of this is to make sure you have the correct R variables in your ~/.bashrc, source your conda env with your desired R + R packages in it, and then run it.

Do not change any of these R_* variables! This will result in weird Java errors.

 

# You can also add these to your ~/.bashrc
export R_DOC_DIR='/usr/share/doc/R-3.4.3'
export R_HOME='/usr/lib64/R'
export R_INCLUDE_DIR='/usr/include/R'
export R_SHARE_DIR='/usr/share/R'
export RSTUDIO_DEFAULT_R_VERSION_HOME='/usr/lib64/R'
export RSTUDIO_DEFAULT_R_VERSION='3.4.3'

# activate your conda env
source activate bioconductor

# Run on a free port
PORT="3003"
/usr/lib/rstudio-server/bin/rsession --standalone=1 \
--program-mode=server --log-stderr=1 \
--session-timeout-minutes=0 --user-identity=$USER --www-port=${PORT}

 

Tip

If you get any errors relating to the port just keep increasing the port by 1 until you find a free port.

 

Tip

If this option works for you add the R_* variables to your ~/.bashrc and then run source ~/.bashrcor create a launch script that does this for you.

 

Check your R

Check which R is being used in RStudio with:

system("type R")

 

Detailed Instructions

If you don’t yet have an R environment, or would just like to start from scratch, start here.

 

Install R into a Conda Env

module load Miniconda3

 

If you don’t have one already, create a conda env with some R packages.

Note

If you don’t know what conda envs you have installed you can list them with conda env list

# -n is for name. You use the name to activate your environment
 conda create -n bioconductor -y -c conda-forge -c bioconda bioconductor-biocgenerics

 

Make your R available to your PATH

Then activate your environment.

source activate bioconductor

 

Note

Make sure you’re activating with the name that you gave your env!

For this next step you may have to change the ports. If anyone else is running from the same node with the same port that port will appear as busy.

 

# You can also add these to your ~/.bashrc
export R_DOC_DIR='/usr/share/doc/R-3.4.3'
export R_HOME=/'usr/lib64/R'
export R_INCLUDE_DIR='/usr/include/R'
export R_SHARE_DIR='/usr/share/R'
export RSTUDIO_DEFAULT_R_VERSION_HOME='/usr/lib64/R'
export RSTUDIO_DEFAULT_R_VERSION='3.4.3'

PORT="3003"
/usr/lib/rstudio-server/bin/rsession --standalone=1 --program-mode=server --log-stderr=1 --session-timeout-minutes=0 --user-identity=$USER --www-port=${PORT}

 

Note that if you already have the R_* variables in your ~/.bashrc that there is no need to add them again.

And open your browser at - https://MY_JHUB_DOMAIN/user/{user}/proxy/3003/

Make sure to include the trailing slash!

 

Check your R

If you want to be sure that your RStudio instance is using the correct R follow the instructions above and then from the RStudio R console type:

system("type R")

 

and you should see your conda activated R.

Was this article helpful?

Comments

0 comments

Please sign in to leave a comment.

Still have questions?

Submit a request