[R] using self-written functions

Prof Brian Ripley ripley at stats.ox.ac.uk
Fri Jun 29 09:38:55 CEST 2007


One of your home directories on Windows is %HOMEDRIVE%%HOMEPATH% (you do 
need to give the drive), but only one: see the rw-FAQ.

Reading ?file.path will help you not reinvent the wheel.

So I think attach(path.expand("~/MMmisc.rda") is far more portable (as 
well as much more readable).

On Thu, 28 Jun 2007, Mike Meredith wrote:

> I use something like Gavin's solution for functions which are not used too
> often. The problem with using 'source' is that the user environment gets
> cluttered.
>
> I 'save' the most useful functions to a single file ("MMmisc.Rda"), put it
> in the HOMEPATH directory (use Sys.getenv("HOMEPATH") to find where that
> is), then I put the line
>
> attach(what=paste(Sys.getenv("HOMEPATH"), "MMmisc.Rda", sep="/"))
>
> in Rprofile.site in C:\Program Files\R\R-2.5.0\etc folder.

Please don't recommend site-wide setting for user settings, which should 
be in ~/.Rprofile.  See ?Startup.

>
> Works for me! You don't have the luxury of help files, etc, but it's easy to
> update.
>
> HTH -- Mike.
>
>
> Gavin Simpson wrote:
>>
>> On Thu, 2007-06-28 at 17:29 +0800, R. Leenders wrote:
>>> ... How can
>>> I access my own functions in R without having to copy-paste them
>>> everytime and run them manually so I can call them later? Do I need to
>>> learn how to write a package and attach the package to make the
>>> functions available at all times? Is there another way?
>>
>> Building a package is one way, and not that difficult once you've read
>> the Writing R Extensions manual.
>>
>> An alternative is to have a directory where you keep R function scripts.
>> Put your functions in here in text files with say a .R extension. Then
>> in R you can source one or more of these R scripts as required, using
>> the source() function.
>>
>> Say you have a directory, myScripts at the base of file system
>> (/home/user say on Linux or C:\ on Windows). in this directory there is
>> a file called my_r_function.R. To use this script/function in an R
>> session, you would issue:
>>
>> ## replace /home/user/ with what ever is the correct path for your
>> ## system
>> source("/home/user/myScripts/my_r_function.R")
>>
>> Which would make available to your current session any functions defined
>> in my_r_function.R.
>>
>> Read ?source for more information.
>>
>> HTH
>>
>> G
>>
>>>
>>> thanks, James
>>>
>>>
>>>
>>>
>>>
>>> ____________________________________________________________________________________
>>>
>>>
>>> 	[[alternative HTML version deleted]]
>>>
>>> ______________________________________________
>>> 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.
>> --
>> %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
>>  Gavin Simpson                 [t] +44 (0)20 7679 0522
>>  ECRC, UCL Geography,          [f] +44 (0)20 7679 0565
>>  Pearson Building,             [e] gavin.simpsonATNOSPAMucl.ac.uk
>>  Gower Street, London          [w] http://www.ucl.ac.uk/~ucfagls/
>>  UK. WC1E 6BT.                 [w] http://www.freshwaters.org.uk
>> %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
>>
>> ______________________________________________
>> 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.
>>
>>
>
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-help mailing list