Skip to content

writing R extension

4 messages · AA, Sarah Goslee, ¨Tariq Khan +1 more

AA
#
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.


----- Original Message ----
From: Sarah Goslee <sarah.goslee at gmail.com>
To: michele de meo <vbarstat at gmail.com>
Cc: r-help at stat.math.ethz.ch
Sent: Wednesday, December 20, 2006 12:09:47 PM
Subject: Re: [R] writing R extension

For a very simple library, you really only need chapter 1 of the manual
on writing R extensions, which even describes the "helper functions"
that take some of the work out of making a "proper" package.

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.

Sarah
On 12/20/06, michele de meo <vbarstat at gmail.com> wrote:

  
    
#
Hi,
The other method that I'm familiar with uses save() and load() rather than
attach. It may be possible to use attach() in some other method.
[1] 9
[1] 6
# start R
character(0)
[1] "temp1" "temp2"


My personal preference is the text source file approach, but opinions
obviously vary on that. I do keep "final" versions in a package.

And yes, the proper term is package, but it seems that if the entity
in question is stored in  /usr/lib/R/library (on my linux system), and
loaded with the function library() the occasional slip might be forgiven?

Sarah
1 day later
#
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
#===============================