Hi, I have a problem in renaming R object and saving them within a loop. For ex: for (i in 1:length(all_files)) { uncov_GR <- "variable created in loop" filename <- paste0(sample_name[[i]],"_uncov", ".Rdata")) save(uncov_GR,file=filename) } Within the above short code (out of a long program), I want to change the name of "uncov_GR" variable to the file name and save it by same file name. But I am unable to come up with any solution to do so. I tried something like the code below: for (i in 1:length(all_files)) { uncov_GR <- "variable created in loop" filename <- paste0(sample_name[[i]],"_uncov", ".Rdata")) Objectout <- paste0(sample_name[[i]],"_uncov") assign(Objectout, uncov_GR) save(Objectout,file=filename) ### problem still persists with saving } Here I am able to make a copy of uncov_GR but I am unable to save the object again with the same filename. Any suggestions in this regard would be highly appreciated. Thanks. Regards, Kamal [[alternative HTML version deleted]]
This is not really a question for r-devel, but basically you should use saveRDS/readRDS rather than save/load. Hadley On Wednesday, April 30, 2014, Kamal <kamal.fartiyal84@gmail.com> wrote:> Hi, > > I have a problem in renaming R object and saving them within a loop. For > ex: > > for (i in 1:length(all_files)) > { > uncov_GR <- "variable created in loop" > filename <- paste0(sample_name[[i]],"_uncov", ".Rdata")) > save(uncov_GR,file=filename) > } > > Within the above short code (out of a long program), I want to change the > name of "uncov_GR" variable to the file name and save it by same file name. > But I am unable to come up with any solution to do so. > > I tried something like the code below: > > for (i in 1:length(all_files)) > { > uncov_GR <- "variable created in loop" > filename <- paste0(sample_name[[i]],"_uncov", ".Rdata")) > Objectout <- paste0(sample_name[[i]],"_uncov") > assign(Objectout, uncov_GR) > save(Objectout,file=filename) ### problem still persists with > saving > } > > Here I am able to make a copy of uncov_GR but I am unable to save the > object again with the same filename. Any suggestions in this regard would > be highly appreciated. > > Thanks. > > Regards, > Kamal > > [[alternative HTML version deleted]] > > ______________________________________________ > R-devel@r-project.org <javascript:;> mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >-- http://had.co.nz/ [[alternative HTML version deleted]]
save(list = Objectout, file = filename) On Wed, Apr 30, 2014 at 12:11 PM, Kamal <kamal.fartiyal84@gmail.com> wrote:> Hi, > > I have a problem in renaming R object and saving them within a loop. For > ex: > > for (i in 1:length(all_files)) > { > uncov_GR <- "variable created in loop" > filename <- paste0(sample_name[[i]],"_uncov", ".Rdata")) > save(uncov_GR,file=filename) > } > > Within the above short code (out of a long program), I want to change the > name of "uncov_GR" variable to the file name and save it by same file name. > But I am unable to come up with any solution to do so. > > I tried something like the code below: > > for (i in 1:length(all_files)) > { > uncov_GR <- "variable created in loop" > filename <- paste0(sample_name[[i]],"_uncov", ".Rdata")) > Objectout <- paste0(sample_name[[i]],"_uncov") > assign(Objectout, uncov_GR) > save(Objectout,file=filename) ### problem still persists with > saving > } > > Here I am able to make a copy of uncov_GR but I am unable to save the > object again with the same filename. Any suggestions in this regard would > be highly appreciated. > > Thanks. > > Regards, > Kamal > > [[alternative HTML version deleted]] > > ______________________________________________ > R-devel@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >[[alternative HTML version deleted]]