>>>>> 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.

    > ______________________________________________
    > 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.

Reply via email to