[R] writing R extension

Mike Prager mike.prager at noaa.gov
Fri Dec 22 18:45:40 CET 2006


ahmad ajakh <aajakh at yahoo.com> wrote:

> Hi all,
> I am dealing with the same issue here and I was wondering whether it would be possible to just save
> the R compliled function objects in a directory and just attach the directory to the search path.
> (I am using R2.4.0+ESS+Xemacs in windows XP). 
> 
> Thanks.
> AA.

Yes.  That is what I do with my own functions.  It is MUCH
simpler than writing a package, though not as functional (no
help pages for example.)

Make sure the workspace has only the functions you need, then
save it.  In your .Rprofile, you can put a line like

attach("d:/R/MHP/MHPmisc/.RData")

to add the workspace to the search path.  

This has the advantage that the functions don't show up when you
type ls() -- but they do when you type ls(nn), where nn is the
position of the added workspace on the search path.

I use the following script, stored in file 00make.r in the same
directory as the functions, to speed this up:

#==============================
## Script 00make.r   MHP
## This clears the current workspace, sources all the scripts
## found in the working directory, and then saves the
## workspace for use by later R sessions

# Clear all existing objects in workspace:
rm(list=ls())

# Make a list of all R source files in this directory:
flist = list.files(path = ".", pattern = ".+\.r")

# Remove from the list all files containing the string "00":
# Such files should be used for temporary functions or
# scripts like this one.
flist2 = flist[-grep("00", flist)]

# Source the files:
for (i in 1:length(flist2)) {
   cat("Sourcing", flist2[i],"\n")
   source(flist2[i])
   }
# Remove temporary objects:
rm(i, flist, flist2)
# Save workspace:
save.image()
# Write message to user:
cat("\nNOTE: The workspace has been saved with all
functions.\n",
    "     When exiting R, please do NOT save again.\n")
ls() in
#===============================

-- 
Mike Prager, NOAA, Beaufort, NC
* Opinions expressed are personal and not represented otherwise.
* Any use of tradenames does not constitute a NOAA endorsement.



More information about the R-help mailing list