The Search Path: How R looks for stuff

The good thing about sharing your knowledge with people is that you always find yourself strangely inspired. That’s what happened to me the other day in a meeting with colleagues where we were looking at some issues we were having with a reporting format we created in R Markdown. I was trying to explain to them, in simple terms, the little I knew about how R’s search path works and how it accesses objects along that path. I typed search() and got this all too familiar output

[1] ".GlobalEnv"        "package:stats"
[3] "package:graphics"  "package:grDevices"
[5] "package:utils"     "package:datasets"
[7] "package:methods"   "Autoloads"
[9] "package:base"
>

Because of the size of my console window that day, I suddenly had an analogy for them.

I asked them to imagine that they were sitting at a desk with two drawers and suddenly started looking for a particular document. What would they do?

A desk with drawers and a chair
© mattbuck 2012

The first impulse would be to start from the tabletop. This represents .GlovalEnv, otherwise known as the Workspace. If not found, one would look through the drawers on one side, one after the other – beginning from the top – and if still not found, one moves over to the other side, also starting from the top drawer.

This is exactly how R uses its search path. The last visible ‘drawer’ that it checks is the base environment, which is accessible with baseenv().

Advertisements

Author: Victor Ordu

Learning new things and growing on a daily basis - that's what life's all about!

2 thoughts on “The Search Path: How R looks for stuff”

Comments

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s