[R] 'require' equivalent for local functions

Ken-JP kfmfe04 at gmail.com
Mon Mar 23 04:36:46 CET 2009


I agree with Duncan.  I used to do exactly what you did - source()ing data
files inside a wrapper not unlike C #define wrappers, but it became a
headache with more files and the files began looking more cluttered.

It has taken me several days to learn about how create a package properly,
along with package RUnit for unit-testing, and with documentation.  The "R
Extensions" file is often a good source of information.  Be sure you find
information about Rcmd install and Rcmd check, which are also very useful. 
prompt() can help you build your .Rd (help files).  Alternatively, you may
use Rdoc$compile() (from package R.oo) if you intend to embed your
Rdoc-style comments inside your R code, as I do.  I also use R.oo as a more
traditional object-oriented alternative to S3/S4.  Once set-up, you can
automagically generate .pdf files and .chm (windows-based help) for your
package.  Help for my own package has helped me keep my code consistent,
clean, and re-factorable.  Best of all, you can use put require( my.package
) or data( my.data) and voila.

It has been a bit of a learning curve, but the packaging facilities in R are
actually very well developed.  Once set-up, maintenance becomes less of a
chore.  Good luck.


Duncan Murdoch-2 wrote:
> 
> On 22/03/2009 5:05 PM, JiHO wrote:
>> Hello everyone,
>> 
>> I often create some local "libraries" of functions (.R files with only  
>> functions in them) that I latter call. In scripts that call a function  
>> from such library, I would like to be able to test whether the  
>> function is already known in the namespace and, only if it is not,  
>> source the library file. I.e. what `require` does for packages, I want  
>> to do with my local functions.
> 
> That's pretty hard to make bulletproof.  Why not just put those 
> functions in a package, and use that package?  If the functions are all 
> written in R, creating the package is very easy:  see package.skeleton. 
>   (And if you have a perfect memory and don't plan to distribute the 
> package to anyone, you can skip documenting the functions:  then it's 
> almost no work at all.)
> 
-- 
View this message in context: http://www.nabble.com/%27require%27-equivalent-for-local-functions-tp22650626p22653884.html
Sent from the R help mailing list archive at Nabble.com.




More information about the R-help mailing list