Rscript: command not found - r

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.

Related

R script "command not found?"

I am trying to call an Rscript I created in Rstudio in Git, but it says "Rscript not found"
$ Rscript processDineSafe
bash: Rscript: command not found
I have made sure I am in the right directory, and also that my directory has the file I am trying to call. I also tried to make sure R was added to the path but I wasn't sure exactly how to confirm that.
I am new to git and R so any help would be greatly appreciated!

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.

Permissions-error using bash-script to install a library in R

I am running a software called CNV-Seq in a Bash script but, I do not have permission to run the following command:
R CMD INSTALL cnv/
The error is
* installing to library '/share/apps/r/3.2.2/intel/lib64/R/library'
Error: ERROR: no permission to install to directory '/share/apps/r/3.2.2/intel/lib64/R/library'
How can I solve this problem without gaining permission.
One approach is creating the bash script that executes R script.
So, the R script will contain:
install.packages("package_name")
Save it as xyz.r
and the bash script will contain:
R -f path_to/xyz.r
Save that as abc.
Run the bash script like this:
bash abc

Rscript: There is no package called ...?

I want to run R files in batch mode using Rscript, however it does not seem to be loading the libraries that I need. The specific error I am getting is:
Error in library(timeSeries) : there is no package called 'timeSeries'
Execution halted
However I do have the package timeSeries and can load it from Rstudio, RGui, and R from the command line no problem. The issue seems to only be when running a script using Rscript.
My system/environment variables are configured as:
C:\Program Files\R\R-3.1.0\bin\x64 (Appended to PATH)
R_HOME = C:\Program Files\R\R-3.1.0
R_User = Patrick
I am running the same version of R in RStudio, RGui, and R from command line. I've also checked .Library from these three sources and got the same output as well.
How can I run Rscript from command line with the packages that I am using (and have installed) in R?
EDIT:
I am using Rscript via Rscript script.r at the windows command line in the directory where script.r is located.
The output of Rscript -e print(.Library) is [1] "C:/PROGRA~1/R/R-31~1.0/library"
which is consistent with the other three options that I mentioned: [1] "C:/PROGRA~1/R/R-31~1.0/library"
However, if I put this in my script:
print(.libPaths())
library(timeSeries) #This is the package that failed to load
I get an output of:
[1] "C:/Program Files/R/R-3.1.0/library"
Error in library(timeSeries) : there is no package called 'timeSeries'
Execution halted
The corresponding call in RStudio gives an additional path to where the package is actually installed:
> print(.libPaths())
[1] "C:/Users/Patrick/Documents/R/win-library/3.1" "C:/Program Files/R/R-3.1.0/library"
In short, the value returned by calling Sys.getenv('R_LIBS_USER') in R.exe needs to be the same as the value returned by calling this at the command line:
Rscript.exe -e "Sys.getenv('R_LIBS_USER')"
and the above value needs to be included in this command line call:
Rscript.exe -e ".libPaths()"
Note that the values of R_LIBS_USER may be differ between R.exe and Rscript.exe if the value of R_USER is changed, either in the .Rprofile or the in target field of user's shortcut to R.exe, and in general, I find that the user library (i.e. .libPaths()[2]) is simply not set in Rscript.exe
Since I'm fond of setting R_USER to my USERPROFILE, I include the following block in at the top of .R files that I wish to run on mulitiple computers or in Rscript.exe's .Rprofile (i.e. Rscript -e "path.expand('~/.Rprofile')"):
# =====================================================================
# For compatibility with Rscript.exe:
# =====================================================================
if(length(.libPaths()) == 1){
# We're in Rscript.exe
possible_lib_paths <- file.path(Sys.getenv(c('USERPROFILE','R_USER')),
"R","win-library",
paste(R.version$major,
substr(R.version$minor,1,1),
sep='.'))
indx <- which(file.exists(possible_lib_paths))
if(length(indx)){
.libPaths(possible_lib_paths[indx[1]])
}
# CLEAN UP
rm(indx,possible_lib_paths)
}
# =====================================================================
As mentioned in the comments, it seems Rscript doesn't recognize the library path defaults automatically. I am writing an R script that needs to be source-able from the command line on different people's computers, so I came up with this more general workaround:
First store the default library path in a variable (Rscript-sourced functions can find this, they just don't automatiocally)
Then include that path in the library() call with lib.loc = argument.
This should work regardless of what the path is on a given computer.
library.path <- .libPaths()
library("timeseries", lib.loc = library.path)
Thanks again to #flodel above for putting me on the right path
This answer will not help the original asker (pbreach), but it may help someone else who stumbles across this question and has a similar problem to me.
I have many bash .sh script files which call RScript to execute .R files. My operating system is Windows 10, and I execute those bash files using cygwin.
Everything had been working fine until yesterday, when I finally upgraded my R from Revolution R 8.0.1 beta to Microsoft R Open 3.4.1. After that upgrade, every bash script that called RScript failed due to the exact same reason asked here (e.g. Error in library(zoo) : there is no package called 'zoo').
Investigation revealed that RScript actually worked fine if called from a DOS shell instead of from a cygwin bash shell.
For example, if I execute this in a DOS shell
C:\Progra~1\Microsoft\ROpen~1\R-3.4.1\bin\x64\Rscript.exe -e ".libPaths()"
I see the output
[1] "C:/Users/HaroldFinch/Documents/R/win-library/3.4"
[2] "C:/Program Files/Microsoft/R Open/R-3.4.1/library"
I eventually discovered the reason. As explained in the R FAQ, to define its home directory, R will first use the R_USER environment variable if defined, else it will use HOME environment variable if defined, else it will use the Windows "personal" directory.
My Windows configuration does not define either R_USER or HOME environment variables. So, in the DOS shell case, R uses my Windows "personal" directory (C:/Users/HaroldFinch/Documents). That is good, because that is where all my libraries are installed (C:/Users/HaroldFinch/Documents/R/win-library/3.4).
In contrast, cygwin defines and exports a HOME environment variable that points to my cygwin user directory, which lacks any R stuff. Hence, RScript called from cygwin had a wrong R home directory, and so failed to load libraries.
There are probably many ways to solve this. I decided to have my bash script set a R_USER environment variable which points to my Windows user directory.
For example, if I execute this in a cygwin bash shell:
R_USER="C:/Users/HaroldFinch/Documents"
export R_USER
/cygdrive/c/Progra~1/Microsoft/ROpen~1/R-3.4.1/bin/x64/Rscript.exe -e ".libPaths()"
I see the output
[1] "C:/Users/HaroldFinch/Documents/R/win-library/3.4"
[2] "C:/Program Files/Microsoft/R Open/R-3.4.1/library"
which is exactly the same output now as the DOS shell example above.
Another cause is packrat. If you are running with packrat, RStudio turns it on for you when you open the project. RScript does not, so you need a packrat::on() early in your script (before the library calls).
As the others have already pointed out, the problem is that Rscript.exe cannot recognise the win-library folder. The easiest solution for me was to explicitly set the path to the library folder by adding:
.libPaths("C:/Users/Benutzer1/Documents/R/win-library/4.0")
to my program. Then it loads all the packages from the win-library folder and it is still capable of loading packages from the standard library folder.

/usr/bin/env: RScript: No such file or directory | After recent R-3.0.1. installation.

I am a bit lost when dealing with installing and using R. I installed R 3.0.1 from source and did the ./configure, make, make check, and make install as suggested. However I tried running R but it said that R wasn't in the /usr/bin folder. So I then copied the entire R-3.0.1/bin directory into my /usr/bin directory using cp. Now I'm getting a few errors regarding /usr/bin/env when trying to use RScript on a hello_world.R script I wrote from the O'Reilly R In a Nutshell book I store in a file hello_world.R the contents are below:
#! /usr/bin/env RScript
print("Hello World!");
Simple enough, but when I try to load it I get the following error:
$ ./hello_world.R
/usr/bin/env: RScript: No such file or directory
I'm not sure if this is a PATH problem or something, but when I search in my /usr/bin directory I do see the RScript file in there along with (R, BATCH, and the others associated with R programming language). Any help is greatly appreciated. Cheers.
You may be using an invalid command line option for Rscript in your shebang line.
For instance ...
#!/usr/bin/env RScript --vanilla
remove "--vanilla" (or other offending option) and rerun your script
#!/usr/bin/env RScript
I know you didn't put this in your example, but the solution may help others searching for the same issue.
Again, the good solution to this problem is very simple and clearly explained in the man page of env. The script should use the env command to invoke Rscript and not Rscript directly:
#!/usr/bin/env Rscript
some R code now...
But a script like this will read the user's .Rprofile among other things. When we want to have a vanilla R session (in order to start with a clean and controlled R), we must pass the option --vanilla. If you try something like
#!/usr/bin/env Rscript --vanilla
some R code now...
env will take the string Rscript --vanilla a the command to execute and will inevitably return the error message
/usr/bin/env: ‘Rscript --vanilla’: No such file or directory
In env's man page, there is an option called -S for splitting the strings. Its role is exactly to solve the problem above and use the first string Rscript as the command name, and the following strings (like --vanilla) as options to pass to Rscript.
The solution is therefore:
#!/usr/bin/env -S Rscript --vanilla
some R code now...
Put in the shebang line of your script #!/usr/bin/Rscript and it should work.
As a side remark if you want to keep up-to-date with the R versions from CRAN and not relying on the native R of your Linux distro (Ubuntu) then add the following line in your apt sources:
deb http://my_favorite_cran_mirror/bin/linux/ubuntu raring/
After that you can always use the apt system to install R which -I would agree with Jake above- it should be the preferable way to install R.
*Change the my_favorite_cran_mirror with a valid CRAN mirror that is close to you.
#! /usr/bin/env RScript
print("Hello World!");
Simple enough, but when I try to load it I get the following error:
$ ./hello_world.R
/usr/bin/env: RScript: No such file or directory
Here u make mistake is that instead of RScript write Rscript.
The syntax will be
#! /usr/bin/env Rscript
print("Hello World!");
Then run it it will work (y) all the best.
$./hello_world.R
I arrived at this question trying to understand this error message on a cluster computer where I did not have control over the R installation.
In general, when I converted Rscript in my makefile to /usr/bin/Rscript the error message no longer occurred.

Resources