The Art of R Programming:Where Else Could I Find the Information

The Art of R Programming : Where else could I find the information?

Functions don't have names in R. Whether you happen to put a function into a variable or not is not a property of the function itself so there does not exist two sorts of functions: anonymous and named. The best we can do is to agree to call a function which has never been assigned to a variable anonymous.

A function f can be regarded as a triple consisting of its formal arguments, its body and its environment accessible individually via formals(f), body(f) and environment(f). The name is not any part of that triple. See the function objects part of the language definition manual.

Note that if we want a function to call itself then we can use Recall to avoid knowing whether or not the function was assigned to a variable. The alternative is that the function body must know that the function has been assigned to a particular variable and what the name of that variable is. That is, if the function is assigned to variable f, say, then the body can refer to f in order to call itself. Recall is limited to self-calling functions. If we have two functions which mutually call each other then a counterpart to Recall does not exist -- each function must name the other which means that each function must have been assigned to a variable and each function body must know the variable name that the other function was assigned to.

How to learn R as a programming language

For starters, you might want to look at this article by John Cook. Also make sure that you read "The R Inferno".

There are many good resources on the R homepage, but in particular, read "An Introduction to R" and "The R Language Definition".

Some very closely related stackoverflow questions:

  • books-for-learning-the-r-language.
  • what-are-some-good-books-web-resources-and-projects-for-learning-r
  • suggestions-on-way-resources-to-start-learning-statistical-language-r

My favorite book on the subject: "Software for Data Analysis: Programming with R", by John Chambers, the creator of the S language.

Can an R function access its own name?

You sure can.

fun <- function(x, y, z) deparse(match.call()[[1]])
fun(1,2,3)
# [1] "fun"

Is `for` a function in R?

for is a function, but the symbol for is also recognised by the parser as part of the convenient syntax that we can use to call the function for. These are two different things conveniently named the same (note that in is not a function).

is.function(`for`)
#> [1] TRUE

x <- y <- z <- 1:3
for (i in 1:length(x)) z[i] <- x[i] + y[i]
z
#> [1] 2 4 6

x <- y <- z <- 1:3
`for`(i, 1:length(x), z[i] <- x[i] + y[i])
z
#> [1] 2 4 6

Created on 2019-05-19 by the reprex package (v0.2.1)

In a similar fashion the if (cond) foo else bar syntax maps to the function call `if`(cond, foo, bar), but there is no else function.

All other control flow constructs (see ?Control) are also functions.



Related Topics



Leave a reply



Submit