Re: [R] save() and load(): *prefer* saveRDS() and readRDS()
Hi Martin: good morning Thank you very much for your detailed explanation. Got it now. With many thanks Abou On Tue, Sep 26, 2023, 3:24 AM Martin Maechler wrote: > > Jeff Newmiller via R-help > > on Mon, 25 Sep 2023 18:46:02 -0700 writes: > > > You never created any object in R called irisdataTest. Objects in > the global environment have names that are unrelated to the names of files > on disk. > > The load function modifies an environment to create a variable named > as it was named in the environment from which it was saved. Thus, you > cannot simply load an object that was saved with one name into an object > named something else. It is possible to create a new environment to put the > loaded objects into, but I wouldn't recommend trying to explain how to do > that to a beginner. Rather, I would instead recommend using saveRDS and > readRDS instead to save/load exactly one object at a time without storing > the object name. > > > saveRDS( mtcars, "my_mtcars.rds" ) > > new_obj <- readRDS( "my_mtcars.rds" ) > > > I would also guide them to never save their environment when > prompted by R... the .RData file this creates will remember mistakes made > in previous sessions making troubleshooting very difficult later. Instead > they should focus on making a top-to-bottom script that has all their > analysis steps so they can start from scratch. > > Yes! > > And just re-iterating what Jeff mentioned above: > Notably when teaching, the use of saveRDS() and readRDS() > should be emphasized as safer / self-documenting, ... > for the case where there's just one object to save/load. > > *and* you can always put several objects into a list and > saveRDS() / readRDS() that. > > Note: Our pkg {sfsmisc} nowadays contains a nice utility function > list_() ==> help page online e.g. here > > https://search.r-project.org/CRAN/refmans/sfsmisc/html/list_named.html > > which comes were handy when you want to easily create a *named* > list from a bunch of objects, as e.g. above to be able to nicely use > > saveRDS(list_(obj1, obj2, table3, grob4, data5), > file = "allthings.rds") > > The cute utility is very simply defined as > > ##' list_(a, b, cc) creates a *named* list using the actual arguments' > names > list_ <- function(...) `names<-`(list(...), vapply(sys.call()[-1L], > as.character, "")) > > > > On September 25, 2023 6:23:01 PM PDT, AbouEl-Makarim Aboueissa < > abouelmakarim1...@gmail.com> wrote: > >> Dear ALL: > >> > >> I am teaching statistical packages class this semester, in R > programing I > >> am trying to explain the use of save() and load() with an example > using the > >> iris data. It seems that the save() function works, BUT when I > tried to > >> load the data back to R, it seems that there is a problem(s), I > could not > >> figure out what went wrong. > >> > >> Any help would be highly appreciated. > >> > >> > >> I saved the iris data in my computer in the text format, > "iris.with.head.txt > >> ". > >> > >> Here are my R codes: > >> > >>> irisdata<-read.table("G:/iris.with.head.txt", header=T) > >>> > >>> head(irisdata) > >> Sepal.Length Sepal.Width Petal.Length Petal.Width Species > >> 1 5.1 3.5 1.4 0.2 setosa > >> 2 4.9 3.0 1.4 0.2 setosa > >> 3 4.7 3.2 1.3 0.2 setosa > >> 4 4.6 3.1 1.5 0.2 setosa > >> 5 5.0 3.6 1.4 0.2 setosa > >> 6 5.4 3.9 1.7 0.4 setosa > >> > >> > >> > >> *# saving the data as an .rda* > >> > >> save(irisdata,file="G:/irisdataTest.rda") > >> > >> *# load the data back to R* > >> > >> load(file="G:/irisdataTest.rda") > >> > >> > >>> head(irisdataTest) > >> Error in head(irisdataTest) : object 'irisdataTest' not found > >> > >>> irisdataTest > >> Error: object 'irisdataTest' not found > >> > >> > >> > >> with many thanks > >> abou > >> __ > >> > >> > >> *AbouEl-Makarim Aboueissa, PhD* > >> > >> *Professor, Mathematics and Statistics* > >> *Graduate Coordinator* > >> > >> *Department of Mathematics and Statistics* > >> *University of Southern Maine* > >> > >> [[alternative HTML version deleted]] > >> > >> __ > >> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > >> 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. > > > -- > > Sent from my phone. Please excuse my brevity. > > > __
Re: [R] save() and load()
Hi Shu: good morning Thank you very much for your detailed explanation. Got it now. With many thanks Abou On Mon, Sep 25, 2023, 9:39 PM Shu Fai Cheung wrote: > Hi, > > You can try this: > > head(irisdata) > > Objects loaded by load() keep their names when being saved. In your > case, it is 'irisdata'. > > You can also use verbose = TRUE to show the names of objects loaded: > > load(file = "irisdataTest.RData", verbose = TRUE) > > Hope this helps. > > Regards, > Shu Fai > > On Tue, Sep 26, 2023 at 9:24 AM AbouEl-Makarim Aboueissa > wrote: > > > > Dear ALL: > > > > I am teaching statistical packages class this semester, in R programing I > > am trying to explain the use of save() and load() with an example using > the > > iris data. It seems that the save() function works, BUT when I tried to > > load the data back to R, it seems that there is a problem(s), I could not > > figure out what went wrong. > > > > Any help would be highly appreciated. > > > > > > I saved the iris data in my computer in the text format, > "iris.with.head.txt > > ". > > > > Here are my R codes: > > > > > irisdata<-read.table("G:/iris.with.head.txt", header=T) > > > > > > head(irisdata) > > Sepal.Length Sepal.Width Petal.Length Petal.Width Species > > 1 5.1 3.5 1.4 0.2 setosa > > 2 4.9 3.0 1.4 0.2 setosa > > 3 4.7 3.2 1.3 0.2 setosa > > 4 4.6 3.1 1.5 0.2 setosa > > 5 5.0 3.6 1.4 0.2 setosa > > 6 5.4 3.9 1.7 0.4 setosa > > > > > > > > *# saving the data as an .rda* > > > > save(irisdata,file="G:/irisdataTest.rda") > > > > *# load the data back to R* > > > > load(file="G:/irisdataTest.rda") > > > > > > >head(irisdataTest) > > Error in head(irisdataTest) : object 'irisdataTest' not found > > > > > irisdataTest > > Error: object 'irisdataTest' not found > > > > > > > > with many thanks > > abou > > __ > > > > > > *AbouEl-Makarim Aboueissa, PhD* > > > > *Professor, Mathematics and Statistics* > > *Graduate Coordinator* > > > > *Department of Mathematics and Statistics* > > *University of Southern Maine* > > > > [[alternative HTML version deleted]] > > > > __ > > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > > 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. > [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
Re: [R] save() and load()
Hi Jeff: good morning Thank you very much for your detailed explanation. Got it now. With many thanks Abou On Mon, Sep 25, 2023, 9:46 PM Jeff Newmiller wrote: > You never created any object in R called irisdataTest. Objects in the > global environment have names that are unrelated to the names of files on > disk. > > The load function modifies an environment to create a variable named as it > was named in the environment from which it was saved. Thus, you cannot > simply load an object that was saved with one name into an object named > something else. It is possible to create a new environment to put the > loaded objects into, but I wouldn't recommend trying to explain how to do > that to a beginner. Rather, I would instead recommend using saveRDS and > readRDS instead to save/load exactly one object at a time without storing > the object name. > > saveRDS( mtcars, "my_mtcars.rds" ) > new_obj <- readRDS( "my_mtcars.rds" ) > > I would also guide them to never save their environment when prompted by > R... the .RData file this creates will remember mistakes made in previous > sessions making troubleshooting very difficult later. Instead they should > focus on making a top-to-bottom script that has all their analysis steps so > they can start from scratch. > > On September 25, 2023 6:23:01 PM PDT, AbouEl-Makarim Aboueissa < > abouelmakarim1...@gmail.com> wrote: > >Dear ALL: > > > >I am teaching statistical packages class this semester, in R programing I > >am trying to explain the use of save() and load() with an example using > the > >iris data. It seems that the save() function works, BUT when I tried to > >load the data back to R, it seems that there is a problem(s), I could not > >figure out what went wrong. > > > >Any help would be highly appreciated. > > > > > >I saved the iris data in my computer in the text format, > "iris.with.head.txt > >". > > > >Here are my R codes: > > > >> irisdata<-read.table("G:/iris.with.head.txt", header=T) > >> > >> head(irisdata) > > Sepal.Length Sepal.Width Petal.Length Petal.Width Species > >1 5.1 3.5 1.4 0.2 setosa > >2 4.9 3.0 1.4 0.2 setosa > >3 4.7 3.2 1.3 0.2 setosa > >4 4.6 3.1 1.5 0.2 setosa > >5 5.0 3.6 1.4 0.2 setosa > >6 5.4 3.9 1.7 0.4 setosa > > > > > > > >*# saving the data as an .rda* > > > >save(irisdata,file="G:/irisdataTest.rda") > > > >*# load the data back to R* > > > >load(file="G:/irisdataTest.rda") > > > > > >>head(irisdataTest) > >Error in head(irisdataTest) : object 'irisdataTest' not found > > > >> irisdataTest > >Error: object 'irisdataTest' not found > > > > > > > >with many thanks > >abou > >__ > > > > > >*AbouEl-Makarim Aboueissa, PhD* > > > >*Professor, Mathematics and Statistics* > >*Graduate Coordinator* > > > >*Department of Mathematics and Statistics* > >*University of Southern Maine* > > > > [[alternative HTML version deleted]] > > > >__ > >R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > >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. > > -- > Sent from my phone. Please excuse my brevity. > [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
Re: [R] save() and load(): *prefer* saveRDS() and readRDS()
> Jeff Newmiller via R-help > on Mon, 25 Sep 2023 18:46:02 -0700 writes: > You never created any object in R called irisdataTest. Objects in the global environment have names that are unrelated to the names of files on disk. > The load function modifies an environment to create a variable named as it was named in the environment from which it was saved. Thus, you cannot simply load an object that was saved with one name into an object named something else. It is possible to create a new environment to put the loaded objects into, but I wouldn't recommend trying to explain how to do that to a beginner. Rather, I would instead recommend using saveRDS and readRDS instead to save/load exactly one object at a time without storing the object name. > saveRDS( mtcars, "my_mtcars.rds" ) > new_obj <- readRDS( "my_mtcars.rds" ) > I would also guide them to never save their environment when prompted by R... the .RData file this creates will remember mistakes made in previous sessions making troubleshooting very difficult later. Instead they should focus on making a top-to-bottom script that has all their analysis steps so they can start from scratch. Yes! And just re-iterating what Jeff mentioned above: Notably when teaching, the use of saveRDS() and readRDS() should be emphasized as safer / self-documenting, ... for the case where there's just one object to save/load. *and* you can always put several objects into a list and saveRDS() / readRDS() that. Note: Our pkg {sfsmisc} nowadays contains a nice utility function list_() ==> help page online e.g. here https://search.r-project.org/CRAN/refmans/sfsmisc/html/list_named.html which comes were handy when you want to easily create a *named* list from a bunch of objects, as e.g. above to be able to nicely use saveRDS(list_(obj1, obj2, table3, grob4, data5), file = "allthings.rds") The cute utility is very simply defined as ##' list_(a, b, cc) creates a *named* list using the actual arguments' names list_ <- function(...) `names<-`(list(...), vapply(sys.call()[-1L], as.character, "")) > On September 25, 2023 6:23:01 PM PDT, AbouEl-Makarim Aboueissa wrote: >> Dear ALL: >> >> I am teaching statistical packages class this semester, in R programing I >> am trying to explain the use of save() and load() with an example using the >> iris data. It seems that the save() function works, BUT when I tried to >> load the data back to R, it seems that there is a problem(s), I could not >> figure out what went wrong. >> >> Any help would be highly appreciated. >> >> >> I saved the iris data in my computer in the text format, "iris.with.head.txt >> ". >> >> Here are my R codes: >> >>> irisdata<-read.table("G:/iris.with.head.txt", header=T) >>> >>> head(irisdata) >> Sepal.Length Sepal.Width Petal.Length Petal.Width Species >> 1 5.1 3.5 1.4 0.2 setosa >> 2 4.9 3.0 1.4 0.2 setosa >> 3 4.7 3.2 1.3 0.2 setosa >> 4 4.6 3.1 1.5 0.2 setosa >> 5 5.0 3.6 1.4 0.2 setosa >> 6 5.4 3.9 1.7 0.4 setosa >> >> >> >> *# saving the data as an .rda* >> >> save(irisdata,file="G:/irisdataTest.rda") >> >> *# load the data back to R* >> >> load(file="G:/irisdataTest.rda") >> >> >>> head(irisdataTest) >> Error in head(irisdataTest) : object 'irisdataTest' not found >> >>> irisdataTest >> Error: object 'irisdataTest' not found >> >> >> >> with many thanks >> abou >> __ >> >> >> *AbouEl-Makarim Aboueissa, PhD* >> >> *Professor, Mathematics and Statistics* >> *Graduate Coordinator* >> >> *Department of Mathematics and Statistics* >> *University of Southern Maine* >> >> [[alternative HTML version deleted]] >> >> __ >> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see >> 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. > -- > Sent from my phone. Please excuse my brevity. > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. __ R-help@r-project.org mailing list -- To UNS
Re: [R] save() and load()
You never created any object in R called irisdataTest. Objects in the global environment have names that are unrelated to the names of files on disk. The load function modifies an environment to create a variable named as it was named in the environment from which it was saved. Thus, you cannot simply load an object that was saved with one name into an object named something else. It is possible to create a new environment to put the loaded objects into, but I wouldn't recommend trying to explain how to do that to a beginner. Rather, I would instead recommend using saveRDS and readRDS instead to save/load exactly one object at a time without storing the object name. saveRDS( mtcars, "my_mtcars.rds" ) new_obj <- readRDS( "my_mtcars.rds" ) I would also guide them to never save their environment when prompted by R... the .RData file this creates will remember mistakes made in previous sessions making troubleshooting very difficult later. Instead they should focus on making a top-to-bottom script that has all their analysis steps so they can start from scratch. On September 25, 2023 6:23:01 PM PDT, AbouEl-Makarim Aboueissa wrote: >Dear ALL: > >I am teaching statistical packages class this semester, in R programing I >am trying to explain the use of save() and load() with an example using the >iris data. It seems that the save() function works, BUT when I tried to >load the data back to R, it seems that there is a problem(s), I could not >figure out what went wrong. > >Any help would be highly appreciated. > > >I saved the iris data in my computer in the text format, "iris.with.head.txt >". > >Here are my R codes: > >> irisdata<-read.table("G:/iris.with.head.txt", header=T) >> >> head(irisdata) > Sepal.Length Sepal.Width Petal.Length Petal.Width Species >1 5.1 3.5 1.4 0.2 setosa >2 4.9 3.0 1.4 0.2 setosa >3 4.7 3.2 1.3 0.2 setosa >4 4.6 3.1 1.5 0.2 setosa >5 5.0 3.6 1.4 0.2 setosa >6 5.4 3.9 1.7 0.4 setosa > > > >*# saving the data as an .rda* > >save(irisdata,file="G:/irisdataTest.rda") > >*# load the data back to R* > >load(file="G:/irisdataTest.rda") > > >>head(irisdataTest) >Error in head(irisdataTest) : object 'irisdataTest' not found > >> irisdataTest >Error: object 'irisdataTest' not found > > > >with many thanks >abou >__ > > >*AbouEl-Makarim Aboueissa, PhD* > >*Professor, Mathematics and Statistics* >*Graduate Coordinator* > >*Department of Mathematics and Statistics* >*University of Southern Maine* > > [[alternative HTML version deleted]] > >__ >R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see >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. -- Sent from my phone. Please excuse my brevity. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
Re: [R] save() and load()
Hi, You can try this: head(irisdata) Objects loaded by load() keep their names when being saved. In your case, it is 'irisdata'. You can also use verbose = TRUE to show the names of objects loaded: load(file = "irisdataTest.RData", verbose = TRUE) Hope this helps. Regards, Shu Fai On Tue, Sep 26, 2023 at 9:24 AM AbouEl-Makarim Aboueissa wrote: > > Dear ALL: > > I am teaching statistical packages class this semester, in R programing I > am trying to explain the use of save() and load() with an example using the > iris data. It seems that the save() function works, BUT when I tried to > load the data back to R, it seems that there is a problem(s), I could not > figure out what went wrong. > > Any help would be highly appreciated. > > > I saved the iris data in my computer in the text format, "iris.with.head.txt > ". > > Here are my R codes: > > > irisdata<-read.table("G:/iris.with.head.txt", header=T) > > > > head(irisdata) > Sepal.Length Sepal.Width Petal.Length Petal.Width Species > 1 5.1 3.5 1.4 0.2 setosa > 2 4.9 3.0 1.4 0.2 setosa > 3 4.7 3.2 1.3 0.2 setosa > 4 4.6 3.1 1.5 0.2 setosa > 5 5.0 3.6 1.4 0.2 setosa > 6 5.4 3.9 1.7 0.4 setosa > > > > *# saving the data as an .rda* > > save(irisdata,file="G:/irisdataTest.rda") > > *# load the data back to R* > > load(file="G:/irisdataTest.rda") > > > >head(irisdataTest) > Error in head(irisdataTest) : object 'irisdataTest' not found > > > irisdataTest > Error: object 'irisdataTest' not found > > > > with many thanks > abou > __ > > > *AbouEl-Makarim Aboueissa, PhD* > > *Professor, Mathematics and Statistics* > *Graduate Coordinator* > > *Department of Mathematics and Statistics* > *University of Southern Maine* > > [[alternative HTML version deleted]] > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
Re: [R] save and load in R
Hi, Check also saveObject() and loadObject() from R.utils: library(R.utils) outfilenames<- paste ("./file", 1:numFiles, '.Rbin', sep="") for ( i in 1:10) { dataFile = read.table (filenames[i], header=TRUE, sep='\t'); saveObject (dataFile, file = outfilenames[i]) } And then: file1 = loadObject (file = './file1.Rdata') HTH, Ivan Le 6/19/2011 17:07, Mary Kindall a écrit : Thanks Jeff and Duncan Assign worked for me. I did not check the other methods suggested by you. I am repreducing my code below: ## filenames = list.files(path = ".", pattern = '.txt$', all.files = FALSE, full.names = TRUE, ignore.case = FALSE) #all input files numFiles = length(filenames) outfilenames<- paste("./file", 1:numFiles, '.Rdata', sep="") # output files for ( i in 1:numFiles) { dataFile = read.table(filenames[i], header=TRUE, sep='\t'); save(dataFile, file = outfilenames[i]) #Saving into the output files } newnames<- paste("file", 1:numFiles, sep="") #output variables to load into for ( i in 1:numFiles) { load(file = outfilenames[i]); assign(newnames[i], dataFile) #assign into corresponding output variables; } # Regards - M On Sun, Jun 19, 2011 at 10:38 AM, Duncan Murdoch wrote: On 11-06-19 10:26 AM, Mary Kindall wrote: I have a list of txt files that I want to convert into .rdata R data object. filenames 1. "./file1.txt" 2. "./file2.txt" 3. "./file3.txt" 4. "./file4.txt" 5. "./file5.txt" 6. "./file6.txt" 7. "./file7.txt" 8. "./file8.txt" 9. "./file9.txt" 10. "./file10.txt" I saved these files as for ( i in 1:10) { dataFile = read.table(filenames[i], header=TRUE, sep='\t'); save (dataFile, file = outfilenames[i]) } The inpt files are saves as: outfilenames 1. "./file1.Rdata" 2. "./file2.Rdata" 3. "./file3.Rdata" 4. "./file4.Rdata" 5. "./file5.Rdata" 6. "./file6.Rdata" 7. "./file7.Rdata" 8. "./file8.Rdata" 9. "./file9.Rdata" 10. "./file10.Rdata" Now I want to load these out files in such a way that the data is loaded into a variable that is same as the file name without extension. file1 = load (file = './file1.Rdata') file2 = load (file = './file2.Rdata') file3 = load (file = './file3.Rdata') file4 = load (file = './file4.Rdata') How can I do that. When you load() a file, the variables in it are restored with the same names that were saved. So you would need something like newnames<- paste("file", 1:10, sep="") # file1, file2, etc. for (i in 1:10) { load(file=outfilenames[i]) # assuming that's still around... assign(newnames[i], dataFile) } It would be a little simpler to use saveRDS() and readRDS() to save and load your files. They don't save the object names. A more R-like version of this would be to create a list of datasets, e.g. files<- list() for (i in 1:10) { load(file=outfilesnames[i]) files[[i]]<- dataFile } Then you don't end up creating 10 objects, but you can still access them separately. Duncan Murdoch -- Ivan CALANDRA PhD Student University of Hamburg Biozentrum Grindel und Zoologisches Museum Dept. Mammalogy Martin-Luther-King-Platz 3 D-20146 Hamburg, GERMANY +49(0)40 42838 6231 ivan.calan...@uni-hamburg.de ** http://www.for771.uni-bonn.de http://webapp5.rrz.uni-hamburg.de/mammals/eng/1525_8_1.php __ R-help@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.
Re: [R] save and load in R
Thanks Jeff and Duncan Assign worked for me. I did not check the other methods suggested by you. I am repreducing my code below: ## filenames = list.files(path = ".", pattern = '.txt$', all.files = FALSE, full.names = TRUE, ignore.case = FALSE) #all input files numFiles = length(filenames) outfilenames <- paste("./file", 1:numFiles, '.Rdata', sep="") # output files for ( i in 1:numFiles) { dataFile = read.table(filenames[i], header=TRUE, sep='\t'); save(dataFile, file = outfilenames[i]) #Saving into the output files } newnames <- paste("file", 1:numFiles, sep="") #output variables to load into for ( i in 1:numFiles) { load(file = outfilenames[i]); assign(newnames[i], dataFile) #assign into corresponding output variables; } # Regards - M On Sun, Jun 19, 2011 at 10:38 AM, Duncan Murdoch wrote: > On 11-06-19 10:26 AM, Mary Kindall wrote: > >> I have a list of txt files that I want to convert into .rdata R data >> object. >> >> filenames >> 1. "./file1.txt" >> 2. "./file2.txt" >> 3. "./file3.txt" >> 4. "./file4.txt" >> 5. "./file5.txt" >> 6. "./file6.txt" >> 7. "./file7.txt" >> 8. "./file8.txt" >> 9. "./file9.txt" >> 10. "./file10.txt" >> >> I saved these files as >> >> for ( i in 1:10) >> { >> dataFile = read.table(filenames[i], header=TRUE, sep='\t'); >> save (dataFile, file = outfilenames[i]) >> } >> >> The inpt files are saves as: >> outfilenames >> 1. "./file1.Rdata" >> 2. "./file2.Rdata" >> 3. "./file3.Rdata" >> 4. "./file4.Rdata" >> 5. "./file5.Rdata" >> 6. "./file6.Rdata" >> 7. "./file7.Rdata" >> 8. "./file8.Rdata" >> 9. "./file9.Rdata" >> 10. "./file10.Rdata" >> >> >> Now I want to load these out files in such a way that the data is loaded >> into a variable that is same as the file name without extension. >> >> file1 = load (file = './file1.Rdata') >> file2 = load (file = './file2.Rdata') >> file3 = load (file = './file3.Rdata') >> file4 = load (file = './file4.Rdata') >> >> How can I do that. >> > > When you load() a file, the variables in it are restored with the same > names that were saved. So you would need something like > > newnames <- paste("file", 1:10, sep="") # file1, file2, etc. > > > for (i in 1:10) { > load(file=outfilenames[i]) # assuming that's still around... > assign(newnames[i], dataFile) > } > > It would be a little simpler to use saveRDS() and readRDS() to save and > load your files. They don't save the object names. > > A more R-like version of this would be to create a list of datasets, e.g. > > files <- list() > > for (i in 1:10) { > load(file=outfilesnames[i]) > files[[i]] <- dataFile > } > > Then you don't end up creating 10 objects, but you can still access them > separately. > > Duncan Murdoch > > -- - Mary Kindall Yorktown Heights, NY USA [[alternative HTML version deleted]] __ R-help@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.
Re: [R] save and load in R
On 11-06-19 10:26 AM, Mary Kindall wrote: I have a list of txt files that I want to convert into .rdata R data object. filenames 1. "./file1.txt" 2. "./file2.txt" 3. "./file3.txt" 4. "./file4.txt" 5. "./file5.txt" 6. "./file6.txt" 7. "./file7.txt" 8. "./file8.txt" 9. "./file9.txt" 10. "./file10.txt" I saved these files as for ( i in 1:10) { dataFile = read.table(filenames[i], header=TRUE, sep='\t'); save (dataFile, file = outfilenames[i]) } The inpt files are saves as: outfilenames 1. "./file1.Rdata" 2. "./file2.Rdata" 3. "./file3.Rdata" 4. "./file4.Rdata" 5. "./file5.Rdata" 6. "./file6.Rdata" 7. "./file7.Rdata" 8. "./file8.Rdata" 9. "./file9.Rdata" 10. "./file10.Rdata" Now I want to load these out files in such a way that the data is loaded into a variable that is same as the file name without extension. file1 = load (file = './file1.Rdata') file2 = load (file = './file2.Rdata') file3 = load (file = './file3.Rdata') file4 = load (file = './file4.Rdata') How can I do that. When you load() a file, the variables in it are restored with the same names that were saved. So you would need something like newnames <- paste("file", 1:10, sep="") # file1, file2, etc. for (i in 1:10) { load(file=outfilenames[i]) # assuming that's still around... assign(newnames[i], dataFile) } It would be a little simpler to use saveRDS() and readRDS() to save and load your files. They don't save the object names. A more R-like version of this would be to create a list of datasets, e.g. files <- list() for (i in 1:10) { load(file=outfilesnames[i]) files[[i]] <- dataFile } Then you don't end up creating 10 objects, but you can still access them separately. Duncan Murdoch __ R-help@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.
Re: [R] save and load user modifications of options
Thanks David! I got a solution, though maybe not optimal. In the zzz.r file of the R package, I include the folloing code: .onLoad <- function(libname,pkgname) { myoptions <- list(ngs.python="sage -python",ngs.macs="macs14") if(exists("myoptions",.GlobalEnv)) options(.GlobalEnv$myoptions) else options(myoptions) } If user save his options in the myoptions list, by loading saved image he does not need to set the option again. However, I still believe there is a more elegant way to let user save his options. Hope someone could come up with a better solution :-) Thanks! Xue On Sat, May 7, 2011 at 5:47 AM, David Winsemius wrote: > > On May 6, 2011, at 8:59 PM, xwang14 wrote: > > Hi, >> >> Could anyone help me with the following problem? After I finished a R >> session and modified some options (for example, set >> options(editor="neditor")), I want save these modifications so that after >> I >> load the saved R data I do not need to modify these options again. What is >> the best way to do this? Is there a way without using an external file? I >> tried save.image. However, only default setting is perserved. >> > > You need to read about your options about setting options at startup. You > can find information in the Installation Manual or by typing: ?Startup > > (I cannot think of a manner in which this can be tied to a data file being > loaded.) > > -- > > David Winsemius, MD > West Hartford, CT > > [[alternative HTML version deleted]] __ R-help@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.
Re: [R] save and load user modifications of options
On May 6, 2011, at 8:59 PM, xwang14 wrote: Hi, Could anyone help me with the following problem? After I finished a R session and modified some options (for example, set options(editor="neditor")), I want save these modifications so that after I load the saved R data I do not need to modify these options again. What is the best way to do this? Is there a way without using an external file? I tried save.image. However, only default setting is perserved. You need to read about your options about setting options at startup. You can find information in the Installation Manual or by typing: ? Startup (I cannot think of a manner in which this can be tied to a data file being loaded.) -- David Winsemius, MD West Hartford, CT __ R-help@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.
Re: [R] Save and load workspace in R: strange error.
Hello, I get the same message when I write the attach function, where is that you change the user quota? I use R with mac OS Thanks. Hongxiao Zhu wrote: > > Hi, > > I finally figured out where the problem is. It was because the > user account has a 10GB quota for the systerm that I used. > Once the space limit is reached, R will have this error whenever you > want to save or load. > Gosh! It took me so long to realize this. > > Hongxiao > ** > * Hongxiao Zhu * > * Department of Statistics, Rice Univeristy * > * Office: DH 3136, Phone: 713-348-2839 * > * http://www.stat.rice.edu/~hxzhu/ * > ** > > On Wed, 3 Oct 2007, Tony Plate wrote: > >> Did you check whether 'junk4.RData' was created and what its length was - >> maybe an empty file is being created. Is there some sort of quota or >> permissions problem? My suggestion would be to look at the size and >> permissions on the directory and the file. If you need more help, I >> would >> suggest posting more details back to the list, e.g., what OS you are >> using, >> and a directory listing that shows file sizes and permissions (i.e., as >> you >> get with 'ls -l' on Unix systems.) >> >> -- Tony Plate >> >> Hongxiao Zhu wrote: >>> Hi, >>> >>> I tried to load a .RData object on unix system using R, it gives error: >>> >>> Error: restore file may be empty -- no data loaded >>> In addition: Warning message: >>> file 'junk3.RData' has magic number '' >>> Use of save versions prior to 2 is deprecated >>> >>> This happens only for using MY user account for the Unix system. I tried >>> to use a friends's user account to load the same data object, it is >>> fine. And it never happened to me before until sometime last week. >>> And This error happens even when I generate a simple random number >>> from my user account and save it, and load it again.(So obviously it is >>> not a R version mismatch problem). Does anybody know what happened? >>> >>> Here is an example what happened: >>> x=rnorm(100) save.image('junk4.RData') load('junk4.RData') >>> Error: restore file may be empty -- no data loaded >>> In addition: Warning message: >>> file 'junk4.RData' has magic number '' >>> Use of save versions prior to 2 is deprecated >>> >>> Thanks for any suggestion. >>> >>> Hongxiao >>> >>> >>> ** >>> * Hongxiao Zhu * >>> * Department of Statistics, Rice Univeristy * >>> * Office: DH 3136, Phone: 713-348-2839 * >>> * http://www.stat.rice.edu/~hxzhu/ * >>> >>> __ >>> R-help@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. >>> >> >> >> !DSPAM:4703b16f15261021468! >> > > __ > R-help@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. > > -- View this message in context: http://old.nabble.com/Save-and-load-workspace-in-R%3A-strange-error.-tp12968832p26418491.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] Save and load workspace in R: strange error.
Hi, I finally figured out where the problem is. It was because the user account has a 10GB quota for the systerm that I used. Once the space limit is reached, R will have this error whenever you want to save or load. Gosh! It took me so long to realize this. Hongxiao ** * Hongxiao Zhu * * Department of Statistics, Rice Univeristy * * Office: DH 3136, Phone: 713-348-2839 * * http://www.stat.rice.edu/~hxzhu/ * ** On Wed, 3 Oct 2007, Tony Plate wrote: > Did you check whether 'junk4.RData' was created and what its length was - > maybe an empty file is being created. Is there some sort of quota or > permissions problem? My suggestion would be to look at the size and > permissions on the directory and the file. If you need more help, I would > suggest posting more details back to the list, e.g., what OS you are using, > and a directory listing that shows file sizes and permissions (i.e., as you > get with 'ls -l' on Unix systems.) > > -- Tony Plate > > Hongxiao Zhu wrote: >> Hi, >> >> I tried to load a .RData object on unix system using R, it gives error: >> >> Error: restore file may be empty -- no data loaded >> In addition: Warning message: >> file 'junk3.RData' has magic number '' >> Use of save versions prior to 2 is deprecated >> >> This happens only for using MY user account for the Unix system. I tried >> to use a friends's user account to load the same data object, it is >> fine. And it never happened to me before until sometime last week. >> And This error happens even when I generate a simple random number >> from my user account and save it, and load it again.(So obviously it is >> not a R version mismatch problem). Does anybody know what happened? >> >> Here is an example what happened: >> >>> x=rnorm(100) >>> save.image('junk4.RData') >>> load('junk4.RData') >> Error: restore file may be empty -- no data loaded >> In addition: Warning message: >> file 'junk4.RData' has magic number '' >> Use of save versions prior to 2 is deprecated >> >> Thanks for any suggestion. >> >> Hongxiao >> >> >> ** >> * Hongxiao Zhu * >> * Department of Statistics, Rice Univeristy * >> * Office: DH 3136, Phone: 713-348-2839 * >> * http://www.stat.rice.edu/~hxzhu/ * >> >> __ >> R-help@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. >> > > > !DSPAM:4703b16f15261021468! > __ R-help@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.
Re: [R] Save and load workspace in R: strange error.
Sounds like you are having permissions problems. And as you're using a mix of Unix and WinXP, you might be suffering from some strange permissions settings. WinXP allows a very rich set of permissions which for many exotic combinations have no corresponding mapping to the much simpler 9-octet Unix permissions space -- so the limited view provided by the Unix permission string -rw-r--r-- may not really reflect what you can do with the file. On my own system, where I use WinXP and cygwin, I've sometimes seem very strange Windows permission sets that look OK in cygwin, but essentially disable use of a particular file. I generally fix this by resetting ownership and all permissions using Windows dialogs. It sounds like you need to get your sysadmins to help you sort this problem out. -- Tony Plate Hongxiao Zhu wrote: > Tony, > > Thanks for return. Actually, the data object 'junk4.RData' was created > but have size 0. It seems no data was saved. But the real data that I > want to load have data in it, which I can't load use my own user > account. But if using other people's user account under the same > system, it can be loaded. > > All the files has the following property if I use ls -l: > -rw-r--r-- > > The OS I used is windows xp. But I use SSH to connect to the unix server. > > I have been using this server for a long time, this error happened > since some day and from then on, I can never load/save workspace. > > Hong > > ** > * Hongxiao Zhu * > * Department of Statistics, Rice Univeristy * > * Office: DH 3136, Phone: 713-348-2839 * > * http://www.stat.rice.edu/~hxzhu/ * > ** > > On Wed, 3 Oct 2007, Tony Plate wrote: > >> Did you check whether 'junk4.RData' was created and what its length >> was - maybe an empty file is being created. Is there some sort of >> quota or permissions problem? My suggestion would be to look at the >> size and permissions on the directory and the file. If you need more >> help, I would suggest posting more details back to the list, e.g., >> what OS you are using, and a directory listing that shows file sizes >> and permissions (i.e., as you get with 'ls -l' on Unix systems.) >> >> -- Tony Plate >> >> Hongxiao Zhu wrote: >>> Hi, >>> >>> I tried to load a .RData object on unix system using R, it gives error: >>> >>> Error: restore file may be empty -- no data loaded >>> In addition: Warning message: >>> file 'junk3.RData' has magic number '' >>> Use of save versions prior to 2 is deprecated >>> >>> This happens only for using MY user account for the Unix system. I >>> tried to use a friends's user account to load the same data object, >>> it is >>> fine. And it never happened to me before until sometime last week. >>> And This error happens even when I generate a simple random number >>> from my user account and save it, and load it again.(So obviously it >>> is not a R version mismatch problem). Does anybody know what happened? >>> >>> Here is an example what happened: >>> x=rnorm(100) save.image('junk4.RData') load('junk4.RData') >>> Error: restore file may be empty -- no data loaded >>> In addition: Warning message: >>> file 'junk4.RData' has magic number '' >>> Use of save versions prior to 2 is deprecated >>> >>> Thanks for any suggestion. >>> >>> Hongxiao >>> >>> >>> ** >>> * Hongxiao Zhu * >>> * Department of Statistics, Rice Univeristy * >>> * Office: DH 3136, Phone: 713-348-2839 * >>> * http://www.stat.rice.edu/~hxzhu/ * >>> >>> __ >>> R-help@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. >>> >> >> >> !DSPAM:4703b16f15261021468! >> > __ R-help@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.
Re: [R] Save and load workspace in R: strange error.
Tony, Thanks for return. Actually, the data object 'junk4.RData' was created but have size 0. It seems no data was saved. But the real data that I want to load have data in it, which I can't load use my own user account. But if using other people's user account under the same system, it can be loaded. All the files has the following property if I use ls -l: -rw-r--r-- The OS I used is windows xp. But I use SSH to connect to the unix server. I have been using this server for a long time, this error happened since some day and from then on, I can never load/save workspace. Hong ** * Hongxiao Zhu * * Department of Statistics, Rice Univeristy * * Office: DH 3136, Phone: 713-348-2839 * * http://www.stat.rice.edu/~hxzhu/ * ** On Wed, 3 Oct 2007, Tony Plate wrote: > Did you check whether 'junk4.RData' was created and what its length was - > maybe an empty file is being created. Is there some sort of quota or > permissions problem? My suggestion would be to look at the size and > permissions on the directory and the file. If you need more help, I would > suggest posting more details back to the list, e.g., what OS you are using, > and a directory listing that shows file sizes and permissions (i.e., as you > get with 'ls -l' on Unix systems.) > > -- Tony Plate > > Hongxiao Zhu wrote: >> Hi, >> >> I tried to load a .RData object on unix system using R, it gives error: >> >> Error: restore file may be empty -- no data loaded >> In addition: Warning message: >> file 'junk3.RData' has magic number '' >> Use of save versions prior to 2 is deprecated >> >> This happens only for using MY user account for the Unix system. I tried to >> use a friends's user account to load the same data object, it is >> fine. And it never happened to me before until sometime last week. >> And This error happens even when I generate a simple random number >> from my user account and save it, and load it again.(So obviously it is not >> a R version mismatch problem). Does anybody know what happened? >> >> Here is an example what happened: >> >>> x=rnorm(100) >>> save.image('junk4.RData') >>> load('junk4.RData') >> Error: restore file may be empty -- no data loaded >> In addition: Warning message: >> file 'junk4.RData' has magic number '' >> Use of save versions prior to 2 is deprecated >> >> Thanks for any suggestion. >> >> Hongxiao >> >> >> ** >> * Hongxiao Zhu * >> * Department of Statistics, Rice Univeristy * >> * Office: DH 3136, Phone: 713-348-2839 * >> * http://www.stat.rice.edu/~hxzhu/ * >> >> __ >> R-help@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. >> > > > !DSPAM:4703b16f15261021468! > __ R-help@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.
Re: [R] Save and load workspace in R: strange error.
Did you check whether 'junk4.RData' was created and what its length was - maybe an empty file is being created. Is there some sort of quota or permissions problem? My suggestion would be to look at the size and permissions on the directory and the file. If you need more help, I would suggest posting more details back to the list, e.g., what OS you are using, and a directory listing that shows file sizes and permissions (i.e., as you get with 'ls -l' on Unix systems.) -- Tony Plate Hongxiao Zhu wrote: > Hi, > > I tried to load a .RData object on unix system using R, it gives error: > > Error: restore file may be empty -- no data loaded > In addition: Warning message: > file 'junk3.RData' has magic number '' > Use of save versions prior to 2 is deprecated > > This happens only for using MY user account for the Unix system. I > tried to use a friends's user account to load the same data object, it is > fine. And it never happened to me before until sometime last week. > And This error happens even when I generate a simple random number > from my user account and save it, and load it again.(So obviously it is > not a R version mismatch problem). Does anybody know what happened? > > Here is an example what happened: > >> x=rnorm(100) >> save.image('junk4.RData') >> load('junk4.RData') > Error: restore file may be empty -- no data loaded > In addition: Warning message: > file 'junk4.RData' has magic number '' > Use of save versions prior to 2 is deprecated > > Thanks for any suggestion. > > Hongxiao > > > ** > * Hongxiao Zhu * > * Department of Statistics, Rice Univeristy * > * Office: DH 3136, Phone: 713-348-2839 * > * http://www.stat.rice.edu/~hxzhu/ * > > __ > R-help@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. > __ R-help@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.