rrgui

R console: single line instructions vs multiple lines instructions


I noticed that, in some cases (I am still a beginner at R!), multi lines instructions cannot be merged 'as is' into simple lines instructions. Let's take an example, which is in a lesson I recently had on line:

> make.power <- function(n) {
+     pow <- function(x) {
+         x^n
+         }
+         pow
+     }

Is the definition of the 'make.power' function. I can then define the 'cube' or the square functions

> cube <- make.power(3)
> square <- make.power(2)

Then, if I try to define make.power on a single line with exactly the very same syntax:

> make.power <- function(n) { pow <- function(x) { x^n } pow }

This will raise an "Unexpected symbol in "make.power ...". This said, the following single line code will work:

> make.power <- function(n) { pow <- function(x) { x^n }}

and, when switched to multi-line, will still work!

> make.power <- function(n) {
+     pow <- function(x) {
+         x^n
+         }
+     }

I must admit that I am quite confused. My questions are the following:

  1. Why can't I just merge the multi-line code in a single line instruction?
  2. In the first multi-line definition of the function, why should one recall the 'pow' function right after the function is defined, which is obviously of no interest, as the same code without the recalling is ok!

Edit: following #Dwin answer, it is true that the following expression:

> make.power <- function(n) { pow <- function(x) { x^n }; pow }

works perfectly. And so does this one:

> make.power <- function(n){; pow <- function(x) {; x^n }; pow }

So it seems that the ";" could be mandatory in some cases (here, between the "}" and second 'pow', and not in other cases (between the '{' and first 'pow')?


Solution

  • The carriage return (or linefeed) separates commands. (I earlier said it separates 'expressions' but in R that is not really the right terminology. "Expressions" in the R-sense are actually separated by commas.) If you insert a semi-colon within a line, you get the same effect. Despite the fact that there is unnecessary, code this still works as an example of that fact:

    make.power <- function(n) { pow <- function(x) { x^n }; pow }
    

    I was rather disappointed that I could not find this stated as such in any help() files I looked at. I thought that by typing ?Syntax I should have found something relevant. The best I could come up with was the help(parse) page, and even there, only by looking at the example would you have been able to deduce this. I guess you were expected to have read "Intro to R" where I'm reasonably sure it must be described. Yep, there it is in section 1.8.

    In response to your Q2: During the process of assignment with "<-" of the inner function(...) to "pow", the value returned to the outer function call was just the unnamed function rather than anything that had the name "pow". Which was the point of my comment to your first comment. That's probably a more subtle point than you would have been expected to have gotten from "Intro to R". The return function is sometimes needed when an assignment of only a portion of an object was the last command, but in this case the most compact code would have been:

    make.power <- function(n) { function(x) { x^n } }