[R] scan or source a text file into a list

Adaikalavan Ramasamy ramasamy at cancer.org.uk
Thu Nov 11 17:55:37 CET 2004


Not sure if I understand your question.

If you know the complete list of all possible variable names, then you
can grep for each occurrence and split on the "=". Otherwise, I hope you
have some sort of markers (like main()) to seperate the input and actual
commands. I think perl and bash are probably more suitable for this kind
of job.

Then you can try commandArgs() to feed into R.



On Thu, 2004-11-11 at 15:57, Andy Bunn wrote:
> I've ported somebody else's rather cumbersome Matlab model to R for
> colleagues that want a free build of the model with the same type of I/O.
> 
> The Matlab model reads a text file with the initial parameters specified as:
> 
> C:\Data\Carluc\Rport>more Params.R
> # Number of years to simulate
> nYears = 50;
> # Initial year for graphing purposes
> year0 = 1970;
> # NPP/GPP ratio (cpp0 unitless)
> fnr = 0.30;
> # Quantum efficency
> qe  = 0.040;
> 
> That is, there are four input variables (for this run - there can be many
> more) written in a way that R can understand them. In R, I can have the
> model source the parameter text file easily enough and have the objects in
> the workspace. The model function in R takes a list at runtime. How can I
> have R read that file and put the contents into the list I need?
> 
> E.g.,
> > rm(list = ls())
> > source("Params.R")
> > ls()
> [1] "fnr"    "nYears" "qe"     "year0"
> > fnr
> [1] 0.3
> > nYears
> [1] 50
> > foo.list <- list(fnr = fnr, nYears = nYears)
> >
> > foo.list
> $fnr
> [1] 0.3
> 
> $nYears
> [1] 50
> 
> 
> The model is then run with
> > CarlucR(inputParamList = foo.list, ...)
> 
> I can't build inputParamList "by hand" as above because the number of
> initial parameters changes with the model run and this runs in a wrapper.
> 
> Any thoughts? Some combination of paste with scan or parse?
> -Andy
> 
> 
> > version
>          _
> platform i386-pc-mingw32
> arch     i386
> os       mingw32
> system   i386, mingw32
> status
> major    2
> minor    0.0
> year     2004
> month    10
> day      04
> language R
> >
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html




More information about the R-help mailing list