Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: What do you call the :: and ::: operators and how do they differ?

I'm wondering how the functions of the :: and ::: operators differ in R.

However, I can't figure out what these operators are called and so a google or SO search has not proven helpful. I also get an error when I try ?:: in R.

So...

  1. What are the :: and ::: operators called?

  2. How do :: and ::: differ? (i.e., what exactly does each do)?

like image 937
theforestecologist Avatar asked Jan 11 '17 03:01

theforestecologist


People also ask

What does the :: operator in R do?

If you know exactly which package contains the object desired then you can reference it directly using the :: operator. Simply place the package name before the operator and the name of the object after the operator to retrieve it. operator (notice the extra colon).

What is :: For in R?

The double-colon operator :: selects definitions from a particular namespace. In the example above, the transpose function will always be available as base::t , because it is defined in the base package. Only functions that are exported from the package can be retrieved in this way.

What are the differences between and <- in R?

The operator <- can be used anywhere, whereas the operator = is only allowed at the top level (e.g., in the complete expression typed at the command prompt) or as one of the subexpressions in a braced list of expressions.

What does %% mean in Rstudio?

%% gives Remainder. %/% gives Quotient. So 6 %% 4 = 2. In your example b %% a , it will vectorised over the values in “a” © Copyright 2013-2022 Analytics Vidhya.


1 Answers

It turns out there is a unique way to access help info for operators such as these colons: add quotations marks around the operator. [E.g., ?'::' or help(":::")].

  • Also, instead of quotation marks, back-ticks (i.e, ` ) also work.

Double Colon Operator and Triple Colon Operator

The answer to the question can be found on the help page for "Double Colon and Triple Colon Operators" (see here).

For a package pkg, pkg::name returns the value of the exported variable name in namespace pkg, whereas pkg:::name returns the value of the internal variable name. The package namespace will be loaded if it was not loaded before the call, but the package will not be attached to the search path.

The difference can be seen by examining the code of each:

> `::`
function (pkg, name) 
{
    pkg <- as.character(substitute(pkg))
    name <- as.character(substitute(name))
    getExportedValue(pkg, name)
}
<bytecode: 0x00000000136e2ae8>
<environment: namespace:base>

> `:::`
function (pkg, name) 
{
    pkg <- as.character(substitute(pkg))
    name <- as.character(substitute(name))
    get(name, envir = asNamespace(pkg), inherits = FALSE)
}
<bytecode: 0x0000000013482f50>
<environment: namespace:base>

:: calls getExportedValue(pkg, name), returning the value of the exported variable name in the package's namespace.

::: calls get(name, envir = asNamespace(pkg), inherits = FALSE), searching for the object name in the Namespace environment of the package, and returning the value of the internal variable name.


So, what exactly is a namespace?

This site does a good job of explaining the concept of namespaces in R. Importantly:

As the name suggests, namespaces provide “spaces” for “names”. They provide a context for looking up the value of an object associated with a name.

like image 106
theforestecologist Avatar answered Oct 22 '22 23:10

theforestecologist