[R] writing R extension

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Wed Dec 20 18:31:09 CET 2006


Sarah Goslee wrote:

> If that's still too complex, you could also save your function to a file
> and load it as needed with source(). That will give the user the
> same effect.
> 
> source("/path/to/my/stuff/myfiles.R")
> 
> Since you didn't tell us OS or anything else about your system,
> it's hard to be more specific.
> 

  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



More information about the R-help mailing list