

Hi, all.
I'm trying to automate some regression operations in R leaving the
possibility to modify the predictors in the regression.
For example, I have saved in a list the results and then exported as a txt
file, in which we can modify the predictors, putting for example
lm(y~x^2) instead of having lm(y~x) as in the original model.
Now, I need to import in R the txt file as a list to evaluate the model.
Is that possible?
I played around
with source() and file() but can't figure it out.
Thanks.
Grazia

M. Grazia Pittau, Ph.D.
PostDoctoral Research Fellow
Department of Statistics
Columbia University
1255 Amsterdam Avenue
New York, NY 10027
[hidden email]
Phone: 212.851.2160
Fax: 212.851.2164
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide! http://www.Rproject.org/postingguide.html


?dump ?source
But do you really need to save the fitted object as a txt file?
Saving/loading it in native format (?save ?load) and then using ?update
would seem to be more straightforward.
 Bert Gunter
Genentech NonClinical Statistics
South San Francisco, CA
"The business of the statistician is to catalyze the scientific learning
process."  George E. P. Box
> Original Message
> From: [hidden email]
> [mailto: [hidden email]] On Behalf Of
> [hidden email]
> Sent: Wednesday, May 10, 2006 8:24 AM
> To: [hidden email]
> Subject: [R] importing a list
>
> Hi, all.
> I'm trying to automate some regression operations in R leaving the
> possibility to modify the predictors in the regression.
>
> For example, I have saved in a list the results and then
> exported as a txt
> file, in which we can modify the predictors, putting for example
> lm(y~x^2) instead of having lm(y~x) as in the original model.
>
> Now, I need to import in R the txt file as a list to evaluate
> the model.
> Is that possible?
>
> I played around
> with source() and file() but can't figure it out.
>
> Thanks.
> Grazia
>
> 
> M. Grazia Pittau, Ph.D.
> PostDoctoral Research Fellow
> Department of Statistics
> Columbia University
> 1255 Amsterdam Avenue
> New York, NY 10027
>
> [hidden email]
> Phone: 212.851.2160
> Fax: 212.851.2164
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/rhelp> PLEASE do read the posting guide!
> http://www.Rproject.org/postingguide.html>
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide! http://www.Rproject.org/postingguide.html


Hello Grazia,
On May 10, 2006, at 10:23 AM, [hidden email] wrote:
> I'm trying to automate some regression operations in R leaving the
> possibility to modify the predictors in the regression.
>
> For example, I have saved in a list the results and then exported
> as a txt
> file, in which we can modify the predictors, putting for example
> lm(y~x^2) instead of having lm(y~x) as in the original model.
>
> Now, I need to import in R the txt file as a list to evaluate the
> model.
> Is that possible?
>
> I played around with source() and file() but can't figure it out.
What OS are you using? When you say the list is in a text file, how
is the text file structured, e.g. CSV, TSV, fixedwidth, other?
Here's an example of creating, writing, and reading a data.frame:
d < data.frame(cbind(x=1, y=1:10))
write.table(d, file="rdata.txt", sep="\t", na="", quote=FALSE,
row.names = FALSE)
d.new < read.delim("rdata.txt")
HTH.
Regards,
 Robert
http://www.cwelug.org/downloadsHelp others get OpenSource software. Distribute FLOSS
for Windows, Linux, *BSD, and MacOS X with BitTorrent
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide! http://www.Rproject.org/postingguide.html


Robert,
thanks first of all.
Actually, the txt file I want to read as a list is like that:
$y
$y$mod.1
[1] "impute.1.y=glm(I(y>0)~x+ z+ w,family=binomial(link =logit),data=
data)"
$y$mod.2
[1] "impute.2.y=lm(I(log (y))~x+ z+ w,subset=y>0)"
$x
[1] "impute.x=lm(x~y+ z+ w, data= data)"
$z
[1] "impute.z=lm(z~y+ x+ w,data= data)"
that is a list of 4 elements with the first element
a list as well.
To save the file I'm using this function:
list2ascii < function(x,
file=paste(deparse(substitute(x)),".txt",sep="")) {
tmp.wid = getOption("width")
options(width=10000)
sink(file)
print(x)
sink()
options(width=tmp.wid)
return(invisible(NULL))
}
So, I should read it using
read.fwf("models.txt", width=getOption("width"))
The problem is that I'm not getting back a list!
Probably I have to play with write.table?!!
thanks,
grazia
On Wed, 10 May 2006, Robert Citek wrote:
> Hello Grazia,
>
> On May 10, 2006, at 10:23 AM, [hidden email] wrote:
>> I'm trying to automate some regression operations in R leaving the
>> possibility to modify the predictors in the regression.
>>
>> For example, I have saved in a list the results and then exported as a txt
>> file, in which we can modify the predictors, putting for example
>> lm(y~x^2) instead of having lm(y~x) as in the original model.
>>
>> Now, I need to import in R the txt file as a list to evaluate the model.
>> Is that possible?
>>
>> I played around with source() and file() but can't figure it out.
>
> What OS are you using? When you say the list is in a text file, how is the
> text file structured, e.g. CSV, TSV, fixedwidth, other?
>
> Here's an example of creating, writing, and reading a data.frame:
>
> d < data.frame(cbind(x=1, y=1:10))
> write.table(d, file="rdata.txt", sep="\t", na="", quote=FALSE, row.names =
> FALSE)
> d.new < read.delim("rdata.txt")
>
> HTH.
>
> Regards,
>  Robert
> http://www.cwelug.org/downloads> Help others get OpenSource software. Distribute FLOSS
> for Windows, Linux, *BSD, and MacOS X with BitTorrent
>
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide! http://www.Rproject.org/postingguide.html


A search for the list2ascii() function, led me to this post by Mike
Prager:
http://finzi.psych.upenn.edu/R/Rhelp02a/archive/66335.htmlin which Mike specifically notes:
"To write it to a file that can be read by R, I would suggest using
"dput" instead."
Thus, instead of using list2ascii() on your list object, use:
sink("YourTextFile")
dput(YourListObject)
sink()
You can then reread the text file back into R using:
source("YourTextFile")
provided of course that you don't compromise the integrity of the file
content when you edit the model formulae.
See ?sink (or ?capture.output) and ?dput for more help here.
However, I wholeheartedly support Bert's recommendation that you use
save()/load()/update(), since these functions enable you to work with R
objects directly in their native format _within R_, as opposed to
messing around with textual representations in an external editor where
you risk introducing errors through typos.
R is an incredibly powerful environment, when you work within it to take
advantage of its power, rather then trying to work around it to obviate
it.
HTH,
Marc Schwartz
On Wed, 20060510 at 13:05 0400, [hidden email] wrote:
> Robert,
> thanks first of all.
> Actually, the txt file I want to read as a list is like that:
>
>
> $y
> $y$mod.1
> [1] "impute.1.y=glm(I(y>0)~x+ z+ w,family=binomial(link =logit),data=
> data)"
>
> $y$mod.2
> [1] "impute.2.y=lm(I(log (y))~x+ z+ w,subset=y>0)"
>
> $x
> [1] "impute.x=lm(x~y+ z+ w, data= data)"
>
> $z
> [1] "impute.z=lm(z~y+ x+ w,data= data)"
>
> that is a list of 4 elements with the first element
> a list as well.
>
> To save the file I'm using this function:
>
> list2ascii < function(x,
> file=paste(deparse(substitute(x)),".txt",sep="")) {
>
> tmp.wid = getOption("width")
> options(width=10000)
> sink(file)
> print(x)
> sink()
> options(width=tmp.wid)
> return(invisible(NULL))
> }
>
> So, I should read it using
> read.fwf("models.txt", width=getOption("width"))
>
> The problem is that I'm not getting back a list!
> Probably I have to play with write.table?!!
> thanks,
> grazia
>
>
>
>
> On Wed, 10 May 2006, Robert Citek wrote:
>
> > Hello Grazia,
> >
> > On May 10, 2006, at 10:23 AM, [hidden email] wrote:
> >> I'm trying to automate some regression operations in R leaving the
> >> possibility to modify the predictors in the regression.
> >>
> >> For example, I have saved in a list the results and then exported as a txt
> >> file, in which we can modify the predictors, putting for example
> >> lm(y~x^2) instead of having lm(y~x) as in the original model.
> >>
> >> Now, I need to import in R the txt file as a list to evaluate the model.
> >> Is that possible?
> >>
> >> I played around with source() and file() but can't figure it out.
> >
> > What OS are you using? When you say the list is in a text file, how is the
> > text file structured, e.g. CSV, TSV, fixedwidth, other?
> >
> > Here's an example of creating, writing, and reading a data.frame:
> >
> > d < data.frame(cbind(x=1, y=1:10))
> > write.table(d, file="rdata.txt", sep="\t", na="", quote=FALSE, row.names =
> > FALSE)
> > d.new < read.delim("rdata.txt")
> >
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide! http://www.Rproject.org/postingguide.html


On Wed, 20060510 at 13:19 0500, Marc Schwartz (via MN) wrote:
> A search for the list2ascii() function, led me to this post by Mike
> Prager:
>
> http://finzi.psych.upenn.edu/R/Rhelp02a/archive/66335.html>
> in which Mike specifically notes:
>
> "To write it to a file that can be read by R, I would suggest using
> "dput" instead."
>
>
> Thus, instead of using list2ascii() on your list object, use:
>
> sink("YourTextFile")
> dput(YourListObject)
> sink()
>
> You can then reread the text file back into R using:
>
> source("YourTextFile")
Actually, quick correction here. That should be:
NewListObject < dget("YourTextFile")
dget() will enable simple assignment of the textual input, whereas
source() will just evaluate it and assignment with source() creates a
list object, unless you use:
NewListObject < source("YourTextFile")$value
Marc
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide! http://www.Rproject.org/postingguide.html


Marc,
it's perfect!!!!
THANKS a lot for your help.
grazia
On Wed, 10 May 2006, Marc Schwartz (via MN) wrote:
> On Wed, 20060510 at 13:19 0500, Marc Schwartz (via MN) wrote:
>> A search for the list2ascii() function, led me to this post by Mike
>> Prager:
>>
>> http://finzi.psych.upenn.edu/R/Rhelp02a/archive/66335.html>>
>> in which Mike specifically notes:
>>
>> "To write it to a file that can be read by R, I would suggest using
>> "dput" instead."
>>
>>
>> Thus, instead of using list2ascii() on your list object, use:
>>
>> sink("YourTextFile")
>> dput(YourListObject)
>> sink()
>>
>> You can then reread the text file back into R using:
>>
>> source("YourTextFile")
>
> Actually, quick correction here. That should be:
>
> NewListObject < dget("YourTextFile")
>
> dget() will enable simple assignment of the textual input, whereas
> source() will just evaluate it and assignment with source() creates a
> list object, unless you use:
>
> NewListObject < source("YourTextFile")$value
>
> Marc
>
>
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide! http://www.Rproject.org/postingguide.html

