Shell PATH to R under macOS Big Sur - r

I have a problem with R CMD under Big Sur. I installed R via default .pkg (not via brew) and I have been using zsh (Z shell) as the default shell. I think that I have a problem with PATH that I am not able to fix.
In Terminal:
~ echo $0
zsh
~ echo $PATH
/Library/Frameworks/Python.framework/Versions/3.9/bin:/Library/Frameworks/Python.framework/Versions/3.9/bin:/Users/vanderleidebastiani/.jenv/bin:/opt/miniconda3/bin:/opt/miniconda3/condabin:/opt/local/bin:/opt/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin:/opt/X11/bin:/Library/Apple/usr/bin:/Library/Frameworks/R.framework/Resources:/Library/Frameworks/R.framework/Resources
~ R CMD
looks ok
In R/RStudio:
> system("echo $0")
sh
> system("echo $PATH")
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/Library/TeX/texbin:/opt/X11/bin:/Library/Apple/usr/bin:/opt/local/bin:/Applications/RStudio.app/Contents/MacOS/postback
> system("R CMD")
sh: R: command not found
Warning message:
In system("R CMD") : error in running command
I can solve this temporarily with Sys.getenv if I include the PATH to "/Library/Frameworks/R.framework/Resources"
Sys.getenv("PATH")
"/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/Library/TeX/texbin:/opt/X11/bin:/Library/Apple/usr/bin:/opt/local/bin:/Applications/RStudio.app/Contents/MacOS/postback"
Sys.setenv(PATH="/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/Library/TeX/texbin:/opt/X11/bin:/Library/Apple/usr/bin:/opt/local/bin:/Applications/RStudio.app/Contents/MacOS/postback:/Library/Frameworks/R.framework/Resources")
system("R CMD")
-looks ok
I tried to include the PATH permanently, so I edited .bash_profile, .profile, .zshrc, .bashrc in my home folder (in case "/Users/vanderleidebastiani") to include:
# Setting PATH for R
export PATH="$PATH:/Library/Frameworks/R.framework/Resources"
However, it does not make any effect. The PATH does not recognize after I restart R, it goes back to initial settings.
Do I need to include PATH in another file or I made some mistake in the way to include it?
Best, Vanderlei

To create a new path just do this in MACOS Big Sur
sudo touch ~/.zshrc
sudo nano export PATH=$PATH:/YOUR PATH
crtl + x and save
source ~/.zshrc or close and open the terminal
Check with echo $PATH

I had the same problem but with access to homebrew installed binaries. The fix I found was to modify the global PATH variable in Big Sur.
It turns out if you drop a text file into the /etc/paths.d folder it will be parsed and added to PATH even after a restart of RStudio.
I used sudo nano /etc/paths.d/homebrew to add a text file and pasted in /opt/homebrew/bin and /opt/homebrew/sbin on separate lines. After saving and closing I rebooted RStudio and ran echo $PATH from RStudio's terminal to check that the path had been imported correctly.
Ref:
https://serverfault.com/questions/16355/how-do-i-set-the-global-path-environment-variable-on-os-x

Related

zsh: command not found for all installed programs

I'm using zsh as my shell on mac OS M1 Pro Monterrey. When I echo my path I get
/Users/vcui/bin:/usr/local/bin:/Users/vcui/bin:/usr/local/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/bin:/Users/vcui/.pyenv/shims:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
However typing in any commands like node or nvm gives me "zsh: command not found". My ~/.zshrc starts with
# If you come from bash you might have to change your $PATH.
export PATH=$HOME/bin:/usr/local/bin:$PATH
# Path to your oh-my-zsh installation.
export ZSH="$HOME/.oh-my-zsh"
I've also first sourced my ~/.zshrc before running node or nvm.
Since I have the PATH specified in my ~/.zshrc, why are all of the program commands unrecognized?

Rscript not working with packaged R for AWS Lambda

I'm trying to run an R script on the command line of an AWS EC2 instance using packaged R binaries and libraries (without installation) -- the point is to test the script for deployment to AWS Lambda. I followed these instructions. The instructions are for packaging up all the R binaries and libraries in a zip file and moving everything to a Amazon EC2 instance for testing. I unzipped everything on the new machine, ran 'sudo yum update' on the machine, and set R's environment variables to point to the proper location:
export R_HOME=$HOME
export LD_LIBRARY_PATH=$HOME/lib
NOTE: $HOME is equal to /home/ec2-user.
I created this hello_world.R file to test:
#!/home/ec2-user/bin/Rscript
print ("Hello World!")
But when I ran this:
ec2-user$ Rscript hello_world.R
I got the following error:
Rscript execution error: No such file or directory
So I checked the path, but everything checks out:
ec2-user$ whereis Rscript
Rscript: /home/ec2-user/bin/Rscript
ec2-user$ whereis R
R: /home/ec2-user/bin/R /home/ec2-user/R
But when I tried to evaluate an expression using Rscript at the command line, I got this:
ec2-user$ Rscript -e "" --verbose
running
'/usr/lib64/R/bin/R --slave --no-restore -e '
Rscript execution error: No such file or directory
It seems Rscript is still looking for R in the default location '/usr/lib64/R/bin/R' even though my R_HOME variable is set to '/home/ec2-user':
ec2-user$ echo $R_HOME
/home/ec2-user
I've found sprinkles of support, but I can't find anything that addresses my specific issue. Some people have suggested reinstalling R, but my understanding is, for the purposes of Lambda, everything needs to be self-contained so I installed R on a separate EC2 instance, then packaged it up. I should mention that everything runs fine on the machine where R was installed with the package manager.
SOLUTION: Posted my solution in the answers.
It thinkt it is staring at you right there:
ec2-user$ whereis R
R: /home/ec2-user/bin/R /home/ec2-user/R
is where you put R -- however it was built for / expects this:
ec2-user$ Rscript -e "" --verbose
running
'/usr/lib64/R/bin/R --slave --no-restore -e '
These paths are not the same. The real error may be your assumption that you could just relocate the built and configured R installation to a different directory. You can't.
You could build R for the new (known) path and install that. On a system where the configured-for and installed-at path are the same, all is good:
$ Rscript -e "q()" --verbose
running
'/usr/lib/R/bin/R --slave --no-restore -e q()'
$
This blog post walks through a similar problem and offers a potential solution. I also had to implement part of the solution from this post.
I changed the very first line of R's source code from this:
#!/bin/sh
# Shell wrapper for R executable.
R_HOME_DIR=${R_ROOT_DIR}/lib64${R_ROOT_DIR}
To this:
R_HOME_DIR=${RHOME}/lib64${R_ROOT_DIR}
I'll explain why below.
NOTE -- The rest of the code is:
if test "${R_HOME_DIR}" = "${R_ROOT_DIR}/lib64${R_ROOT_DIR}"; then
case "linux-gnu" in
linux*)
run_arch=`uname -m`
case "$run_arch" in
x86_64|mips64|ppc64|powerpc64|sparc64|s390x)
libnn=lib64
libnn_fallback=lib
;;
*)
libnn=lib
libnn_fallback=lib64
;;
esac
if [ -x "${R_ROOT_DIR}/${libnn}${R_ROOT_DIR}/bin/exec${R_ROOT_DIR}" ]; then
R_HOME_DIR="${R_ROOT_DIR}/${libnn}${R_ROOT_DIR}"
elif [ -x "${R_ROOT_DIR}/${libnn_fallback}${R_ROOT_DIR}/bin/exec${R_ROOT_DIR}" ]; then
R_HOME_DIR="${R_ROOT_DIR}/${libnn_fallback}${R_ROOT_DIR}"
## else -- leave alone (might be a sub-arch)
fi
;;
esac
fi
if test -n "${R_HOME}" && \
test "${R_HOME}" != "${R_HOME_DIR}"; then
echo "WARNING: ignoring environment value of R_HOME"
fi
R_HOME="${R_HOME_DIR}"
export R_HOME
You can see at the bottom, the code sets R_HOME equal to R_HOME_DIR, which it originally assigned based on R_ROOT_DIR.
No matter what you set the R_HOME_DIR or R_HOME variable to, R resets everything using the R_ROOT_DIR variable.
With the change, I can set all my environment variables:
export RHOME=$PWD/R #/home/ec2-user/R
export R_HOME=$PWD/R #/home/ec2-user/R
export R_ROOT_DIR=/R #/R
I set RHOME to my working directory where the R package sits. RHOME basically acts as a prefix, in my case, it's /home/ec2-user/.
Also, Rscript appends /R/bin to whatever RHOME is, so now I can properly run...
Rscript hello_world.R
...on the command line. Rscript knows where to find R, which knows where to find all it's stuff.
I feel like packaging up R to run in a portable self-contained folder, without using Docker or something, should be easier than this, so if anyone has a better way of doing this, I'd really appreciate it.
Another more quickly method:
create same folder /usr/lib/R/bin/
then put R into this folder.

Rscript: command not found

I'm working with R for a while, and I always worked with Rstudio, I tried just now to run a Rscript command in terminal (I have a mac..) and I got this error-
>Rscript script.R
-bash: Rscript: command not found
when I tried to open R in the terminal I go the same error-
>R
-bash: R: command not found
I can run R code with the Rstudio and the R application, but I know there is a way to run R throw the terminal.
Did I miss something when I installed R on my computer? do I need to add R to my PATH?
thanks in advance!
Steps to run R script through Windows command prompt
Set the PATH variable for Rscript.exein the environment variables. Rscript.exe can be found inside bin folder of R. Set the path for Rscript.exe to use Rscript command in Windows command prompt. To check if Rscript.exe has been set environmentally or not, type Rscript in command prompt. The follwoing message should come.
Go to Command Prompt, set the path where your .R file is there.
Run the following command: Here abcd.R is present under Documents folder. So I set path and then run Rscript abcd.R
For those who stumbled upon this but use a mac, you might find this useful. I recently downloaded and installed R and RStudio through the CRAN site. I didn't do it through homebrew. Since I downloaded this install directly from the site, it DID NOT add the RScript executable to my /usr/local/bin directory.
I have locate on my mac so I did a quick lookup:
locate RScript
And I found it here:
/Library/Frameworks/R.framework/Versions/4.0/Resources/bin/Rscript
What I had to do was create a symbolic link to my /usr/local/bin directory to get it to work:
cd /usr/local/bin
ln -s /Library/Frameworks/R.framework/Versions/4.0/Resources/bin/Rscript Rscript
Now I'm able to run Rscript through the command line. This may help someone else out there.

How to set or change the default software path in R

I have a program called "bowtie2" and I changed the default path in terminal:
$export PATH=/opt/NGS/bowtie2-2.2.8:/usr/local/sbin:/usr/local/bin:/usr/sbin
$bowtie2 --version
/opt/NGS/bowtie2-2.2.8/bowtie2-align-s version 2.2.8
But when I call it from R it's still the old version from other path:
> bowtie2="bowtie2"
> system2(bowtie2,"--version",stdout=TRUE)[1]
[1] "/usr/bin/bowtie2-align version 2.1.0"
My question is how to change the default path for the program in R (so that the version is "/opt/NGS/bowtie2-2.2.8/bowtie2-align-s version 2.2.8" rather "/usr/bin/bowtie2-align version 2.1.0")
You need to add export PATH=/opt/NGS/bowtie2-2.2.8:/usr/local/sbin:/usr/local/bin:/usr/sbin to the end of .bashrc(linux) or .bash_profile(mac) file in your home folder.
vi ~/.bash_profile
And then add the export command at the end of the file, otherwise it will only work for your current shell session. When you use system command in R, it launches a new shell session, which the export command won't apply to.
See this link to find out more about export command

Trying to install Theos

I'm trying to install Theos on my iMac using the instructions from HERE.
It seems to have installed correctly, but when I try to set up a new project in terminal using:
$THEOS/bin/nic.pl
It says:
-bash: /bin/nic.pl: No such file or directory
Any help would be great!
You should unhide your OSX hidden files using this command through TERMINAL :
defaults write com.apple.finder AppleShowAllFiles TRUE; killall Finder
Then open .bash_profile under your USERNAME folder
[edit]
if .bash_profile doesn't existe
Open TERMINAL then write
touch .bash_profile
Then
open -a TextEdit.app .bash_profile
Then add this line inside
export THEOS=/opt/theos
Good luck
If you followed the guide of iphonedevwiki you should have all that you need to use theos. The correct way to call nic.pl should be:
sudo su
/opt/theos/bin/nic.pl

Resources