R-help, Sometimes I define functions I wish to have in any R session. The obvious thing to do is copy-paste the code The thing is that sometimes I don't know where I have the function code. My question is if somehow I could define a function and "introduce" it (let's say 'base' package ) so that could be used anytime I run a different R project. Thank you in advance
I think the usual way is to create an R package for yourself and load it when you need it for whatever project. -roger Luis Ridao Cruz wrote:> R-help, > > Sometimes I define functions I wish to have in any R session. > The obvious thing to do is copy-paste the code > The thing is that sometimes I don't know where I have the function > code. > > My question is if somehow I could define a function and "introduce" it > (let's say 'base' package ) so that > could be used anytime I run a different R project. > > Thank you in advance > > ______________________________________________ > 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 >
See at http://cran.r-project.org/doc/manuals/R-intro.pdf at page 54. .First() can help you. Or create an own package (see http://cran.r-project.org/doc/manuals/exts.pdf ) and load the package, when needed. Best, Matthias> -----Urspr?ngliche Nachricht----- > Von: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] Im Auftrag von Luis > Ridao Cruz > Gesendet: Mittwoch, 06. April 2005 15:48 > An: r-help at stat.math.ethz.ch > Betreff: [R] Introduce a new function in a package? > > > R-help, > > Sometimes I define functions I wish to have in any R session. > The obvious thing to do is copy-paste the code > The thing is that sometimes I don't know where I have the > function code. > > My question is if somehow I could define a function and > "introduce" it (let's say 'base' package ) so that > could be used anytime I run a different R project. > > Thank you in advance > > ______________________________________________ > 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 >
Another approach, if making a package is a bit more than what you want to do, is: 1. Save those functions/objects in an image using save(). 2. attach() that image every time you start R. There are a few ways that you can do #2 above. See ?Startup. Andy> From: Roger D. Peng > > I think the usual way is to create an R package for yourself and load > it when you need it for whatever project. > > -roger > > Luis Ridao Cruz wrote: > > R-help, > > > > Sometimes I define functions I wish to have in any R session. > > The obvious thing to do is copy-paste the code > > The thing is that sometimes I don't know where I have the function > > code. > > > > My question is if somehow I could define a function and > "introduce" it > > (let's say 'base' package ) so that > > could be used anytime I run a different R project. > > > > Thank you in advance > > > > ______________________________________________ > > 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 > > > > > ______________________________________________ > 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 > > >
or you could create a package containing all these functions and edit .Rprofile to load it at start-up (see also ?.Startup). Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/16/336899 Fax: +32/16/337015 Web: http://www.med.kuleuven.ac.be/biostat/ http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm ----- Original Message ----- From: "Luis Ridao Cruz" <Luisr at frs.fo> To: <r-help at stat.math.ethz.ch> Sent: Wednesday, April 06, 2005 3:47 PM Subject: [R] Introduce a new function in a package?> R-help, > > Sometimes I define functions I wish to have in any R session. > The obvious thing to do is copy-paste the code > The thing is that sometimes I don't know where I have the function > code. > > My question is if somehow I could define a function and "introduce" > it > (let's say 'base' package ) so that > could be used anytime I run a different R project. > > Thank you in advance > > ______________________________________________ > 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 >
Some other advantages of making your own package are: - you can use help.search to search for your own functions even if you don't load the package - if you can't even remember where your functions are (and I often can't) then you may not remember what they do either and packaging them gives a convenient way to associate documentation. Once you have found your function you can use ? to gets its documentation. - you get to use ' CMD check' whch is very helpful If you are doing it on Windows the amount of software you need to download and install first may be a bit offputting and you may need to sort out some path and latex problems but its probably worth it in the end if you do enough R development. On Apr 6, 2005 10:55 AM, Don MacQueen <macq at llnl.gov> wrote:> Expressions in .Rprofile are executed *before* any previously saved > global environment is loaded (i.e., before the .RData file in the > current working directory is loaded, causing the message " > [Previously saved workspace restored]" to a appear). > > If you define a function in .Rprofile, and then later answer "yes" to > the "Save workspace image?" question when you quit R, the function > will exist in the saved workspace. > > When you next start R, the version that comes in from .Rprofile will > be replaced by the version in the saved workspace -- because the > saved workspace is loaded after .Rprofile is executed. > > This means that if you decide to change the function in .Rprofile, > your changes will immediately be lost when the previously saved > workspace is loaded, since that has the previous version. > > So defining personal utility functions in .Rprofile is not very > effective. Much, much, better to create a package, and then require() > that package in .Rprofile. And since creating a package is really > very easy, I strongly recommend that option. > > Saving the functions in an image file and then attaching it is fine, > but less convenient, in my opinion, since you have to keep track of > where it is in the file system. > > -Don > > At 4:09 PM +0100 4/6/05, Jan T. Kim wrote: > >On Wed, Apr 06, 2005 at 09:57:00AM -0400, Roger D. Peng wrote: > >> I think the usual way is to create an R package for yourself and load > >> it when you need it for whatever project. > >> > >> -roger > > > >Alternatively, one can also write the function in question into one's > >~/.Rprofile; then, it's automatically available in all R sessions. > >To avoid confusion, make sure that you choose a unique name, i.e. one > >that isn't used by any package, if possible. > > > >This method should be used only for functions intended to provide some > >convenience in interactive sessions, code in scripts should not rely > >on functions being provided by ~/.Rprofile. For scripting, an R package > >is definitely preferred. > > > >Best regards, Jan > > > >> Luis Ridao Cruz wrote: > >> >R-help, > >> > > >> >Sometimes I define functions I wish to have in any R session. > >> >The obvious thing to do is copy-paste the code > >> >The thing is that sometimes I don't know where I have the function > >> >code. > >> > > >> >My question is if somehow I could define a function and "introduce" it > >> >(let's say 'base' package ) so that > >> >could be used anytime I run a different R project. > >> > > >> >Thank you in advance > >> > > >> >______________________________________________ > >> >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 > >> > > >> > >> ______________________________________________ > >> 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 > > > >-- > > +- Jan T. Kim -------------------------------------------------------+ > > | *NEW* email: jtk at cmp.uea.ac.uk | > > | *NEW* WWW: http://www.cmp.uea.ac.uk/people/jtk | > > *-----=< hierarchical systems are for files, not for humans >=-----* > > > >______________________________________________ > >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 > > -- > -------------------------------------- > Don MacQueen > Environmental Protection Department > Lawrence Livermore National Laboratory > Livermore, CA, USA > > ______________________________________________ > 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 >
> ~/.RprofileYou could also write in .Rprofile soemthing like this: for (x in dir("Mylibrary",full.names=T,recursive=T)) source(x) where "Mylibrary" is a directory which contains your functions without making a package. Josef Eschgf?ller