r double and triple colons
The double colons (::
) and triple colons (:::
) are used in R for specifying the package or namespace from which a function or object should be called.
Double Colon (::
)
The double colon operator is used to access functions or objects from a specific package. It allows you to call a function or access an object without having to load the entire package using the library()
function.
The syntax for using the double colon operator is package::function()
or package::object
. Here, package
refers to the name of the package, function
refers to the name of the function, and object
refers to the name of the object.
For example, if you want to use the mean()
function from the stats
package, you can use the double colon operator as follows:
stats::mean(x)
This syntax explicitly tells R to use the mean()
function from the stats
package, even if there are other functions with the same name in other loaded packages.
Triple Colon (:::
)
The triple colon operator is used to access non-exported functions or objects from a specific package. Non-exported functions or objects are those that are not intended to be used directly by users. They are usually helper functions or internal objects.
The syntax for using the triple colon operator is package:::function()
or package:::object
.
For example, if you want to use a non-exported function named helper()
from the mypackage
package, you can use the triple colon operator as follows:
mypackage:::helper()
It is generally advised to avoid using the triple colon operator unless absolutely necessary, as non-exported functions or objects may change or be removed in future versions of the package.
Summary
In summary, the double colon (::
) and triple colon (:::
) operators in R are used to specify the package or namespace from which a function or object should be called. The double colon operator is used for accessing functions or objects from a specific package, while the triple colon operator is used for accessing non-exported functions or objects from a specific package.