I'd like to develop a simple library in R in which to save my particular functions. I have read the manual on "Writing R Extensions" but it's too difficult. Someone could help me? I want only save my personal function (recorded in R-code, not in C) in "myLibrary" and I want to call it with: >library (myLibrary). After this, the functions should be available directly in R. I know that it's possible to save functions in the workspace, but I need this functions in a library. # vbaRstat # [[alternative HTML version deleted]]
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:> I'd like to develop a simple library in R in which to save my > particular functions. > I have read the manual on "Writing R Extensions" but it's too > difficult. Someone could help me? > > I want only save my personal function (recorded in R-code, not in C) in > "myLibrary" and I want to call it with: > > >library (myLibrary). > > After this, the functions should be available directly in R. > > I know that it's possible to save functions in the workspace, but I > need this functions in a library. >-- Sarah Goslee http://www.stringpage.com http://www.functionaldiversity.org
2006/12/20, michele de meo <vbarstat at gmail.com>:> I'd like to develop a simple library in R in which to save my > particular functions.> I know that it's possible to save functions in the workspace, but I > need this functions in a library.- As an option you could also save the function in a textfile and source it in the beginning of an R session (-> e.g. in the profile.site file in RHOME\etc - Otherwise you will have to re-read about building packages. Packages are great (e.g. for consistent documentation, tests, ...) but have the disadvantage that they are more complicated and less flexible than just sourcing code. If your code changes (often) I'd recommend to automate the whole package-building-process. If you are on windows, maybe the following which I had laying around will help: << Download and Install -------------------- - Perl: http://activestate.com/store/activeperl/ - Rtools: http://www.murdoch-sutherland.com/Rtools/tools.zip (extract in a directory like e.g. programme\R\Rtools) - MS HTML Help Workshop: http://msdn.microsoft.com/library/en-us/htmlhelp/html/hwmicrosofthtmlhelpdownloads.asp - TeX: download current version at: http://www.miktex.org/ at the moment: http://prdownloads.sourceforge.net/miktex/basic-miktex-2.5.2471.exe?download Adjust Windows environmental variables (~System->Erweitert->Umgebungsvariablen) -------------------- - PATH: In this variable the paths to the installed programs above have to be. With the installation this has normally be done automatically. For the Rtools you have to do it yourself: add the path to the tools bin folder. Take care not to change the other paths. And check if the path for the programs above are correct/included. A GOOD IDEA is to copy the whole path in a text editor, modify and copy it back. - If I remember correctly the TeX program needs a nonstandard temp directory variable: "TMPDIR". Add this variable to the system variables, choose the same value as for the other temp variable (TEMP, TMP, ...). Optional tools ------------- - yap (to read *.dvi files, maybe this is already included in the miktex TeX download) Additonal info ------------- [1] http://statmath.wu-wien.ac.at/staff/dekic/Rwin/ [2] http://statmath.wu-wien.ac.at/staff/dekic/Rwin/LinkedDocuments/Anleitung.pdf [3] http://www.murdoch-sutherland.com/Rtools/>>Use package.skeleton to make a prototype (this is VERY useful). To build the packages you can use bat (batch) files, e.g. something like: set rver=2.3.1 set rcmd="C:\Programme\R\R-%rver%\bin\Rcmd.exe" %rcmd% check %prj% %rcmd% INSTALL --docs="txt,html,example,chm" --library=../../TempInstall %prj% -- Regards, Hans-Peter
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:> I'd like to develop a simple library in R in which to save my > particular functions. > I have read the manual on "Writing R Extensions" but it's too > difficult. Someone could help me? > > I want only save my personal function (recorded in R-code, not in C) in > "myLibrary" and I want to call it with: > > >library (myLibrary). > > After this, the functions should be available directly in R. > > I know that it's possible to save functions in the workspace, but I > need this functions in a library. >-- Sarah Goslee http://www.stringpage.com http://www.functionaldiversity.org ______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Thanks Barry, I tested this solution and it works. Thanks also to Sarah Goslee for bringing up alternative ideas. I guess I need to get into building proper packages now. AA. ----- Original Message ---- From: Barry Rowlingson <b.rowlingson at lancaster.ac.uk> Cc: r-help at stat.math.ethz.ch Sent: Wednesday, December 20, 2006 12:31:09 PM Subject: Re: [R] writing R extension Sarah Goslee wrote:> 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. >Yikes No! That will load all the objects into the current workspace. If you save when you quit, you'll end up with umpteen copies of your package code! For simple bundles of functions, it would be better to use save() to save them all to a .RData-type file and then 'attach()' it. This way it doesn't get stuck in your workspace. So: > foo=function(x){x^2} > bar=function(y){y^6} > baz=function(z){z*3} > myFunctions=c("foo","bar","baz") > save(list=myFunctions,file="myFunctions.RData") then quit R, start R in another workspace: > attach("/path/to/wherever/you/put/myFunctions.RData") > foo(2) [1] 4 Building proper _packages_ (never call them 'libraries' - libraries are collections of packages) isn't that hard once you've done it a dozen times, although I'm starting the find the bondage and discipline of packaging R code getting to me. Barry ______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Thanks Mike. AA. ----- Original Message ---- From: Mike Prager <mike.prager at noaa.gov> To: r-help at stat.math.ethz.ch Sent: Friday, December 22, 2006 12:45:40 PM Subject: Re: [R] writing R extension 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. ______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.