0

I have recently encountered the following R syntax:

(`::`("ggplot","aes"))()

or

(`$`(mylist,"column"))

I understand what it does, but I struggle to find any documentation on it, as it is quite difficult to search for special characters. Can anyone tell me what this Syntax is called and where I can learn more about it?

Edit: I am NOT asking about the meaning of the operators but about the SYNTAX of putting them in backticks infront of parentheses. I hope that clarifies what I mean.

Using the ? operator in R could not provide me with any useful information. But maybe I did not use it correctly.

Phil
  • 7,287
  • 3
  • 36
  • 66
Pycruncher
  • 25
  • 2
  • 1
    You'll find some doc under `?\`::\`` (double colon operator). The parenthesis syntax is usually referred to as an anonymous function (or lambda function) – Maël Feb 21 '23 at 10:13
  • https://cran.r-project.org/doc/manuals/r-release/R-lang.html#Operators – Roland Feb 21 '23 at 10:18
  • @Maël Thank you, I was looking for anonymous/lambda functions! – Pycruncher Feb 21 '23 at 10:24
  • 1
    It works because the parser translates operators into function calls. – Roland Feb 21 '23 at 10:24
  • 1
    Backticks are used to call non-syntactic names (see `?Quotes`, or https://stackoverflow.com/questions/36220823/what-do-backticks-do-in-r). The parentheses are used to use the function: check what `\`::\`("ggplot2","aes")` does. – Maël Feb 21 '23 at 10:24
  • 6
    Sounds like you're looking for some info on rewriting functions into prefix form. See https://adv-r.hadley.nz/functions.html#prefix-transform – Ritchie Sacramento Feb 21 '23 at 10:33
  • 2
    @RitchieSacramento, post as an answer? – Ben Bolker Feb 21 '23 at 15:13

1 Answers1

-3

As answered by @Maël, it is referred to as a lambda or anonymous function.

Thanks to the comments of other users, it seems the answer provided my @Maël is not correct. Instead, as suggested by @RitchieSacramento, https://adv-r.hadley.nz/functions.html#prefix-transform says:

An interesting property of R is that every infix, replacement, or special form can be rewritten in prefix form. Doing so is useful because it helps you better understand the structure of the language, it gives you the real name of every function, and it allows you to modify those functions for fun and profit.

The following example shows three pairs of equivalent calls, rewriting an infix form, replacement form, and a special form into prefix form.

x + y
`+`(x, y)

names(df) <- c("x", "y", "z")
`names<-`(df, c("x", "y", "z"))

for(i in 1:10) print(i)
`for`(i, 1:10, print(i))

This provides exactly the explanation that I was looking for and I would advise anyone to checkout the referenced chapter for more info.

Pycruncher
  • 25
  • 2