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
Hi, 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 ?getwd) Similarly you can load the data, load("mydata.rda") 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. HTH, baptiste 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, EX4 4QL, UK http://newton.ex.ac.uk/research/emag
Hi, You might be interested in : - this (out of date) page : http://www.sciviews.org/_rgui/ - this (not yet filled) page : http://wiki.r-project.org/rwiki/doku.php?id=guis:projects for a list of potential guis/text editor you can use to write your R code. Romain On 08/21/2009 12:06 PM, Peter Meilstrup wrote:> > 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-- Romain Francois Professional R Enthusiast +33(0) 6 28 91 30 30 http://romainfrancois.blog.free.fr |- http://tr.im/w33B : Completion for java objects |- http://tr.im/vzip : Code Snippet : List of CRAN packages `- http://tr.im/vsK1 : R parser package on CRAN
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
Peter- I use emacs and ESS. Google r ess emacs and check out the first few hits. I use a split screen with the R file to edit on the left and get the R output on the right. Single line commands are executed with C-c C-n and selected regions are executed using C-c C-r as well as a bunch of other useful commands for R and, of course, all of the editing power and functionality of emacs. I am using linux, but it is said to work fine on Windows. There is an ESS help list too. HTH, Ron Peter Meilstrup wrote:> 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. > >-- R. R. Burns Physicist (Retired) Oceanside, CA