[R] Looping through file names and loading...
Duncan Murdoch
murdoch at stats.uwo.ca
Thu Apr 16 00:22:16 CEST 2009
greggallen at gmail.com wrote:
> Dear R-Community:
>
> I am assisting a community college instructor in an introductory
> statistics class.
>
> He has asked me to do this:
>
> Read in a file, of filenames, line by line:
>
> Student_models.txt : (contains 1500+ filenames)
> ________________________
> Experiment_name_student_name_date_time_Model_number_etc_etc
> Another_file_name_with_similar_info_as_above
> And_another
> And_another_one
> And_about_1500_more
> ..........
> ..........
>
>
> ________________________
>
> function(filename)# I pass in filename
> {
> for(i in 1:length(filename))
> {
> s <- filename[i,1]
> print(s) # I check the file name, and it is OK.
> load(s) # This returns all sorts of errors, most common is: object
> "mod" not found.
>
Calling load(s) here will load the objects from a saved image into the
local evaluation frame of your function. They could overwrite i, or s,
or filename, or any other local variables. Are you sure that's what you
want to do? I think it would be safer to do something like
e <- new.env()
load(s, e)
and then you've loaded things into e, not into your evaluation frame,
and they shouldn't mess you up. It makes it a bit more work to extract
things, but not that much more.
> ..................
> pred <- predict( s , test_vector) # I never get this far
>
Now this doesn't make sense: isn't "s" a filename?
Duncan Murdoch
> print(pred)
>
> write(filename, pred, "output.txt")
>
> .....
>
> }
>
> The filenames are correct, and I can use "load(s)" interactively, but
> it won't work in a function.
>
> Thanks for ANY help!!!!
>
> Gregg Allen
> USA
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
More information about the R-help
mailing list