making commandargs comma delimited or parsing spaces

不羁的心 提交于 2019-11-29 10:23:12

Using eval(parse()) is probably not the best and most efficient way to parse command line arguments. I recommend to use a package like the optparse to do the parsing for you. Parsing command line args has already been solved, no need to reimplement this. I could imagine that this solves your problems. Although, spaces in path names are a bad idea to begin with.

Alternatively, you could take a very simple approach and pass the arguments like this:

R CMD BATCH --slave arg1 arg2

Where you can retrieve them like:

ca = commandArgs(TRUE)
arg1 = ca[2]
arg2 = ca[3]

This avoids the eval(parse which I think is causing the issues. Finally, you could try and escape the space like this:

R CMD BATCH --slave "C:/spam\ bla"

You could also give Rscript a try, R CMD BATCH seems to be less favored than Rscript.

As an enhancement of @PaulHimestra answer here how you can use Rscript :

you create a launcher.bat ,

echo off
C:
PATH R_PATH;%path%
cd DEMO_PATH
Rscript youscript.R arg1 arg2
exit

with R_PATH something like C:/Program Files/R/R-version

antonio

There are many similarities with this post: R command line passing a filename to script in arguments (Windows)

Also this post is very OS related. My answer applies only to Windows.

Probably what you are looking for is RScript.exe instead of R.exe. The latter has no problem with spaces: path\to\RScript "My script.r".

One boring thing may be searching or setting the path for RScript and doing this every time one updates R.

Among the convenience scripts I have in my search path, I wrote a little facility to run RScript without bothering with paths. Just in case it may be of interest for someone:

@echo off
setlocal 


::Get change to file dir par (-CD must be 1st par)
::================================================
Set CHANGEDIR="F"
If /I %1 EQU -cd  (
 Set CHANGEDIR="T"
 SHIFT        
)       

::No args given
::=============
If [%1] EQU [] GoTo :USAGE 

::Get R path from registry 
::========================
:: may check http://code.google.com/p/batchfiles for updates on R reg keys
Call :CHECKSET hklm\software\R-core\R  InstallPath
Call :CHECKSET hklm\software\wow6432Node\r-core\r InstallPath
if not defined RINSTALLPATH echo "Error: R not found" & goto:EOF

::Detect filepath when arg not starting with "-" 
::==============================================

::Note the space after ARGS down here!!!
Set ARGS= 
:LOOP          
if [%1]==[] (GoTo :ELOOP)
Set ARGS=%ARGS% %1
::Echo [%ARGS%] 

Set THIS=%~1
if [%THIS:~0,1%] NEQ [-] (Set FPATH=%~dp1)

SHIFT          
GoTo :LOOP
:ELOOP        
::echo  %FPATH%


::Run Rscript script, changing to its path if asked
::=================================================
If %CHANGEDIR%=="T" (CD %FPATH%)
Echo "%RINSTALLPATH%\bin\Rscript.exe" %ARGS%
"%RINSTALLPATH%\bin\Rscript.exe" %ARGS%



endlocal 
:: ==== Subroutines ====
GoTo :EOF  

:USAGE       
Echo USAGE:
Echo  R [-cd] [RScriptOptions] Script [ScriptArgs]

Echo.          
Echo  -cd changes to script dir. Must be first par. 
Echo  To get RScript help on options etc.:
Echo  R --help

GoTo :EOF  


:CHECKSET  
if not defined RINSTALLPATH for /f "tokens=2*" %%a in ('reg query %1 /v %2 2^>NUL') do set RINSTALLPATH=%%~b
GoTo :EOF  

The script prints the actual RScript invoking line, before running it.

Note that there is an added argument, -cd, to change automatically to the script directory. In fact it is not easy to guess the script path from inside R (and set it with setwd()), in order to call other scripts or read/write data files placed in the same path (or in a relative one).

This (-cd) might possibly make superfluous your other commandargs, as you may find convenient calling them straight from inside the script.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!