R: Difference between revisions

Jump to navigation Jump to search
72 bytes added ,  2 years ago
Marked this version for translation
(Marked this version for translation)
Line 153: Line 153:




== Passing arguments to R scripts ==
== Passing arguments to R scripts == <!--T:84-->
Sometimes it can be useful to pass parameters as arguments to R scripts, to avoid having to either change the R script for every job or having to manage multiple copies of otherwise identical scripts. This can be useful to specify the names for input- or output-files, or maybe numerical parameters.
Sometimes it can be useful to pass parameters as arguments to R scripts, to avoid having to either change the R script for every job or having to manage multiple copies of otherwise identical scripts. This can be useful to specify the names for input- or output-files, or maybe numerical parameters.


<!--T:85-->
The following example expects exactly two arguments. The first one should be a string which will be used for the variable "name" and the second one should be an integer for the variable "number".
The following example expects exactly two arguments. The first one should be a string which will be used for the variable "name" and the second one should be an integer for the variable "number".
{{File
{{File
Line 163: Line 164:
args = commandArgs(trailingOnly=TRUE)
args = commandArgs(trailingOnly=TRUE)


<!--T:86-->
# test if there is at least two arguments: if not, return an error
# test if there is at least two arguments: if not, return an error
if (length(args)<2) {
if (length(args)<2) {
Line 168: Line 170:
}
}


<!--T:87-->
name      <- args[1]                # read first argument as string
name      <- args[1]                # read first argument as string
number    <- as.integer( args[2] )  # read second argument as integer
number    <- as.integer( args[2] )  # read second argument as integer


<!--T:88-->
print(paste("Processing with name:'", name, "' and number:'", number,"'", sep = ''))
print(paste("Processing with name:'", name, "' and number:'", number,"'", sep = ''))
}}
}}


<!--T:89-->
This script can be used like this:
This script can be used like this:
{{Command
{{Command
rsnt_translations
56,420

edits

Navigation menu