O.rka
O.rka

Reputation: 30677

How to set up conda-installed R for use with RStudio?

enter image description here

I've been trying to set up my R using conda (eventually to use with Beaker Notebook) and I want to be able to use RStudio with my conda-installed version of R.

My method of installing R:

conda install -c r r
conda install -c r r-essentials
conda install -c r r-rserve
conda install -c r r-devtools
conda install -c r r-rcurl
conda install -c r r-RJSONIO
conda install -c r r-jpeg
conda install -c r r-png
conda install -c r r-roxygen2
conda install --channel https://conda.anaconda.org/bioconda bioconductor-edger

I ran that version of R (I only installed this version)

> version
               _                           
platform       x86_64-apple-darwin11.0.0   
arch           x86_64                      
os             darwin11.0.0                
system         x86_64, darwin11.0.0        
status                                     
major          3                           
minor          3.1                         
year           2016                        
month          06                          
day            21                          
svn rev        70800                       
language       R                           
version.string R version 3.3.1 (2016-06-21)
nickname       Bug in Your Hair   

Running R in Jupyter is kind of buggy. For example, when it outputs errors, it outputs to stdout and splits every character in the string with a linebreak. I want to use RStudio but I don't want to install another version of R.

How can I route my conda version of R into RStudio?

Here's my .bash_profile not sure if this will be useful:

$ cat ~/.bash_profile
# added by Anaconda3 4.0.0 installer
export PATH="/Users/jespinoz/anaconda/bin:$PATH"

export RSTUDIO_WHICH_R=/Users/jespinoz/anaconda/bin/R 

I've been trying to follow these tutorials but I am lost. I'm really not too familiar with environment variables and such things.

(1) https://support.rstudio.com/hc/en-us/community/posts/207830688-Using-RStudio-with-conda

(2) Launch mac eclipse with environment variables set

when I looked for my R it directed me to:

$ which R
/Users/jespinoz/anaconda/bin/R

but the directions from (1) is using this path which is very confusing:

/Users/jespinoz/anaconda/lib/R/bin/R

I tried doing what this guy did and added this to my .bash_profile but it didn't work. I even made a .bashrc but it still didn't work (I sourced both after I added the lines)

export RSTUDIO_WHICH_R=/Users/jespinoz/anaconda/bin/R

How to tell RStudio to use R version from Anaconda

Unfortunately, anaconda has no tutorial for this in https://docs.continuum.io/anaconda/ide_integration

Upvotes: 40

Views: 58590

Answers (8)

merv
merv

Reputation: 76700

Launch RStudio from Activated Conda Environment

At least for Mac OS X, I find that it is sufficient to activate the environment in a shell session, then launch RStudio.

$ conda activate my_r_env
$ /Applications/RStudio.app/Contents/MacOS/RStudio

Once in R, one can verify that values of R.home() and .libPaths() point to the environment-specific locations.

The advantage here is that you aren't fixed to whatever was last set in the environment variables, e.g., via .bash_profile. Instead, one can have many R-based environments and switch between them (or run multiple ones simultaneously) without tampering with global settings.

Suggested Aliases for Convenience

Perhaps the only global setting I might recommend is to add an alias for rstudio to your .bash_profile so you don't have to type the full path every time, like

alias rstudio='/Applications/RStudio.app/Contents/MacOS/RStudio &'

which enables one to then do

$ conda activate my_r_env
$ rstudio
$

where the & enables one to continue using the shell, or close it, without affecting the RStudio instance.

Auto-detecting architecture

The newer RStudio builds for macOS ship in a unified format, including both x86_64 and aarch64 binaries. Many Conda R packages are not yet built for osx-arm64 architecture (for example, all of Bioconda), requiring users to create environments that emulate osx-64 and launching rstudio as above will be incompatible with this. Here's what I use on Apple Silicon machines to automatically switch the RStudio architecture.

~/.zshrc

alias crun='conda run --no-capture-output --live-stream -n'

crunr() {
  ## is arm64?
  conda run -n $1 R --version | grep 'aarch64' > /dev/null
  if [ $? -eq 0 ]; then
    crun $1 /Applications/RStudio.app/Contents/MacOS/RStudio &
  else
    crun $1 arch -x86_64 /Applications/RStudio.app/Contents/MacOS/RStudio &
  fi
}

That is, I have two commands crun and crunr, which each take an environment name as an argument. So, I can do stuff like:

## launch RStudio with **osx-arm64** environment
crunr r43

## launch RStudio with **osx-64** environment
crunr r43_x86

Upvotes: 9

Dongdong Kong
Dongdong Kong

Reputation: 416

Making a soft link works for me with RStudio v2023.09.1:

sudo ln -s /opt/miniconda3/envs/r-4.3/bin/R /usr/bin/
sudo ln -s /opt/miniconda3/envs/r-4.3/lib/R/lib/libR.so /usr/lib/

Upvotes: 1

joelostblom
joelostblom

Reputation: 48879

There is now a version of RStudio in the conda forge repo, which can be used to install the more recent versions of RStudio (such as 2022.12.0).

conda install -c conda-forge rstudio-desktop

Note that when you are creating your R conda environment, it is important that you only use packages from the same channel and don't mix e.g. conda-forge with the default channel (this can lead to errors such as unable to load shared object '/home/joel/miniconda3/envs/r/lib/R/library/ragg/libs/ragg.so' (also happens with stringi often)). One way to insure that channel mixing does not happen is to specify the channel with -c and also use --override-channels to prevent that the default channels are searched at all:

conda create -n r --override-channels -c conda-forge r-base r-devtools rstudio-desktop

Upvotes: 3

Manuel F.
Manuel F.

Reputation: 255

if is up to any good (now)... conda has the package rstudioapi which brings Rstudio to your local environment, and picks up the local/default r-base installed of your active environment.

you can install it (once your environment is activated) by typing:

conda install -c conda-forge r-rstudioapi

then you just type (inside your environment): rstudio

Upvotes: 0

Piotr Migdal
Piotr Migdal

Reputation: 12792

See https://anaconda.org/r/rstudio:

$ conda install -c r rstudio

Then from command line:

$ rstudio

(It is how I installed it and it works.)

Upvotes: 18

Ray Donnelly
Ray Donnelly

Reputation: 4086

Update: The Anaconda Distribution now has packages for RStudio so you should be able to use that and not have to jump through any hoops at all. You can also install it directly the Anaconda Navigator.

Upvotes: 1

O.rka
O.rka

Reputation: 30677

Update: ADD THIS TO ~/.bash_profile !

export RSTUDIO_WHICH_R="/Users/jespinoz/anaconda/bin/R"
launchctl setenv RSTUDIO_WHICH_R $RSTUDIO_WHICH_R

Credits to @Z-Shiyi for the last line https://github.com/conda/conda/issues/3316#issuecomment-241246755


An addition to what @Ray Donnelly said above. Basically, it has to be executed from the correct environment (i.e. run it from the terminal).

You can either:

(A) Put this in your ~/.bash_profile export RSTUDIO_WHICH_R=/Users/[yourusername]/anaconda/bin/R (if youre using conda but you could put any R path)

(B) then type this in the terminal after it's been sourced (either restart terminal or do source .bash_profile): open -a RStudio That should work.

or you can do what I did:

(A) open up automator (sorry if you're not on a mac; this will only work on mac)

(B) use a Run Shell Script

(C) then delete cat that's already in there and put in: export RSTUDIO_WHICH_R=/Users/[yourusername]/anaconda/bin/R open -a RStudio

(D) Save it as something like run_rstudio.app then just run that and it should work: enter image description here

Upvotes: 11

Ray Donnelly
Ray Donnelly

Reputation: 4086

So long as which R shows up a working R interpreter (which it should do if you have installed the r package from conda and activated your environment) then launching rstudio from that same environment should pick it up just fine.

For a test, on ArchLinux, I built and installed: https://aur.archlinux.org/packages/rstudio-desktop-git/

.. then force removed the R interpreter (pacman -Rdd r), then installed r from conda (conda install -c r r) and it worked fine. I then closed my terminal and opened a new one (so that the correct conda environment was not activated and successfully launched RStudio with the following command: RSTUDIO_WHICH_R=/home/ray/r_3_3_1-x64-3.5/bin/R rstudio

I think the crux is to launch RStudio from the right environment? Your ~/.bash_profile and ~/.bashrc are only sourced when you run bash. For environment variables to be set so that the your desktop environment knows about them, on Linux, you should put them in ~/.profile or else in /etc/pam.d (you may need to logout or shutdown after making those changes) and on OS X, you should check out https://apple.stackexchange.com/q/57385

Upvotes: 12

Related Questions