I have an R script that takes arguments that I want to be able to execute without having to type the entire path to the script location. For examples sake, lets say this is the script:
# arguments from command line
args <- commandArgs(trailingOnly=TRUE)
# argument 1:
# if no argument, end program with exit message.
if(is.na(args[1])) {
cat('Please provide an input.')
quit()
}
cat(paste("Hello", args[1]))
I am able to execute the file fine using:
Rscript R:\folder1\folder2\scripts\helloScript.r John
Hello John
and if no input is provided, it prints 'Please provide an input.' and quits so the script is functioning fine.
Since the script is nested in a bunch of folders I want to be able to run it without having to type the whole path to the script, and I would like to keep the location of the script where it currently is. I tried adding the \scripts\
directory to my path, but when I try to run it, no error is given or output created. When executing Rscript alone, the help file is output with no issues.
Here is my amended path output:
$env:Path
C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files (x86)\Aperio\Common;C:\Program Files\Git\cmd;C:\Program Files\R\R-4.4.2\bin;C:\Users\User1\AppData\Local\Programs\Quarto\bin;C:\Users\User1\AppData\Local\Microsoft\WindowsApps;C:\Users\User1\AppData\Local\Programs\VSCodium\bin;R:\folder1\folder2\scripts\
Any solutions? I would like to be able to run this helloScript.r
from any location without having to specify the entire path. Thank you!
I have an R script that takes arguments that I want to be able to execute without having to type the entire path to the script location. For examples sake, lets say this is the script:
# arguments from command line
args <- commandArgs(trailingOnly=TRUE)
# argument 1:
# if no argument, end program with exit message.
if(is.na(args[1])) {
cat('Please provide an input.')
quit()
}
cat(paste("Hello", args[1]))
I am able to execute the file fine using:
Rscript R:\folder1\folder2\scripts\helloScript.r John
Hello John
and if no input is provided, it prints 'Please provide an input.' and quits so the script is functioning fine.
Since the script is nested in a bunch of folders I want to be able to run it without having to type the whole path to the script, and I would like to keep the location of the script where it currently is. I tried adding the \scripts\
directory to my path, but when I try to run it, no error is given or output created. When executing Rscript alone, the help file is output with no issues.
Here is my amended path output:
$env:Path
C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files (x86)\Aperio\Common;C:\Program Files\Git\cmd;C:\Program Files\R\R-4.4.2\bin;C:\Users\User1\AppData\Local\Programs\Quarto\bin;C:\Users\User1\AppData\Local\Microsoft\WindowsApps;C:\Users\User1\AppData\Local\Programs\VSCodium\bin;R:\folder1\folder2\scripts\
Any solutions? I would like to be able to run this helloScript.r
from any location without having to specify the entire path. Thank you!
2 Answers
Reset to default 2Changing your path on Windows won't really help with this situation. The path controls where your OS looks for executable files. In your case, it's not the script that's the executable, the executable is Rscript
. With linux and MacOS you can make arbitrary individual files executable; that's not the case with Windows.
However, you can make .bat files which are executable.
So create a file called helloScript.bat
which contains
@echo off
Rscript "R:\folder1\folder2\scripts\helloScript.r" %*
And then make sure that file is located somewhere in your path. Then you can run
helloScript.bat John
to run the script.
We can create a file that is both an R script and a batch file provided we create a 1-line batch file utility called #Rscript2.bat as described below. Being a batch file it can be placed anywhere on the PATH.
Note that this is not exactly the same as the use of batch files discussed in a comment. In that comment it would require 2*N files (a batch file and an R file for each of N scripts) whereas this approach requires only N+1 (#Rscript2.bat plus a file for each script).
- Create a batch file #Rscript2.bat containing this single line. Modify the path as appropriate or omit the path if Rscript.exe is on your PATH. This only has to be done once regardless of how many script files you have.
"C:\Program Files\R\R-4.4\bin\Rscript.exe" %*
- Now for each R script put this line as the first line in the script and after that the R code can follow. The file should have a .bat exension, not .R and can have any base filename you like. for example it could be called myscript.bat . This makes the file both a .bat file and an .R file. The script should be on your PATH. There can be as many scripts as you like as long as they are all on your PATH.
#Rscript2.bat "%~dpnx0" %*
- To invoke this call it like this from any directory.
myscript
or like this:
myscript.bat