[R] Writing R packages in an easier way?
Barry Rowlingson
b.rowlingson at lancaster.ac.uk
Fri Feb 11 12:03:44 CET 2011
On Fri, Feb 11, 2011 at 7:52 AM, Dr. Michael Wolf <m-wolf at muenster.de> wrote:
> Dear R colleagues,
>
> is there an easier way to write R packages for the own use - without RTools
> and TeX?
There are simpler ways of maintaining R source code than building
packages. I bashed out a quick way of keeping code in directories as
source and reading in on demand. All you need is two functions:
import <- function(dir){
e = attach(NULL,name=dir)
assign("__path__",dir,envir=e)
reload(e)
invisible(e)
}
reload <- function(e){
path = get("__path__",e)
files = list.files(path,".R$",full.names=TRUE,recursive=TRUE,ignore.case=TRUE)
for(f in files){
sys.source(f,envir=e)
}
}
Now put the source code in some folder/directory somewhere. You can
even make subdirs if you wish to organise it that way.
I've got some .R files in "lib1". I do;
> import("lib1")
and that runs 'source' on all the .R files in there and loads them
into position 2 on the search list. ls(pos=2) shows them.
If you edit the source code, just do reload(2) (assuming it hasn't
moved because you've loaded another package) and your stuff will be
updated. It just sources everything again. Do detach(2) to get rid of
it.
If you want to distribute your code in "source" format, just make a
zip or tar of the folder with the code in, and make sure your users
have the import and reload functions (I should probably put them in a
package one day...).
To distribute in 'binary' format, do an import, and then save as .RData:
> save(list=ls(pos=2),file="lib1.RData", envir=as.environment(2)) #
not well tested....
Then your users just need to attach("lib1.RData") to get your functions.
Now this scheme could get more complex - for example it doesn't deal
with C or Fortran code, or documentation, or tests, or examples, or
version numbering. Adding any of those would be pointless - if you
want any of that either use packages and learn to write R docs
(roxygen helps) or add it to my code yourself. You'll end up
rebuilding the R package system anyway.
Hope this helps. Doubtless someone else will come up with a simple
way to build proper packages without all the bondage and discipline
that annoys you.
Barry
More information about the R-help
mailing list