Why does the R language require “::” before function names?

R/Rstudio

When analyzing in the R language, two colons may be written before the function to be executed, as shown below.

 dplyr::select(Valuable_1,Valuable_2…Valuable_n, data=dat)

This means to use the select function in the dplyr package, but you know, it should be possible to execute the select function without the “::”.

So, until recently, I was not quite sure why it was necessary to add the “::”, but I recently figured out the reason and share it with you.

Two colons (::) are used to specify which package the function is for

The reason for the two colons (::) is to clearly indicate which package the function is for.
Some of the packages you install may have different content objects with the same name.

For example, the object “alpha” is a function that represents the transparency of the color of the graph in the ggplot2 package, which can create beautiful graphs.
(You can adjust the transparency of the graph color by writing something like alpha =0.5.)

On the other hand, in the psych package, which is well known in the field of psychology, the object “alpha” is assigned a function to check the alpha coefficient, which is the confidence coefficient of internal consistency.

Thus, if you are calling multiple packages that have different objects with the same name, you need to make it clear which package the object is from.

summary

If a script that worked fine the first time but produces errors when run later, suspect that the object may have been masked by the calling package.

タイトルとURLをコピーしました