[R] Calling r-scripts with arguments from other scripts, or possibly R programming conventions/style guide
Sam Tuck
STuck @ending from nz@uperfund@co@nz
Wed Jun 13 04:33:41 CEST 2018
Hi All,
I am new to R and am wondering if there is a way to pass arguments between rscripts. I have this working but have had to create a C# shell calling the scripts in sequence via windows scripting which enables command line arguments to get the necessary interaction.
I'm wondering if I'm using an outdated program construction technique - I create r files like I would programme functions or reoccurring code snippets in C. It may be that r was not designed to create lots of little r script modules that interact via a master script?
Ideally I'd like to call r scripts from other r scripts and have all the variables still in memory: For example
I've been using RStudio Version 1.1.447 to programme and regression test my individual scripts,.
Script Arg Script.R
{
# We are going to pass arguments into this script
arguments <- commandArgs(trailingOnly = TRUE)
#arguments[1] is double
#arguments[2] is double
#arguments[3] is double.
if(length(arguments) <3)
{
stop("Not enough arguments, please supply 3, [% dbl}total deviation, [% dbl] individual deviation, [int] periods before recenter")
}
TotalDeviation <- as.numeric(arguments[1])/100
IndividualDeviation <- as.numeric(arguments[2])/100
RecenterPeriods <- as.numeric(arguments[3])
# We then manipulate some objects based on these inputs, but for this test we will output them to a file.
fileConn<-file("output.txt")
writeLines(c(TotalDeviation, IndividualDeviation, RecenterPeriods), fileConn)
close(fileConn)
}
Script RunningScript.R
{
Arg Script.R 0.6 0.4 132
}
To which I get
Error: unexpected symbol in " Arg Script.R"
When I use the script RunningScript.R
{
system(paste("Arg Script.R", 0.8, 0.4, 132))
}
Nothing occurs (there is no output file created, but also no error)
When I use RunningScript.R
{
commandArgs <- c(0.6,0.4,132)
source("Arg Script.R')
}
I don't get any args passed into the file. Instead getting the error
Not enough arguments, please supply 3, [% dbl}total deviation, [% dbl] individual deviation, [int] periods before recenter
Thanks
Sam Tuck
More information about the R-help
mailing list