最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Executing an R file with Rscript without specifying entire path with Powershell on Windows - Stack Overflow

programmeradmin0浏览0评论

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!

Share Improve this question asked Mar 28 at 16:33 Tom ChiodoTom Chiodo 111 bronze badge 0
Add a comment  | 

2 Answers 2

Reset to default 2

Changing 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).

  1. 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" %*
  1. 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" %*
  1. To invoke this call it like this from any directory.
  myscript
or like this:
  myscript.bat
发布评论

评论列表(0)

  1. 暂无评论