Ueli Hofstetter
Ueli Hofstetter

Reputation: 2524

What is the reason to add quotation marks around R function names?

What is the difference between defining a function called myfunction as

"myfunction" <- function(<arguments>){<body>}

and

myfunction <- function(<arguments>){<body>}

furthermore: what about the comments which are usually placed around such a function, i.e.

#myfunction{{{

 "myfunction" <- function(<arguments>){<body>}

#}}}

are they just for documentation or are they really necessary (if so for what)?

EDIT: I have been asked for an example where comments like

#myfunction{{{

are used: For example here https://github.com/cran/quantmod/blob/master/R/getSymbols.R

Upvotes: 6

Views: 1348

Answers (1)

BrodieG
BrodieG

Reputation: 52637

The quoted version allows otherwise illegal function names:

> "my function" <- function() NULL
> "my function"()
NULL

Note that most people use backticks to make it clear they are referring to a name rather than a character string. This allows you to do some really odd things as alluded to in ?assign:

> a <- 1:3
> "a[1]" <- 55
> a[1]
[1] 1
> "a[1]"
[1] "a[1]"
> `a[1]`
[1] 55

Upvotes: 8

Related Questions