1

What is the purpose of using :: in R? I noticed in some of the guides they use :: to indicate what package the function is from. Does this make things run faster or is it merely to indicate what packages are necessary?

Also, are there any other purposes/uses for ::?

Snippet from ggplot2 package introduction:

grid::polygonGrob(
      coords$x, coords$y, 
      default.units = "native",
      gp = grid::gpar(
        col = first_row$colour,
        fill = scales::alpha(first_row$fill, first_row$alpha),
        lwd = first_row$size * .pt,
        lty = first_row$linetype
      )
cgage1
  • 579
  • 5
  • 15
  • 2
    If two packages have functions with the same name, then using `pkg::fn` instead of `library(pkg); fn` is safer (since it avoids confusion and name conflicts). Here's an example: http://stackoverflow.com/q/3241539 – Frank Apr 25 '16 at 23:23
  • I didn't think the question was the same nor did any of the answers actually answer this question directly. – IRTFM Apr 25 '16 at 23:34

1 Answers1

5

At any given time there is a search path of NAMESPACEs established on the basis of the order of loading packages. Sometimes loading a package will "mask" a function with the same name as had already existed in the workspace. This is usually announced at the time of the new package loading, but users may overlook this concern. Using "::" ensures that a particular function will be accessed from the desired package with the correct environment. Attempting to reload a function with a second library call would fail because the library() function checks to see if the library is already loaded and simply does nothing if it is in the search path.

IRTFM
  • 258,963
  • 21
  • 364
  • 487