Skip to content
Prev 106411 / 398513 Next

writing R extension

Sarah Goslee wrote:

            
Yikes No!

  That will load all the objects into the current workspace. If you save 
when you quit, you'll end up with umpteen copies of your package code!

  For simple bundles of functions, it would be better to use save() to 
save them all to a .RData-type file and then 'attach()' it. This way it 
doesn't get stuck in your workspace. So:

  > foo=function(x){x^2}
  > bar=function(y){y^6}
  > baz=function(z){z*3}

  > myFunctions=c("foo","bar","baz")
  > save(list=myFunctions,file="myFunctions.RData")

then quit R, start R in another workspace:

  > attach("/path/to/wherever/you/put/myFunctions.RData")
  > foo(2)
    [1] 4

  Building proper _packages_ (never call them 'libraries' - libraries 
are collections of packages) isn't that hard once you've done it a dozen 
times, although I'm starting the find the bondage and discipline of 
packaging R code getting to me.

Barry