zsh: command not found: R on terminal using Big Sur Mac - r

I installed R from official cran website and i can run R from the Rstudio but when i try to use R from the terminal, I get the following results:
(base) ege#Eges-MBP ~ % R
zsh: command not found: R
(base) ege#Eges-MBP ~ % RScript
zsh: command not found: RScript
How can i enable the command R and run RScript from terminal on my mac?

This is possibly due to a bug with the current version of CRAN's R installation package (present as of version 4.0.5), that affects certain versions of Big Sur. I've answered this question here: R: command not found
In short, if running uname -r gives you a release number greater than 20, then you may need to manually create symbolic links that the package installer failed to.
In short:
sudo -s
mkdir -p /usr/local/bin
cd /usr/local/bin
rm -f R Rscript
ln -s /Library/Frameworks/R.framework/Resources/bin/R .
ln -s /Library/Frameworks/R.framework/Resources/bin/Rscript .

I found a way to do this.
I first found the location of R and Rscript on my disk by running R.home() on my R console.
Then, i followed this guide and copied the returned location from R.home() and pasted on a new line on /etc/paths.
Then, i restarted the terminal and i was able to use R and Rscript commands

I had a similar issue. I was able to solve it by adding the below line (path obtained by running R.home() inside R) to my .zshrc. Thanks for the guidance.
export PATH="/Library/Frameworks/R.framework/Resources:${PATH}"

You need to add the directory where the R binaries are installed to the PATH environment variable.
You can add this in the current session as follows:
PATH=/usr/local/bin/:$PATH
To have this done automatically in every new session ensure that this line is added to your ~/.zshrc file:
export PATH="/usr/local/bin/:${PATH}"

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

Rstudio does not start "Unable to determine real path of R script" due to previous error during compilation of R

I am using Fedora 32, I have R (3.5.1 ) within conda. I also compiled R 4.0.0 from source but since I was having another problem with Rstudio I removed this version trying to solve these issues. (With 4.0.0 I couldn't install packages because I got an 'C++11 standard requested but CXX11 is not defined' error, I made the mistake of using --with-x=no during that compiling)
Now I tried to either compile a new version (4.0.1) or get R through yum, but every time I try to reinstall Rstudio I get this error:
Unable to determine real path of R script /home/andrespara/R-4.0.0/bin/R (system error 2 (Folder doesn'exist*))
I removed ~/.config/rstudio ~/.local/share/rstudio ~/.rstudio/ every time I removed/reinstalled Rstudio. I also searched for help in the rstudio community forums. I also
I compiled 4.0.1 with this line (deactivating conda before this avoided the X11 error that I had before)
./configure \
--prefix=/opt/R/${R_VERSION} \
--enable-memory-profiling \
--enable-R-shlib \
--with-blas \
--with-lapack
I added symbolic links following these instructions https://docs.rstudio.com/resources/install-r-source/
sudo ln -s /opt/R/${R_VERSION}/bin/R /usr/local/bin/R
sudo ln -s /opt/R/${R_VERSION}/bin/Rscript /usr/local/bin/Rscript
R 4.0.1 is now correctly installed, I even used it today and installed some package the only missing link is with Rstudio and its installation that doesn't recognize it.
My question is how to either jump to another version of R working with Rstudio and get rid of that message when Rstudio starts.
I should add can't even start Rstudio because it still asks for the 'broken version' even when I tried to reinstall it several times.
I managed to solve this thanks to a colleague who found a workaround.
First running RSTUDIO_WHICH_R=$(which R) and then rstudio in the console bypassed the first error.
conda deactivate
RSTUDIO_WHICH_R=$(which R)
rstudio
Then knowing the launcher was the error and thanks to this answer https://askubuntu.com/a/112259/265501 I went to /usr/share/applications
and edited the line pointing to the useless binary with the correct one in the rstudio.desktop file found there.
Edit the file ~/.profile
export RSTUDIO_WHICH_R=$(which R)
The problem is that you've created a symlink from the wrong location. Rstudio launcher is looking for R in /home/andrespara/R-4.0.0/bin/R but it can't find it there. Your symlinks should be redirecting from that location to where you installed R like this:
sudo ln -s /opt/R/${R_VERSION}/bin/R home/andrespara/R-4.0.0/bin/R
sudo ln -s /opt/R/${R_VERSION}/bin/Rscript home/andrespara/R-4.0.0/bin/Rscript
If any of the directories in that file path don't exist, you'll have to create them as well.

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

How to uninstall R completely from OS X

I'm on Mac OS X 10.11.5, R version 3.3.1
I followed the instructions to remove R:
http://cran.r-project.org/doc/manuals/r-release/R-admin.html#Uninstalling-under-OS-X
In command line:
sudo rm -rf /Library/Frameworks/R.framework /Applications/R.app \
/usr/bin/R /usr/bin/Rscript
I no longer see R in applications or anywhere else on my computer. However, I am still able to run R from the command line.
I am mystified. What is going on? How do I get rid of R completely? I want a fresh start.
Use which R. It will give you the location where R is running so that you can remove it from there.

Painless way to install a new version of R?

Andrew Gelman recently lamented the lack of an easy upgrade process for R (probably more relevant on Windows than Linux). Does anyone have a good trick for doing the upgrade, from installing the software to copying all the settings/packages over?
This suggestion was contained in the comments and is what I've been using recently. First you install the new version, then run this in the old verion:
#--run in the old version of R
setwd("C:/Temp/")
packages <- installed.packages()[,"Package"]
save(packages, file="Rpackages")
Followed by this in the new version:
#--run in the new version
setwd("C:/Temp/")
load("Rpackages")
for (p in setdiff(packages, installed.packages()[,"Package"]))
install.packages(p)
Just for completeness, there are some ways to prevent you from having this problem. As Dirk said, save your packages in another directory on your computer.
install.packages("thepackage",lib="/path/to/directory/with/libraries")
You can change the default .Library value using the function .libPaths too
.libPaths("/path/to/directory/with/libraries")
This will put this path as a first value in the .Library variable, and will make it the default.
If you want to automate this further, you can specify this in the Rprofile.site file, which you find in the /etc/ directory of your R build. Then it will load automatically every time R loads, and you don't have to worry about that any more. You can just install and load packages from the specified directory.
Finally, I have some small code included in my Rprofile.site allowing me to reinstall all packages when I install a new R version. You just have to list them up before you update to the new R version. I do that using an .RData file containing an updated list with all packages.
library(utils)
## Check necessary packages
load("G:\Setinfo\R\packagelist.RData") # includes a vector "pkgs"
installed <- pkgs %in% installed.packages()[, 'Package']
if (length(pkgs[!installed]) >=1){
install.packages(pkgs[!installed])
}
I make the packagelist.RData by specifying .Last() in my Rprofile.site. This updates the package list if I installed some :
.Last <- function(){
pkgs <- installed.packages()[,1]
if (length(pkgs) > length(installed)){
save(pkgs,file="G:\Setinfo\R\packagelist.RData")
}
}
When I install a new R version, I just add the necessary elements to the Rprofile.site file and all packages are reinstalled. I have to adjust the Rprofile.site anyway (using sum contrasts, adding the extra code for Tinn-R, these things), so it's not really extra work. It just takes extra time installing all packages anew.
This last bit is equivalent to what is given in the original question as a solution. I just don't need to worry about getting the "installed" list first.
Again, this doesn't work flawless if you have packages that are not installed from CRAN. But this code is easily extendible to include those ones too.
If you are using Windows, you might want to use the installr package:
install.packages("installr")
require(installr)
updateR()
The best way of doing this is from the RGui system. All your packages will be transferred to the new folder and the old ones will be deleted or saved (you can pick either).
Then once you open RStudio again, it immediately recognizes that you are using an updated version. For me this worked like a charm.
More info on installr here.
Two quick suggestions:
Use Gabor's batchfiles which are said to comprise tools helping with e.g. this bulk library relocations. Caveat: I have not used them.
Don't install libraries within the 'filetree' of the installed R version. On Windows, I may put R into C:/opt/R/R-$version but place all libraries into C:/opt/R/library/ using the following snippet as it alleviates the problem in the first place:
$ cat .Renviron # this is using MSys/MinGW which looks like Cygwin
## Example .Renviron on Windows
R_LIBS="C:/opt/R/library"
The method suggested above will not completely work if you have packages that are not from CRAN. For example, a personal package or a package downloaded from a non-CRAN site.
My preferred method on Windows (upgrading 2.10.1 to 2.11.0):
Install R-2.11.0
Copy R-2.10.0/library/* to R-2.11.0/library/
Answer "no" to the prompts asking you if it is okay to overwrite.
Start R 2.11.0
Run the R command update.packages()
With respect to the solution given in the question, it might not be easy to run your older version of R if you have already installed the new version. In this case, you can still reinstall all missing packages from the previous R version as follows.
# Get names of packages in previous R version
old.packages <- list.files("/Library/Frameworks/R.framework/Versions/3.2/Resources/library")
# Install packages in the previous version.
# For each package p in previous version...
for (p in old.packages) {
# ... Only if p is not already installed
if (!(p %in% installed.packages()[,"Package"])) {
# Install p
install.packages(p)
}
}
(Note that the argument to list.files() in the first line of code should be the path to the library directory for your previous R version, where all folders of packages in the previous version are. In my current case, this is "/Library/Frameworks/R.framework/Versions/3.2/Resources/library". This will be different if your previous R version is not 3.2, or if you're on Windows.)
The if statement makes sure that a package is not installed if
It's already installed in the new R version, or
Has been installed as a dependency from a package installed in a previous iteration of the for loop.
Following Dirk's suggestion, here is some R code to do it on windows: How to easily upgrade R on windows XP
Update (15.04.11): I wrote another post on the subject, explaining how to deal with common issues of upgrading R on windows 7
Two options:
Implement my answer here
If you use R under Eclipse with StatET, open Run Configurations, click on Console tab and in the box called R snippet run after startup add this line with your choice of directory: .libPaths("C:/R/library")
I am on Windows 8 and for some weird reason, I can never install packages using my internet connections.
I generally install it using the .zip file from CRAN.
After I went from R 3.2.5 to R 3.3.1.
I simply copied the packages from
C:\Path\to\packa\R\win-library\3.2 to C:\Path\to\packa\R\win-library\3.3.
And then I restarted the R session. Worked perfectly.
I haven't checked if ALL the packages are functioning well.
But, the ones I checked are working perfectly well.
Hope this hack works for everybody.
Cheers.
The accepted answer might work if you have foresight, but I had already gotten rid of the old version so wasn't able to follow these directions.
The steps described below worked for OSX upgrading from 2.1 and 3.1.
UPDATED: To get the directory for your most recent version (instead of typing in 3.1 or 3.2) you can use the below commands. The second one converts directly to the R-variable, skipping . and .. and .DS_Store, use:
OLD=$(ls -d /Library/Frameworks/R.framework/Versions/*.* |tail -n 2 | head -n 1)Resources/library/
echo "packages = c(\"`ls $OLD | tail +4| paste -s -d ',' - | sed -E 's|,|\",\"|'g`\")" | tr -d "/"
(Add |pbcopy to the end to copy it directly to your Mac clipboard)
Then within R you can paste that variable that is generated. Once that is defined in the new version of R, you can loop through the installed packages from the instructions above...
for (p in setdiff(packages, installed.packages()[,"Package"]))
install.packages(p, dependencies=TRUE, quiet=TRUE, ask=FALSE)
for me this page is good
https://www.r-statistics.com/2013/03/updating-r-from-r-on-windows-using-the-installr-package/
or
another option is just install the new option and at final you put, for example in windows in my pc
.libPaths(c(
"D:/Documents/R/win-library/3.2",
"C:/Program Files/R/R-3.2.3/library",
"C:/Program Files/R/R-3.2.0/library",
"D:/Documents/R/win-library/2.15"
)
every path of last version in my case i always put the first path is "D:/Documents/R/win-library/3.2" that is fixed
and then i put the other because you do not need copy or move any packages, in my sugest just call it
linux + bash + debian + apt users:
If you're installing/upgrading to the newest version of R, then we may assume you have root permissions. (Not essential, just makes the process a lot simpler; for consistency the script below uses sudo for all installs.)
As the R packages are also installed by root, it is thus permissible to place these in /usr/local/.
The call to curl below assumes you are already interested in the sid release of R, the very latest unstable version (as required when building/checking an R package) i.e.
cat /etc/apt/sources.list | grep 'sid' || exit 1
although this could easily be replaced with a recent stable release e.g. buster.
Note that I am not using a key as is typically recommended. This is not essential, particularly if (as in the script which follows) we install packages within R itself (Rscript -e below). Also, such keys have a tendency to break/change every few years. Thus, you are of course welcome to add the following preface to the file R.sh which follows:
sudo apt-key adv --keyserver keyserver.ubuntu.com \
--recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9
The array of R packages is clearly not exhaustive but gives some examples which I personally find useful. A fresh install/upgrade with the debian package r-recommended, as below, should give the latest version of all of the the standard 'recommended' packages (e.g. survival). I believe there may be a slight lag between a CRAN release and an update to the relevant debian package. Thus, you may wish to add some of these to the array below if having the latest version of a 'recommended' R package is essential.
The debian packages installed in the process below are also neither essential (for using r-base) nor exhaustive but provide a no. of 'add-ons' which are important for a reasonable no. of R packages.
Anyway... place the following in R.sh:
sudo apt update && sudo apt --yes full-upgrade
sudo apt install --yes libappstream4 curl
### ov1 = online version; lv1 = local version (i.e. currently installed)
ov1=$(curl --silent --url https://packages.debian.org/sid/r-base |
grep 'meta name=\"Keywords\"' |
grep --only-matching '[0-9].*[0-9]') ; echo $ov1
## command -v = print a description of COMMAND similar to the `type' builtin
## && = if prior command succeeds, then do; || = if prior fails, then do
command -v 'R --version' &&
lv1=$(R --version |
grep --only-matching '[0-9\.]*[0-9]' |
## || = otherwise
head -1) ||
lv1=0
## 'lt' = less than
if dpkg --compare-versions "$lv1" 'lt' "$ov1"
then ## declare -a = indexed array
declare -a deb1=('r-base' 'r-base-dev' 'r-recommended')
for i in "${deb1[#]}"
do sudo apt install --yes "$i"
done
fi
### certain Debian packages are required by 'R' so best have these first
sudo apt install --yes ccache libcairo2-dev libxml2-dev libcurl4-openssl-dev \
libssl-dev liblapack-dev libssl-dev
declare -a pkg1=('data.table' 'ggplot2' 'knitr' 'devtools' 'roxygen2')
## installing as 'root' so these are installed in
Rscript -e ".libPaths()[1]"
for i in "${pkg1[#]}"
do sudo Rscript -e "install.packages('$i', dependencies=TRUE)"
done
### other useful additions
sudo apt install --yes libblas-dev libboost-dev libarmadillo-dev \
jags pandoc pandoc-citeproc
sudo apt update && sudo apt full-upgrade
Then execute it, e.g. assuming in directory already: source R.sh.
Installing packages (whether debian or R) one-by-one in a loop from shell is somewhat inefficient, but allows for simpler tracing of errors, IMHO. May take some time depending on the no. of R packages, so maybe simplest to let run overnight...
In linux, Now it is very simple. Just make:
install.packages("ropenblas")
ropenblas::rcompiler()

Resources