The Art of R Programming

(WallPaper) #1
Here, we found two objects whose names include the string"notebook"
and then asked to remove them, which was confirmed by the second call
tols().

NOTE You may find the functionbrowseEnv()helpful. It will show in your web browser your
globals (or objects in a different specified environment), with some details on each.

9.4.3 Saving a Collection of Objects with the save() Function.............


Callingsave()on a collection of objects will write them to disk for later
retrieval byload(). Here’s a quick example:

> z <- rnorm(100000)
> hz <- hist(z)
> save(hz,"hzfile")
> ls()
[1] "hz" "z"
> rm(hz)
> ls()
[1] "z"
> load("hzfile")
> ls()
[1] "hz" "z"
> plot(hz) # graph window pops up

Here, we generate some data and then draw a histogram of it. But
we also save the output ofhist()in a variable,hz. That variable is an object
(of class"histogram", of course). Anticipating that we will want to reuse this
object in a later R session, we use thesave()function to save the object to
the filehzfile. It can be reloaded in that future session viaload(). To demon-
strate this, we deliberately removed thehzobject, then calledload()to reload
it, and then calledls()to show that it had indeed been reloaded.
I once needed to read in a very large data file, each record of which
required processing. I then usedsave()to keep the R object version of the
processed file for future R sessions.

9.4.4 “What Is This?”.................................................


Developers often need to know the exact structure of the object returned by
a library function. If the documentation does not give sufficient details, what
can we do?
The following R functions may be helpful:


  • class(),mode()

  • names(),attributes()

  • unclass(),str()

  • edit()


228 Chapter 9

Free download pdf