[R] Where to put source code?

baptiste auguie baptiste.auguie at googlemail.com
Fri Aug 21 12:39:07 CEST 2009


Say you have the following data and functions that you want to reuse,

d = data.frame(1:10)

foo = function(x,y , ...){
	plot(x,y, type="l", ...)

You may save the code in a file "testing.r", noting that in general
data may find a convenient storage format using save(d, file=
"mydata.rda"). When opening a new R session you can load this code
with ?source,

source("/your/path/to/this/file/testing.r") # or simply
source("testing.r")  if you're in the same working directory (see

Similarly you can load the data,


The code and data are then loaded and available for you to use.

If you find after some time that you have accumulated quite a lot of
functions and data that clutter your workspace, writing a package is a
neat and easy option to organize them, see ?package.skeleton to get
you started.



2009/8/21 Peter Meilstrup <peter.meilstrup at gmail.com>:
> I'm trying to move from Matlab to R, and I'm stuck even getting
> started. This sounds to me like the dumbest question in the world
> but... how does one put R source code in files? Over the last three
> days I've gone front to back through the Introduction to R and the R
> Language Definition, and while I'm excited that the language is so
> Lispish, none of what I read described how to write a function, save
> the source code in a file, and have it be usable other than directly
> entering the function definition at the command prompt.
> How do you edit your functions, where do you put the source files that
> contain your functions, how do you tell R to use function definitions
> from a particular file? How do you make incremental changes to your
> functions while debugging?
> I have not gone into the documentation about writing "packages" and
> "extensions" other than to glance at them and decide that can't
> POSSIBLY be the answer I'm looking for.
> 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.
> Peter
> ______________________________________________
> R-help at r-project.org 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.


Baptiste Auguié

School of Physics
University of Exeter
Stocker Road,
Exeter, Devon,


More information about the R-help mailing list