Rscript execution error: No such file or directory - r

The binary of Rscript is available but when I try to use it I get:
Rscript helloworld.r
Rscript execution error: No such file or directory
If I just do Rscript, it brings the help/usage for it through.
R CMD BATCH is working fine.
I tried adding shebang lines in the R code at the start but it didn't work.
#!/sys_apps_01/R/R-3.2.0/bin/R
#!/sys_apps_01/R/R-3.2.0/bin/Rscript

As in your case, this was caused by me moving R (in order to try to use it in an AWS lambda function).
I resorted to doing the equivalent call on R itself:
./R --slave --no-restore --file=TheScript.R

It's likely this was installed to (configured for) another directory and then moved after installation. Afterwards Rscript won't be able to find the (hardcoded?) R binary. I just had the same problem, which could be solved by reinstalling R.
Andreas

I preface this solution issuing a caution to do this at one's own risk. However I encountered the same issue and had the following solution:
Say you've run make && make install which has installed R to /path/to/install/loc. Once you've moved this to path/to/new/loc, R/Rscript will then complain it can't find the right file/directory.
Editing the R and Rscript executables in path/to/new/loc/bin, you can change any reference to /path/to/old/loc to /path/to/new/loc. This has worked for me and haven't encountered any further issues
As has been previously mentioned, it's definitely preferable to install R to the required location either through prefix=... in the configure script, or by using the rhome=... argument following make install

I encountered the same issue. What happened in my case was that, R was first installed at /usr/lib/R with deb packages, then I moved the dir to /opt/R and defined R_HOME to the new dir, hoping R will adapt to it automatically, but turns out there are hardcoded paths of /usr/lib/R in bin/R. Unless I update the paths, simply moving R to another location will break the installation.

Related

Execute a script from the exec directory

In R packages there can be a directory exec which contains some executable scripts. I have such a script called json_merge.R in my package numericprojection. This gets installed to ~/R/x86_64-redhat-linux-gnu-library/3.6/numericprojection/exec/json_merge.R.
To execute it I can of course specify that particular path and call it with Rscript from the command line. I was wondering whether there is some way to have R resolve this path such that I could just specify json_merge.R and numericprojection.
In the meantime I constructed this here:
r_libs_user="$(Rscript -e "cat(Sys.getenv('R_LIBS_USER'))")"
script="$r_libs_user/numericprojection/exec/projected_merge.R"
script="${script/#\~/$HOME}" # https://stackoverflow.com/a/27485157/653152
"$script"
That's what the system.file command is for. In your case that command should look like this:
system.file("exec", "json_merge.R", package = "numericprojection")
And will return:
~/R/x86_64-redhat-linux-gnu-library/3.6/numericprojection/exec/json_merge.R
If that is where the file was installed.
However, I think that your question is likely based on a misunderstanding as outlined in the comments.

Running R from Mac OSX terminal

I've searched the web, and I'm still unclear on how to run R from the Mac terminal. I have Rstudio and the standalone R app installed. I thought I could just type "R" from the command line as I do with "python", but that doesn't work. Is it necessary to edit the PATH in my bash profile? If so, how do I give the correct location of R?
Thanks for any help
Edits after receiving comments
So, I'm running Sierra, and when I type "r" or "R" at the terminal, I get "-bash: R: command not found." If I type, "which R" in the terminal I do not get any output.
Here is the output from "echo $PATH": /usr/local/heroku/bin:/opt/local/bin:/opt/local/sbin:/Library/Frameworks/Python.framework/Versions/2.7/bin:/Users/samuelcolon/anaconda/bin:/Library/Frameworks/Python.framework/Versions/3.5/bin:/Users/samuelcolon/.rvm/gems/ruby-2.1.0/bin:/Users/samuelcolon/.rvm/gems/ruby-2.1.0#global/bin:/Users/samuelcolon/.rvm/rubies/ruby-2.1.0/bin:/Library/Frameworks/Python.framework/Versions/2.7/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/samuelcolon/.rvm/bin:/Users/samuelcolon/.rvm/bin
As for the installation, I believe I downloaded it directly from cran.r-project.org a while ago. I can locate the GUI in my applications and open it--
I have version 3.13. Is it possible, I only have R.app installed but not R? Perhaps that's the reason I'm getting the 'command not found' when typing "R" into the terminal?
Generally, I've been working in RStudio, but I'd still like to access R from the terminal and also to find where things are located. I'm fine with removing and re-installing R if it's easiest to start from square one. I hope the extra detail helps, and I appreciate the responses.
An answer for those not that familiar with Terminal and Bash.
I have done a fresh update install of R from the R.org cran site as part of seeking an answer to your question.
I found this latest install version 3.4.0 installs R for access in Terminal, and also installs R.app as part of the package.
To my understanding, reading support docs, if you have an older version of R it will update that. However it will not update an installation of R installed by the anaconda package.
Where are the R files stored?
I can only assume that with a fresh install of the latest R, R will work for you in Terminal.
To learn where the R files are that are being accessed - in Terminal after starting R, and in R.app, type:
>R.home()
In my case as example:
In R.app - the R version 3.4.0 is accessed in the top directory (not my user folder):
R.home()
[1] "/Library/Frameworks/R.framework/Resources"
In Terminal - the R version 3.3.2 is accessed in the Anaconda package, again in the top level directory.
R.home()
[1] "/anaconda/lib/R"
So I have two different versions of R, and Terminal accesses a different version to R.app.
How can I ensure I access the same version in Terminal as I do in the R.app?
For someone familiar with bash, and how the whole bash command system works I am sure there is a well constructed command. All the same here are some novice solutions.
-
• First Solution:
I could update the anaconda version, however, I would prefer not to as as other elements of the anaconda package my depend on this older version of R. For those not yet familiar with Terminal and bash, not such a novice solution.
-
• Second Solution:
This solution came from mko. It provides a single use solution. From the result above, and checking the directory structure a little further to find this R file.
Finding the significant R file enables me to edit an extension of the above path shown in the R.app. So add /bin/R to enter
/Library/Frameworks/R.framework/Resources/bin/R
Entering and pressing return will start R from this version.
Alternatively, one can find this file and icon in the GUI Finder, lead by the above result, and just double click on it, and it will open Terminal and a session with R running for you. Easy!
One could also make an alias of it and put it on your desktop for easy future starts.
-
• Third Solution:
My last solution I think may be best, adding to mko's solution. Make an alias.
Being in my home directory in Terminal I open .bash_profile using the nano text editor. (If you do not already know how to do this, then best not use this solution.)
I then add the line in this env file.
alias Rv340='/Library/Frameworks/R.framework/Resources/bin/R'
I then save the changes and exit this terminal session. I then open a new Terminal window. (This is so the changes to the env above are incorporated in the new terminal session).
Then when I enter the alias:
Rv340
The version of R I want opens.
You can choose a different alias name to "Rv340".
-
• Fourth Solution:
A second more permanent solution for opening the same version of R in Terminal is as follows.
Copy the path as showing in R.app in response to the R.home() command above, and add that path to PATH in your .bash_profile. (If you do not know already how to do this, then ignore this solution.) Do so as follows.
export PATH="/Library/Frameworks/R.framework/Resources:$PATH"
To my understanding, this ensures that bash looks here for R (and anything else), then moves on to the other paths in PATH. Since this adds this path to the beginning of $PATH, an env variable, bash looks here first where it finds the newer version first, and stops looking.
When it comes to understanding PATH in the env set up in .bash_profile the following two links were helpful.
About PATH.
How to correctly add a path to PATH.
This solution may muck with anaconda's invocation of R. I have yet to check this.
First of all, you have to start terminal application. You can use either built in Terminal.app, or you can use replacement. My favorite one is iTerm2
https://www.iterm2.com
Then, you simply open terminal window and run R. Just like shown below:
Have fun with R!
Just ran into the same issue when installing R-4.0.3.pkg on my MacBook (MacOS BigSur). Can open R.app to the clunky R GUI, but typing in 'R' in terminal doesn't work.
Turns out, an R executable lives here: /Library/Frameworks/R.framework/Versions/4.0/Resources/bin/R
So I added this alias to my newly created .zshrc script:
alias R '/Library/Frameworks/R.framework/Versions/4.0/Resources/bin/R'
Now when I type in R, it opens... I swear this all happened seamlessly in earlier versions.
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. This only impacts MacOS system releases of 20 and above (you can check with uname -r).
I've included more info here, along with what the "correct" fix should be: manually creating symbolic links to /usr/local/bin that point to the R and Rscript binaries themselves. If this is the current challenge, then this would be a far better solution to creating aliases or manipulating PATH in various ways, since it's what the installation package intended to do (and presumably will again soon).
R: command not found
In short, if this is the problem, then Ashkan Mirzaee's answer (https://stackoverflow.com/a/67202173/2093929) to create the symbolic links directly is correct in form, but might not have the right link command. The 4.0.5 package intends instead to use:
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 .
You can create a symbolic link from R and Rscript binaries to /usr/local/bin to add them to the PATH:
sudo ln -s /Library/Frameworks/R.framework/Versions/Current/Resources/bin/R /usr/local/bin
sudo ln -s /Library/Frameworks/R.framework/Versions/Current/Resources/bin/Rscript /usr/local/bin
Now which R should return /usr/local/bin/R and you can use R.
An easy way to open RStudio with admin privilege on macOS:
Go to Applications, then right click on RStudio
Select "Show Package Contents"
Go to Contents/MacOS
Now open terminal(in bash mode). Type sudo and drag the RStudio.exec into terminal and press on ENTER
Now RStudio will have admin access!

Cannot run interactive R session from the command line - windows 10

I cannot run a R session from the command line by typing 'R', despite having
C:/Program Files/R
and
C:/Program Files/R/R-3.3.2/bin
in both 'Path' (System) and 'PATH' (User) environment variables (the latter directory is where the actual application is located, I was just trying everything by adding the former).
If I manually change the working directory to C:/Program Files/R/R-3.3.2/bin and type R, a session runs.
Any insight as to why this might be the case? Other programs such as Python 3 run fine, provided the the environment variable is set, why not with R?
This question is part of a larger problem where I am trying to get rpy2 working, but though successfully installed, none of it's basic functions are working, and I thought this maybe to do with a version of R being installed at C:/Users/Anaconda2/R (Because this dir was in Path). The R application installed here would not work when opened, but was the directory returned when I typed R RHOME into cmd - despite the fact this installation is not even functioning, and has apparently only existed since I installed rpy2 a couple of hours ago. I reasoned that rpy2 is trying to use the instance of R installed here rather than my normal one, thus causing the error when I call robjects.r. But this still doesn't explain why I can't run R from cmd. When using R.home(component = "home") from inside a an R session (in RStudio or out of it), it returns my normal, working R directory, at C:/PROGRA~1/R/R-33~1.2. By moving the R file tree installed in Anaconda's directory to a somewhere not in Path, I hoped Windows would find the correct installation via Path - but instead I just get Failed to create process.
My last guess was that Windows didn't like the space in "Program Files", but loads of variables in Path go through the same folder. Any ideas?
I am using:
Windows 10,
R 3.3.2,
Python 2.17.13
UPDATE, PERHAPS SOLVED:
On Josh O'Brien's recommendation (thanks!), I typed where R into cmd, returning:
C:\Users\charl\Anaconda2\Scripts\R.exe
C:\Program Files\R\R-3.3.2\bin\R.exe
The first path is marginally different from the C:\Users\charl\Anaconda2\R listed in my first post (both are in Path), and leads to a third R.exe. It's outside the R file tree located in Anaconda, unlike my standard R installation, but seems to be dependent on it nonetheless. If I remove this R.exe to another location (not in Path), Typing R in cmd will run R from the desired directory, thus fixing the problem. R.home() from inside that session confirms this.
My only remaining concern then; is whether this installation of R in Anaconda necessary for rpy2 or can I just delete it, fixing the original problem? It may just be my ignorance, but I was not conscious that installing rpy2 (I used the standard Windows installer rather than compiling manually) would install another instance of R - is it necessary for the package or just a default/option I failed to omit?

Passing argument to R with Command Prompt [duplicate]

I'm exploring package building on Windows 7 but have been running into trouble after reaching the Command Prompt stage. The following thread appears to be on the same topic that I'm having trouble with, but the question remains unresolved:
Cannot install R package in Windows 7
The problem: whenever I try an R CMD command in Command Prompt (e.g. I've tried R CMD install pkgName, R cmd install pkgName, Rcmd install pkgName, where pkgName is a place-holder for an actual package that has been R CMD checked on OS X and also posted on CRAN, where I have the unzipped source in the current working directory), I get the following error message:
'R' is not recognized as an internal or external command,
operable program or batch file.
(If I try the Rcmd install pkgName command, then the error starts out as 'Rcmd' is not ....) Note that even if I type in just R in Command Prompt and hit enter, I get the same error, so I'm not even able to bring up R here, though it opens perfectly fine in its stand-alone interface.
I have examined the following guides without success in identifying the trouble:
Karl W Broman's website
Rob J Hyndman's website
Steven Mosher's blog
The most immediate thing that came to mind was that the path variables in Windows were not set right, but this also has not (yet) led to the answer. For example, I have triple-checked the paths to ensure they are correct, including for the R version number, that x64 in the path name is accurate, etc.
I realize this is a complex issue to remotely diagnose, but any advice and suggestions on resolving this trouble is appreciated. For reference, below are the steps I have taken already to try to resolve this issue:
Uninstall R and Rtools
Install the latest version of R (2.15.0)
Install Rtools215 to match R version 2.15.0
Originally I let Rtools specify the path variables (turning that option on during installation). No success after this method.
I updated the path variables on my computer, where the below paths were used:
C:\Rtools\bin; C:\Program Files\R\R-2.15.0\bin\x64;
I've also tried the R CMD (and Rcmd) commands with the following paths included (note that these paths don't lead to anything on my computer, so I tried doing R CMD / Rcmd with and without them specified):
C:\Rtools\perl\bin; C:\Rtools\MinGW\bin;
Tried adding/removing the following path, which goes to the only other folder visible within the Rtools folder:
C:\Rtools\gcc-4.6.3;
Tried multiple variations of the R CMD commands, as noted earlier.
I tried this all a few months ago when I had R version 2.14.1 and Rtools214 installed, and I had the exact same trouble.
Note: I already had Cygwin installed prior to R and Rtools, and I have not installed MikTeX (yet).
Have you closed the command prompt and opened a new one after adjusting the PATH?
Enter the command PATH on the command line and ensure that you are properly updating your path
Try navigating to the directory where R.exe is saved and running it locally -- cd C:\Program...\bin\x64, R.exe.
Ensure that you're running on Windows' Command Prompt and not Cygwin's terminal
If you actually have a space after the semi-colon in your PATH, you'll need to remove that: /bin;C:/Program.... Try combining that with Joshua's solution.
I attach my whole solution here:
Install Rtools which is compatible with my R version
Add PATH: C:\Program Files\R\R-2.15.1\bin\x64;C:\Rtools\bin;C:\Rtools\gcc-4.6.3\bin
In cmd, enter: R CMD INSTALL packagename
Click "enter", done.
I had a lot of trouble getting this to work, and finally found the golden nugget on r-project (R doesn't like spaces in paths) so here's how I finally did all this:
I installed R to "c:\programs" instead of "c:\program files...". My final path to RSCRIPT.exe is "C:\Programs\R\R-2.15.2\bin\Rscript.exe"
I added that directory to my PATH, but that doesn't seem to help
RStudio is also installed under c:\Programs (e.g. c:\Programs\RStudio), but RTools is installed under the "c:\" root directory (e.g. c:\RTools).
I downloaded all the packages to "c:\downloads\r"
I opened a command prompt in "c:\downloads\r" and ran the R CMD install from this downloads directory, specifying the path to R
e.g. "C:\Programs\R\R-2.15.2\bin\x64\R CMD INSTALL colorspace_1.2-0.zip"
It appears that the CMD processor is Case Sensitive. You have to use INSTALL vs. install
Simple solution, use quotes:
c:> "C:\Program Files\WinZip\wzunzip.exe"
if you run this, you don't need to worry about the space in Program Files.
Hope this help.
I encountered a similar problem while trying to use SparkR from Windows Power Shell. Each time I would do ".\sparkR.cmd" (I was inside the folder containing this file), it would throw an error message like,
cmd is not recognized as an internal and external
command...blah...blah...blah.
I solved the problem by adding "C:\Windows\System32" to my PATH.
I restarted my system and then repeated the process and this time, I saw a new error,
R is not recognized as an internal and external
command...blah...blah...blah.
I tried all sorts of approaches, including the ones mentioned above. NOTHING WORKED, I was probably wasn't efficient enough to make it work.
Then I came across this place at code.google. I simply copied the R.bat onto a notepad, named it as R.bat. Kept it inside the folder where sparkR.cmd was located. For me it was,
C:\Apache\spark-1.5.1-bin-hadoop2.6\bin\
The I placed it on my PATH as :
C:\Apache\spark-1.5.1-bin-hadoop2.6\bin\R.bat
Restarted my laptop, opened my Power Shell as an administrator and typed :
.\R.bat help
It worked fine. Then I typed,
.\sparkR.cmd
and there it was.
It worked.
I hope it helps to people new to R. Thank you G. Grothendieck.
In the .bat file add a line of code specifying the path of the folder where R is installed and then give the path of the script as follows -
#ECHO OFF
PATH C:\Program Files\R\R-3.5.1\bin
R CMD BATCH D:\project_abc\helloworld.R
Following this, run the .bat file and you will see the code in the script will run smoothly.
In my case the issue was solved by using Edit instead of New while selecting PATH in the window below:
enter image description here
After selecting Edit I added the directory below:
C:\Program Files\R\R-4.0.2\bin\x64
This solved the issue in my case.

Including Command Line Scripts with an R Package

I am interested in providing a command line interface to an R package called Slidify that I am authoring. It uses Rscript and I think that would make it cross-platform. The scripts are stored in the subdirectory inst/slidify. In order to use the script from any directory, I added its path to my .bash_profile as I am on a Mac.
My question is
How should I handle installation of the script in an automated cross-platform way?
How can I make sure that the file permissions are retained in this process?
What should the shebang line for the script be? I am currently using
#!/usr/bin/Rscript --vanilla --slave
I would appreciate pointers on how to handle this and any examples of R packages that already do it. Just to make sure, I am clear on how this would work, a user would be able to generate a slide deck from slides.Rmd by just running slidify generate slides.Rmd from the command line.
UPDATE:
Here is how I install it on a Mac from the command line. I use the excellent sub library by 37 signals to create the scripts.
echo "$(path/to/clidir/slidify init -)" >> ~/.bash_profile
exec bash
Two follow up questions
Can I package these commands into an R function install_slidify_cli?
How can I mirror these commands for Windows users?
Lovin' slidify so would be glad to help.
But in short, you can't.
R packages simply cannot install outside of $R_HOME or the chosen library folder. Ship the script in the package, and tell users to copy it. If there was a better way, out littler package with predecessor / alternative to Rscript would long have used it, and roxygen / roxygen2 would also have shipped something.

Resources