[R] Where to put source code?

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Fri Aug 21 13:10:19 CEST 2009


On Fri, Aug 21, 2009 at 11:06 AM, Peter
Meilstrup<peter.meilstrup at gmail.com> wrote:

> I'm just looking for the local equivalent of (in matlab) writing a
> function, saving it as functionName.m and then being able to call
> functionName(). Or in Python of writing a module.py and then typing
> "import module" at the prompt.
>
> Again this feels like an extraordinarily stupid question. But for some
> weird reason I'm coming up with nothing in the FAQ or in Rseek.

 It's a bit of a paradigm shift from the way Matlab and Python do it,
I'm afraid...

 In Matlab and Python, the system always goes to the file
representation of the function.

 In R, it doesn't. It goes to the R object in the in-memory storage
(aka "environment"). You update this by running 'source("file.R")'
which executes 'foo = function(...){etc}', thus overwriting the old
function definition.

  At the end of an R session, if you save the image (in a .RData
file), then when you restart R with that saved image your function
definitions will be reloaded too - but from the .RData and not the .R
text file.

 I use Emacs with ESS, and edit my functions in .R files. I then hit
Ctrl-C Ctrl-L and they are "sourced" into R. I find that's the easiest
way to develop simple functions. The other R environments can do
similar things.

Barry




More information about the R-help mailing list