R CMD defaults to R version selection screen in Linux terminal - r

When I enter R CMD ... in my Linux terminal, I receive a dialog to choose the version of R I'd like to run:
$ R CMD
Available Versions:
------------------------
3.4.0
3.4.1
3.4.2
3.4.4
3.5.0
3.5.1
3.5.2
3.5.3
3.6.0
3.6.1
4.0.2
Enter The R Version You'd Like To Run:
Is there an easy fix to this from the command line?
Issue recognized by #MrFlick below
The choice of R versions comes from a non-standard wrapper around the R command:
$ which R
alias R='/opt/rVersionSelect.sh'
/opt/rVersionSelect.sh
Here is the script, in which you can see the selection mechanism:
$ head -500 /opt/rVersionSelect.sh
#!/bin/bash
RDIR=/opt/R
echo "Available Versions:"
echo "------------------------"
for rVer in $RDIR/* ; do
echo $(echo $rVer | cut -d- -f2)
done
read -n 5 -p "Enter The R Version You'd Like To Run: " RunThisVersion
if [ -d $RDIR/R-$RunThisVersion/ ] ; then
echo "Loading $RunThisVersion...."
wait 10
clear
$RDIR/R-$RunThisVersion/bin/R --interactive
else
echo "Unrecognized Version: $RunThisVersion"
fi
As it turns out, the R in R CMD needs to point to one of the many R versions installed. For example, /opt/R/R-3.5.3/bin/R CMD.

For the R in R CMD, it looks like I need to point to one of the many installations of R. For example, /opt/R/R-3.5.3/bin/R CMD INSTALL rJava gives me the same result in the Terminal as using Build, Install and Restart in Rstudio.
When I try to write a custom build script like R CMD javareconf, it fails to execute in RStudio. But /opt/R/R-3.5.3/bin/R CMD javareconf successfully executes in the Terminal.

The way I've dealt with it is to go into the RStudio instance, open a project, and Configure Build Tools. Then I could achieve the same objectives using options from the Build menu. I think this question has merit, because difficult R package installations will suggest using R CMD ... to make the installation work.

Related

R: command not found

I want to install rJava but this fails with the following suggestion:
Make sure you have Java Development Kit installed and correctly registered in R.
If in doubt, re-run "R CMD javareconf" as root.
ERROR: configuration failed for package ‘rJava’
* removing ‘/Library/Frameworks/R.framework/Versions/4.0/Resources/library/rJava’
* restoring previous
‘/Library/Frameworks/R.framework/Versions/4.0/Resources/library/rJava’
Hence, I follow this suggestion (and the one everyone else seems to suggest) and run R CMD javereconf in the terminal but now I get the message that zsh: command not found: R.
How can I get R CMD javereconf to work?
Thanks.
EDIT: While I followed the blogpost suggested by #Till, I still struggle to run R CMD javereconf (same error). In the meantime, I figured that I should mention that I'm using MacOS Big Sur with an Apple M1 Chip.
When typing R.home() RStudio returns /Library/Frameworks/R.framework/Resources/R.
There is currently a bug in CRAN's R installation package that results in it not correctly installing symbolic links to R and Rscript for commandline use. I've just verified this by inspecting the postflight script in their 4.0.5 installation package.
Basically, there's a problem wherein the uname check doesn't know about operating system releases of 20 and above.
If your uname -r release version is over 20, this would explain why the installation silently failed. My recommendation would be to manually create the symbolic links the package is supposed to create by doing something like this:
if uname -r | grep '^2' >/dev/null; then
## 15.0 and higher don't allow messing with /usr/bin
## so use /usr/local/bin instead
if [ ! -e /usr/local/bin ]; then
mkdir -p /usr/local/bin
fi
cd /usr/local/bin
# create convenience links to R and Rscript
rm -f R Rscript
ln -s /Library/Frameworks/R.framework/Resources/bin/R .
ln -s /Library/Frameworks/R.framework/Resources/bin/Rscript .
fi
I'm going to file a bug with R-project shortly.
Find the location of R and Rscript on your disk by running R.home() on Rstudio's R console.
Then, follow this guide and copy the returned location from R.home() and paste on a new line on /etc/paths.
Then, restart the terminal and run R CMD javareconf

Change R version of the shell on Mac

my R version on the zsh shell is the 3.6 that is different respect to the one of RStudio that is the 4. In fact, using the Jupyter notebook, the R version is the one used by conda. In fact, to install an r package I must run conda install -c r r-package_name. So, this is very annoying. How can I link the R version of Rscript of the shell with the one of the Rstudio?

Write a file using `saveRDS()` so that it is backwards compatible with old versions of R

My compute cluster recently updated to R version R 3.6.0 and deleted the old versions of R. I had been running my project in R 3.4.0. I decided this would be fine, ran some code, and saved the output during my R 3.6.0 session as:
saveRDS(output, output.path)
This file was then transferred to a different computer where interactive R use takes place. This computer runs R/3.4.0, and updating the version of R is not an option. When I go to open the above file, I get the following error:
readRDS(output.path)
Error in readRDS(output.path) : cannot read workspace version 3 written by R 3.6.0; need R 3.5.0 or newer
This is a bummer. I am not the system administrator on either of these computers, so I can't just synchronize the versions. This is my question:
Is there a way to write a file using saveRDS() in R 3.6.0 such that it would be backward compatible in an R 3.4.0 environment?
Expanding on my comment with a demo:
$ Rscript --version | head -1
R scripting front-end version 3.6.0 (2019-04-26)
$ Rscript -e 'saveRDS(1:10, file="foo.rds")'
$
$ docker run --rm -ti r-base:3.4.0 Rscript --version | head -1
R scripting front-end version 3.4.0 (2017-04-21)
$ docker run --rm -ti -v ${PWD}:/work -w /work r-base:3.4.0 Rscript -e 'print(readRDS("foo.rds"))'
Error in readRDS("foo.rds") :
cannot read workspace version 3 written by R 3.6.0; need R 3.5.0 or newer
Calls: print -> readRDS
Execution halted
$
$ Rscript -e 'saveRDS(1:10, file="foo.rds", version=2)'
$ docker run --rm -ti -v ${PWD}:/work -w /work r-base:3.4.0 Rscript -e 'print(readRDS("foo.rds"))'
[1] 1 2 3 4 5 6 7 8 9 10
$
I use my normal R version which I show to be 3.6.0, and I then launch R 3.4.0 via Rocker, also showing its version.
As expected, it first fails -- and once the data is resaved with version=2 it works as is should.
As I understand from here you have to set version = 2 in saveRDS:
Serialization format version 3 becomes the default for serialization
and saving of the workspace (save(), serialize(), saveRDS(),
compiler::cmpfile()). Serialized data in format 3 cannot be read by
versions of R prior to version 3.5.0. Serialization format version 2
is still supported and can be selected by version = 2 in the
save/serialization functions. The default can be changed back for the
whole R session by setting environment variables
R_DEFAULT_SAVE_VERSION and R_DEFAULT_SERIALIZE_VERSION to 2. For
maximal back-compatibility, files ‘vignette.rds’ and ‘partial.rdb’
generated by R CMD build are in serialization format version 2, and
resave by default produces files in serialization format version 2
(unless the original is already in format version 3).

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

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
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.)
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
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:
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 Alias 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.
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.
Making a soft link works for me:
ln -s /opt/miniconda3/envs/r-4.2/bin/R /usr/bin/R
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

Roxygen, package building, and use.Rd2=TRUE

I have a simple shell script that builds my Roxygen documents, builds the package, checks, then installs the newly built package on my machine. It's quite simple:
#! /bin/sh
R CMD roxygen -d myPackage
R CMD build myPackage/
R CMD check myPackage_0.01.tar.gz
R CMD INSTALL myPackage myPackage_0.01.tar.gz
But I'm having issues with Roxygen picking up my .onLoad() function as described previously on StackOverflow. The solution is to use the use.Rd2=TRUE option with roxygenize. Well I want to build from the command prompt so I changed this line
R CMD roxygen -d myPackage
to the following line which shoves a roxygenize line to R through the stdin:
echo 'require("roxygen"); roxygenize("myPackage", roxygen.dir="myPackage",
copy.package=FALSE, use.Rd2=TRUE)' | R --no-save < /dev/stdin
This seems to work just dandy. But it feels a little convoluted. Is there an easier and/or more elegant way?
I do something similar, but I use a HERE document in the shell script to make it look cleaner.
#!/bin/sh
##
##
## Must be run from the parent of the package directory (no options
## to change target of check or tarball!?!)
PACKAGE="analyzeNMON"
VERSION=$(awk -F": +" '/^Version/ { print $2 }' ${PACKAGE}/DESCRIPTION)
R --no-restore --slave <<EOR
library(roxygen)
roxygenize(package.dir="${PACKAGE}",
roxygen.dir="${PACKAGE}",
use.Rd2=TRUE,
overwrite=TRUE,
copy.package=FALSE,
unlink.target=FALSE)
EOR
R CMD build ${PACKAGE}
R CMD check ${PACKAGE}_${VERSION}.tar.gz
R CMD INSTALL ${PACKAGE}_${VERSION}.tar.gz
The R code is very similar to that in the script run during R CMD roxygen.
The roxygen that is installed on my system (version 0.1; installed from CRAN this week) doesn't seem to support the -s option mentioned above...
May be the R CMD roxygen -s option will help here. I believe it is effectively the same as setting use.Rd2=TRUE in the roxygenize function.

Resources