3.6 Running functions without loading packages

Returning to the project at hand, I’d like to show you a slightly different way to generate the same graph:

In this code above, I use quantmod::getSymbols() instead of just getSymbols() and dygraphs::dygraph() instead of dygraph(). By adding packagename:: before a function, I’m able to access a function from an external package without having to load the package into memory first with library(packageName). This syntax PackageName::FunctionName works for any package that exists on your computer’s hard drive but isn’t loaded into memory. There can be a couple of advantages to this. First, if the package is large and you’re only using one function once, you can save system memory by not loading all the package’s functions into your session. (However, if you’re using several of a package’s functions multiple times throughout a script, it’s often easier to just load it into memory.)

Second, when you look at a code snippet months later, it’s clear which package each function belongs to.

Finally, if you’ve got multiple external packages loaded into memory, it’s possible that the author of package1 named a function the same thing as the author of package2. Using package1::myfun() lets R know you want the myfun() function in package1, and not any other function named myfun() from some other package. We’ll encounter that exact situation with two different functions called describe().