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>
james.holtman@convergys.com
2004-Nov-11 16:30 UTC
[R] scan or source a text file into a list
use an 'environment' to read in the values; e.g.,> with(e1 <- new.env(),source('/tempxx.txt', local=T)) # read in the fileto a new environment> myList <- list() # define empty list > for (i in ls(e1)){ # process each element+ myList[i] <- get(i, e1) + }> > ls(e1) # show objects in the list[1] "fnr" "nYears" "qe" "year0"> myList # output my list$fnr [1] 0.3 $nYears [1] 50 $qe [1] 0.04 $year0 [1] 1970>__________________________________________________________ James Holtman "What is the problem you are trying to solve?" Executive Technical Consultant -- Office of Technology, Convergys james.holtman at convergys.com +1 (513) 723-2929 "Andy Bunn" <abunn at whrc.org> To: "R-Help" <r-help at stat.math.ethz.ch> Sent by: cc: r-help-bounces at stat.m Subject: [R] scan or source a text file into a list ath.ethz.ch 11/11/2004 10:57 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
Here's one way:> myList <- mget(ls(), .GlobalEnv) > myList$fnr [1] 0.3 $nYears [1] 50 $qe [1] 0.04 $year0 [1] 1970 HTH, Andy> From: Andy Bunn > > 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 > >
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
On Thursday 11 November 2004 09: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?Approaching this slightly differently, how about read.pars <- function(file) { foo <- read.table(file, as.is = TRUE) ans <- lapply(strsplit(foo[[3]], ";"), as.numeric) names(ans) <- foo[[1]] ans } Deepayan
On Thu, 11 Nov 2004, 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:I think I would be tempted to try out read.dcf() for this, which returns a character matrix of field values in named columns, from which values cast to numeric:> system("cat tmp/Params.dcf")nYears: 50 year0: 1970 fnr: 0.30 qe: 0.040> x <- read.dcf("tmp/Params.dcf") > xnYears year0 fnr qe [1,] "50" "1970" "0.30" "0.040"> str(x)chr [1, 1:4] "50" "1970" "0.30" "0.040" - attr(*, "dimnames")=List of 2 ..$ : NULL ..$ : chr [1:4] "nYears" "year0" "fnr" "qe" Since you will know the mode you need, you could read the same-mode fields together and cast them:> x <- read.dcf("tmp/Params.dcf", fields=c("nYears", "year0")) > mode(x) <- "integer" > xnYears year0 [1,] 50 1970 and make x a data frame if say x$nYears is needed, which gets to list:> as.list(data.frame(x))$nYears [1] 50 $year0 [1] 1970 and c() of the different mode lists to make inputParamList. I think treating everything as numeric may be enough, and the DCF format is flexible. Roger> > 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 >-- Roger Bivand Economic Geography Section, Department of Economics, Norwegian School of Economics and Business Administration, Breiviksveien 40, N-5045 Bergen, Norway. voice: +47 55 95 93 55; fax +47 55 95 93 93 e-mail: Roger.Bivand at nhh.no
Andy Bunn <abunn <at> whrc.org> writes: : : 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. : The following sources the file in the environment that exists within function f returning a list of all variables in that environment except those variables that begin with a dot. You could alternately replace the source(...) statement with eval(parse(.file)) . R> f <- function(.file) { source(.file, local = TRUE); as.list(environment()) } R> f("clipboard") $qe [1] 0.04 $fnr [1] 0.3 $year0 [1] 1970 $nYears [1] 50 :
Seemingly Similar Threads
- read.table and trouble
- Markov transition matrices , missing transitions for certain years
- ROCR package finding maximum accuracy and optimal cutoff point
- [PATCH] hvm: Correct RTC time offset update error due to tm->tm_year
- [PATCH] v2v: Use unitless methods for methods which don't change the internal state.