A function in R is invoked using fun(arg1, arg2, ...) but certain functions can be invoked using different syntax. When we write BOD[1, 2]
what we are really doing is invoking the [ function on the arguments BOD, 1 and 2 and this can be alternately written as a normal function invocation. Because [ uses a special character that is not normally allowed in object names we must surround it with backticks to tell R to regard it as a name. It can also be specified as a constant string. Thus these are all the same:
BOD[1, 2]
`[`(BOD, 1, 2) # same
"["(BOD, 1, 2) # same
'['(BOD, 1, 2) # same
examples
Here are other examples of this:
1 + 2
`+`(1, 2) # same
3 %in% 2:4
`%in%`(3, 2:4) # same
if (2 > 3) 4 else 5
`if`(2 > 3, 4, 5) # same
getAnywhere
We can find the code of a function using getAnywhere
like this:
getAnywhere(`[`)
but in this case it is just a primitive so we get:
A single object matching ‘[’ was found
It was found in the following places
package:base
namespace:base
with value
.Primitive("[")
Actually, in this case what [
does when the first argument is a data frame is to invoke [.data.frame
and that one has R source so we do this to see its source:
getAnywhere(`[.data.frame`)
In some cases getAnywhere
finds several occurrences of a name. In that case it will tell you where it found each and to get the ith use getAnywhere(...)[i]
where ... is the name you are looking for.