Is there a way to toggle between versions of R on linux? - r

I have two versions of R on my linux machine (Ubuntu 18.04). I need R 4.0.2 to run an analysis that requires library (psych); and I need R 3.6.3 to run an afni analysis (3dLME). Right now, R 4.0.2 is in usr/bin/R and R 3.6.3 is usr/local/bin. However, "which R" will only return /usr/bin/R.
I have tried to add /usr/local/bin/R to my .bashrc with no luck. Is there a way to toggle back and forth between which R version that linux will use? Optimally, I would like Rstudio to use 4.0.2, but afni to use 3.6.3; but I am also fine with editing my .bashrc file when I need to use the different tools. Thanks!

See if this tutorial helps. It states, as part of the instructions,
If you want to override which version of R is used then you can set
the RSTUDIO_WHICH_R environment variable to the R executable that you
want to run against. For example:
export RSTUDIO_WHICH_R=/usr/local/bin/R Not that in order for RStudio
to see this environment variable when launched from the Ubuntu desktop
Applications menu (as opposed to from a terminal) it must be defined
in the ~/.profile file.

Related

Can renv be used to create a virtual environment with a specific version of R?

I recently installed R 4.0, after previously using relying R 3.6.3. To manage R repositories, I use Rstudio (currently 1.2.5042 on a Windows 10 machine). After upgrading to R 4.0, I opened a project from a few months ago, and realized that Rstudio is now, by default, using the newer version of R (and it's library folder). When running renv::restore(), renv attempts to re-install all libraries in the .lock file for the newer version of R, and I don't see any way to specify that I want to keep using R 3.6.3 and it's associated library.
Coming from a python background, I had assumed that renv would create a virtual environment that isolates both the interpreter and the libraries that the project uses (similar to how anaconda environments are created). However, after looking through the documentation and doing a few searches, I have found no reference to isolating a particular version of R. I have, however, found that Rstudio defaults to using the latest version of R, which is not necessarily the behaviour that I want.
I have tried using anaconda to manage an R environment. However, Anaconda relies on its own smaller repository of R packages, and many of the libraries I need are from researchers that house their code on GitHub.
Is there a way to create an R environment in which I can isolate both the R libraries and the version of R itself? Or, perhaps there is something I am missing about how environments with R/Rstudio are intended to be used?
You are correct that renv only manages the installed R packages, and not the R interpreter itself.
Depending on how you're using RStudio, you can still "fake" this by setting the RSTUDIO_WHICH_R environment variable. For example:
export RSTUDIO_WHICH_R=/path/to/R
rstudio
would tell RStudio to "bind" to the version of R specified by the RSTUDIO_WHICH_R environment variable.
For what it's worth, the ability to bind projects to a specific version of R is a feature of the professional editions of RStudio; however, it's not available in the open-source version. See here for more details.

How come XQuartz/X11 makes the file explorer called with tk_choose.files look like this?

MacOS: High Sierra, version 10.13.6
R version: 3.6.1
R studio verison: 1.2.1335
I have been using Anaconda to manage my R packages for a while now. However, I keep getting errors in regards to C and Fortran compilers when trying to install and run packages that depend on them through the Anaconda R studio distribution. Thus I started using the stand-alone version of R studio again. I use the tcltk package in pretty much all of my scripts to easily choose file paths to load multiple data sets etc. When using the tcltk::tk_choose.files() command through the Anaconda R studio distribution, the file explorer window looks like the standard file explorer in MacOS. However, when using the same command in the stand-alone version the file explorer looks like this when running through XQuartz/X11:
My question is why the file explorer suddenly looks this "old" and if there is a way to fix it so I can continue using tcltk? Otherwise are there any alternatives?
I have tried out rJava together with rChoiceDialogs but I cannot get the rChoiceDialogs::jchoose.files() to work.
I managed to solve the issue. Meaning the file explorer looks normal again. I uninstalled my R, RStudio, and XQuartz. I then reinstalled all three using homebrew following the guide posted here.

How to change R versions on Mac

I'm trying to use the legacy version of R to run a package on Mac.
On Windows there is a switch in the General tab. However, it is not found on Mac RStudio. I referred to this post: https://support.rstudio.com/hc/en-us/articles/200486138-Using-Different-Versions-of-R
And I do not quite understand the 3 options:
Run the installer from CRAN for the R version you want to be current
List item Use the RSwitch utility available at: http://r.research.att.com/
Update the R.framework/Versions/Current directory alias directly
using ln -s
Especially for RSwitch utility, how do I even open/use that?
Thanks!
This old question is again timely, with the move to R version 4.
There is an RSwitch.app available for MacOS 10.14+ that provides this functionality via a menu bar app. You can choose which of your R builds to use, integrating with the standard Mac GUI and with RStudio. (I've had trouble with the newest Mac GUI not working with R 3.6.3 but RStudio seems to work fine with both that and R 4.0.1 along with this RSwitch.app.) The help pages for Rswitch show what's going on underneath, if you prefer to work via a command line.
If you use a standard .pkg via GUI to install a newer version of R it will effectively forget the other versions. You can use the command
pkgutil --forget (path to package name)
with the .pkg to overcome that. (I've had trouble installing from a tarball instead; might just be my incompetence.) Installing an older version from a .pkg won't overwrite a newer version but in that case it might be safest to omit any attempt to instal older Tcl/Tk and Texinfo, selectable with the "Customize" button at the "Installation Type" stage of the installation. With security settings in MacOS you might have trouble opening some R .pkg files; the awkward workaround is to use Control/right/two-finger click on the file in the Finder, then select ‘Open With’ and ‘Installer’ from the menu that appears.
The version of RSwitch.app noted in the question is still available via a link from this page. This is evidently 32-bit as it is disabled on my Mac under OS 10.14.

Specify R for Jupyter Notebook with Anaconda Windows

I feel pretty comfortable working with R, and I want to get into Python through Anaconda.
Upon trying the Jupyter Notebook and finding it has compatibility with R, I really want to use it.
I'm having problem installing the R packages that don't come in R-essentials; and mainly because I noticed it uses a different R installation than the one I had before. Not only is this a different R installation, but it also uses a previous version of R. In my local installation I have updated to 3.2.3 but in the Anaconda environment for Jupyter I got 3.1.
I also found a post to change the .libPaths variable to include the packages that I had already installed. Still, I see this as a potential problem because of the different R versions.
I wanted to know if I can update the R version that's used in Anaconda, or if I can point to the one that's installed locally.
Thank you.
You can install IRkernel in the normal R installation and then register the kernel: simply follow the instructions at http://irkernel.github.io/installation/

Compile an older R version and switch between versions in Ubuntu and RStudio

I just made the switch from Windows to Ubuntu. In windows I had the possibility to change the R version used in Rstudio IDE with Tools -> Global options -> R versions.
This is not possible in Ubuntu in the same way, indeed the R versions menu does not appear in Ubuntu version of RStudio (why??). Could you help me in finding the good solution for switching among different, already installed versions of R, in Ubuntu?
The switch among different versions is important for satisfying packages modifications and making old scripts working.
I apologize was not a great question, but more of the kind: "please help me, I'm desperate, I've loose all my Windows habs!" ;)
So if anyone like me want to make the switch from Windows to Ubuntu here is how you can compile an older R version and switch between versions using RStudio. As already #Andrie commented out here are some informations, however not complete https://support.rstudio.com/hc/en-us/articles/200486138-Using-Different-Versions-of-R.
You already have the R version provided by Ubuntu Software Center or a more recent version but you need an older version to run an old important script?
First you need to compile the desired R version from source. In Ubuntu updating R to a newer version leave you with one only version (the updated ones) and not two the older and the new like in Win.
Let's go to the shell and install all you need to build R from source
sudo apt-get build-dep r-base
to install all libraries you will need for compiling. (look also here http://cran.us.r-project.org/bin/linux/ubuntu/)
Download the version you need on CRAN
/bin/linux/ubuntu/MyUbuntu(trusty,precise,lucid) The archive you
need is something like:
r-base_TheVersionIWant.orig.tar.gz
Extract it in a directory (maybe ~/R)
then (in the shell) enter the top directory of the unpacked archive
then you can read the INSTALL file, and run configure with the --enable-R-shlib option (otherwise you will not be able once compiled to run it in RStudio)
./configure --enable-R-shlib && make
then following the INSTALL file
make check
make pdf
make info
If all was ok, you can tell R studio where find the bin/R file it needs
export RSTUDIO_WHICH_R= myPahtTo/bin/R
and fire rstudio with
rstudio
You have an RStudio version running the desired R Version.
When you will close the terminal and the rstudio session you will come back to the system default version of R. Then repeat just the last two lines to come back when you want.

Resources