Get startedGet started for free

Packages and Namespaces

1. Packages and Namespaces

R packages are wonderful things. When we use any R package, we get a number of benefits for free.

2. Your global environment

For example, when interacting with R, we live in the Global Environment. Any objects we create are placed in this box. We can view the contents of the box with the ls() function. However, any mildly complex analysis quickly fills up this box. The consequence of this is that we lose track of what's inside the package. Even worse, we might mix up two objects and start introducing hard to find bugs.

3. Packages and environments

When we load a package, the contents of the package isn't placed into the Global environment, instead it's loaded into its namespace. I like thinking of namespaces as box that contains the package functions. The namespace keeps things tidy, just like folders.

4. Exported functions

When we use the library() function, we get direct access to this package box. We can see the contents of this box more precisely, the exported functions, via the function getNamespaceExports(). The dplyr library has over 200 exported functions. Sometimes we want to specify exactly which box a function belongs to. In this case we can use the double colon operator to grab the function directly from a package.

5. Great minds think alike

Great minds think alike, or so the saying goes. Well package authors are just the same. Take the filter() function. In the stats package, filter is a function for manipulating time series data. But in dplyr, it is used to remove rows from a data frame. So the million pound question is, if I type filter(), which version do I get? Unfortunately, the answer is "It depends". It depends on the order the packages were loaded in.

6. Great minds think alike

The search() function returns this order. R goes through each environment looking for objects. It returns the object it comes to that matches the request. The Global environment is always first in this list, then the order is determined by the package load order.

7. Be defensive

Namespace clashes, that is, when two functions have the same name, are a pain and can lead to hard to diagnose bugs.

8. Conflicted

A relatively new package, aims to make this process a little easier. The conflicted package automatically highlights potential package clashes, and forces the user to make a clear choice. For example suppose we load the dplyr package. When we come to use the filter function, the conflicted package highlights the issue and makes us explicitly choose.

9. Time to wake up

Right, time to wake up and put what we've discussed into practice!

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.