Execute a script from the exec directory - r

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.

Related

Rscript execution error: No such file or directory

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.

How does R system() recognize command path?

How does R's built-in system() function know where to look to invoke some arbitrary OS command specified by the command argument? For example, if I homebrew install some_command_line_program, how does R's system() function know where it is located when I type:
cmd <- "some_complicated_code_from_some_command_line_program"
system(cmd, wait=FALSE)
In other words, how is R smart enough to know where to look without any user input? If I compile from source via Github (instead of homebrew install), would system() also recognize the command?
What system does depends on your OS, you've not told us (although you've given us some clues...).
On unix-alike systems, it executes it as a command in a bash shell, which searches for the first match in the directories on the $PATH environment variable. You can see what that is in R:
> Sys.getenv("PATH")
[1] "/usr/local/heroku/bin:/usr/local/heroku/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/nobackup/rowlings/NEST4B"
In Windows, it does something else.
You can get a full path to whatever it might run with Sys.which, which uses the systems' which command on unixes and fakes it on Windows. Read the help for more.
If you compile something from source then it will be found if the file that runs the command (a shell script, an executable, a #!-script in any language) is placed in a folder in your $PATH. You can create a folder yourself, say /home/user/bin, put your executables in there, add that to your $PATH, and (possibly after logging out an in again, or restarting R, or just firing up a new shell...) then R will find it.

How to execute a makefile from R

I am new to using makefiles in R, so excuse the really simple question: How do I execute a makefile which resides in a folder using R?
Is there a way to do so using a .R script? If so, what would the command line include?
I found the following solution to my problem in calling make using R (assuming you use Windows and RStudio):
Turns out you first need to add Rtools to your PATH
Select Avanced System Settings / Environment Variables / System Variables New.
Add the following to PATH:
C:\Rtools\gcc-4.6.3\bin
C:\Rtools\bin
Now Reboot.
After this is done, reopen your R console. Now you can use #yihui's servr package, and call a folder containing a makefile as follows:
servr::make(dir="path containing Makefile")
Hope this helps other newbies to makefile too...

Is there an approach for distributing R command-line scripts with an R package? [duplicate]

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.

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