Skip to content
Prev 106508 / 398513 Next

writing R extension

ahmad ajakh <aajakh at yahoo.com> wrote:

            
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
#===============================