R Command Line Passing a Filename to Script in Arguments (Windows)

R command line passing a filename to script in arguments (Windows)

As I said in my comment, I would use Rscript instead of R CMD BATCH:

Rscript myscript.R batch.csv

where myscript.R contains:

args <- commandArgs(TRUE)
batch_args <- read.table(args[1], sep=",")
# loop over multiple runs

Passing script as parameter to RGui

As said, you normally cannot do that. If you hack into your Rprofile or Rprofile.site ( see ?Startup for more information, or this site), you can go around that, but the code is not portable to other computers. So if you feel really lucky and daring, you can try to do the following.

You add this code to your Rprofile file or Rprofile.site (which can be found in the /etc folder of your R install):

Args <- commandArgs(trailingOnly=TRUE)
if(length(Args)>0 & sum(grepl(" -f ",commandArgs()))==0 ){
if(grepl("(?i).r$",Args[1])){
File <- Args[1]
Args <- Args[-1]
tryCatch(source(File) , error=function(e) print(e) )
}
}

This will allow you to do :

Rgui --args myscript.r arg1 arg2
Rscript myscript.r arg1 arg2
R --args myscript.r arg1 arg2
R -f myscript.r --args arg1 arg2

The --args argument will take care of the popups that @iterator warns for. The code will result in a variable Args which is contained in the base environment (which is not .GlobalEnv!). This variable contains all arguments apart from the filename. You can subsequently access that one from your script, eg:

#dumb script
print(Args)

If called with Rgui or R, there will also be a variable File that contains the name of the file that has been sourced.

Be reminded that changing your rProfile is not portable to other computers. So this is for personal use only. You can also not give -f as a parameter after --args, or you'll get errors.

Edit: We better search for " -f " than "-f" as this can occur in "path/to/new-files/".

R commandline arguments and makefile

You have two questions here.

The first question is about command-line argument parsing, and we already had several questions on that on the site. Please do a search for "[r] optparse getopt" to find e.g.

  • Parsing command line arguments in R scripts
  • R command line passing a filename to script in arguments (Windows)
  • Is there a package to process command line options in R?

and more.

The second question concerns basic Makefile syntax and usage, and yes, there are also plenty of tutorials around the net. And you basically supply them similar to shell arguments. Here is e.g. a part of Makefile of mine (from the examples of RInside) where we query R to command-line flags etc:

## comment this out if you need a different version of R, 
## and set set R_HOME accordingly as an environment variable
R_HOME := $(shell R RHOME)

sources := $(wildcard *.cpp)
programs := $(sources:.cpp=)

## include headers and libraries for R
RCPPFLAGS := $(shell $(R_HOME)/bin/R CMD config --cppflags)
RLDFLAGS := $(shell $(R_HOME)/bin/R CMD config --ldflags)
RBLAS := $(shell $(R_HOME)/bin/R CMD config BLAS_LIBS)
RLAPACK := $(shell $(R_HOME)/bin/R CMD config LAPACK_LIBS)

## include headers and libraries for Rcpp interface classes
RCPPINCL := $(shell echo 'Rcpp:::CxxFlags()' | \
$(R_HOME)/bin/R --vanilla --slave)
RCPPLIBS := $(shell echo 'Rcpp:::LdFlags()' | \
$(R_HOME)/bin/R --vanilla --slave)

## include headers and libraries for RInside embedding classes
RINSIDEINCL := $(shell echo 'RInside:::CxxFlags()' | \
$(R_HOME)/bin/R --vanilla --slave)
RINSIDELIBS := $(shell echo 'RInside:::LdFlags()' | \
$(R_HOME)/bin/R --vanilla --slave)

[...]

Running R function as command line script with arguments

You might find the information in ?commandArgs helpful.

From the description:

 Provides access to a copy of the command line arguments supplied
when this R session was invoked.

Batch file: Pass file name to sub from FOR loop

parameters are separated by spaces - so C:\Batch File Example\file.txt are three parameters (more, if the filename also contains spaces).

Either use %* ("all parameters") instead of %1 or use quotes: ... CALL :doecho "%%I", then it's a single (quoted) parameter. If you need to remove the quotes in your subroutine, use %~1

How to check if arguments have been correctly passed to Rscript on Windows

As Vincent said, you should use the trailingOnly argument to commandArgs to simplify things.

As Konrad said, never, ever, ever compare directly to TRUE and FALSE.

Also, use assertive for doing assertions.

library(assertive)
library(methods)
cmd_args <- commandArgs(TRUE)

if(length(cmd_args) < 3)
{
stop("Not enough arguments. Please supply 3 arguments.")
}
inputFile <- cmd_args[1]
if (!file_test("-f", inputFile))
{
stop("inputFile not defined, or not correctly named."
}
headerSpec <- match.arg(cmd_args[2], c("header", "no_header"))
numberOfResamples <- as.numeric(cmd_args[3])
assert_all_numbers_are_whole_numbers(numberOfResamples)
assert_all_are_positive(numberOfResamples)

message("Success!")


Related Topics



Leave a reply



Submit