The Art of R Programming

Norman Matloff

Mentioned 2

A guide to software development using the R programming language covers such topics as closures, recursion, anonymous functions, and debugging techniques.

More on

Mentioned in questions and answers.

I've been using R for a little over a year now and it's been a successful venture. But all to often, I find that there is something that I can't figure out for lack of knowing how to find it or an example of it.


Could you recommend a pathway for learning R in a manner that provides one with a toolset at their disposal to solve problems of a statistical nature?

There's a wealth of knowledge on the internet, between the r-project website and the mailings lists but it seems to be "everywhere" and nowhere when you're actually looking for it.

For example, when I first started using R, I went through "Intro to R". Then I read the language definition (which obviously hasn't sunk in). But every time I ask a question on Stackoverflow I'm presented with some new badass function that is the solution to all my problems in the short term. My question is, how did you know these functions existed in the first place? And how does one go about finding them? Presumably, you read something or found some resources that detoured your learning to the exponential part of the curve. What was it?

Obviously, R's functionality as a statistical tool is broad. For my own purposes I work mostly with economic or financial data. Hence, answers with this in mind would be most helpful.

The book that helped my learning the most was The Art of R Programming. A lot of programming books can be dry. Since R is commonly an entry point to programming it's important for the voice of materials to resonante with the student. That book did just that with me. The voice felt very casual and I liked that.

I have created a function (which is quite long) that I have saved in a .txt file. It works well (I use source(< >) to access it). My problem is that I have created a few variables in that function ie:

myfun<-function(a,b) {
Var2=Var1 + ..

Now I want to get those variables. When I include return() inside the function, its fine: the value comes up on the screen, but when I type Var1 outside the function, I have an error message "the object cannot be found". I am new to R, but I was thinking it might be because "myfun" operates in a different envrionment than the global one, but when I did

environment: R_GlobalEnv>
environment: R_GlobalEnv>

It seems to me the problem is elsewhere...

Any idea?


If you want to do it in a nice way, write a class and than provide a print method. Within this class it is possible to return variables invisible. A nice book which covers such topics is "The Art of R programming".

An easy fix would be save each variable you need later on an list and than return a list (as Peter pointed out):

return(list(VAR1=VAR1, .....))