<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
 
 <title>O Beautiful Code</title>
 <link href="http://obeautifulcode.com/atom.xml" rel="self"/>
 <link href="http://obeautifulcode.com"/>
 <updated>2013-03-21T11:46:59-04:00</updated>
 <id>http://obeautifulcode.com/</id>
 <author>
   <name>Suraj Gupta</name>
 </author>
 <rights>&#169; 2012 Suraj Gupta</rights>
 
 <entry>
   <title>RserveCLI2, a .net client for Rserve</title>
   <link href="http://obeautifulcode.com/R/RserveCLI2-A-Dot-Net-Client-For-Rserve"/>
   <updated>2013-03-21T00:00:00-04:00</updated>
   <id>http://obeautifulcode.com/R/RserveCLI2-A-Dot-Net-Client-For-Rserve</id>
   <content type="html">&lt;p&gt;&lt;a href=&quot;https://rservecli.codeplex.com/&quot;&gt;RserveCLI&lt;/a&gt; is a .net/cli client for Rserve, created by Oliver M. Haynold.  Oliver has done a great job with this project.&lt;/p&gt;

&lt;p&gt;I forked this project to add features, fix bugs, and do some restructuring.  I thought it was a significant enough depature to create a new project.&lt;/p&gt;

&lt;p&gt;To that end, I&amp;rsquo;m hosting &lt;strong&gt;&lt;a href=&quot;https://github.com/SurajGupta/RserveCLI2&quot;&gt;RserveCLI2&lt;/a&gt;&lt;/strong&gt; on github.&lt;/p&gt;

&lt;p&gt;Contributions appreciated!&lt;/p&gt;

</content>
   <rights>&#169; 2012 Suraj Gupta</rights>
 </entry>
 
 <entry>
   <title>Nixed Amazon Linux</title>
   <link href="http://obeautifulcode.com/Cloud/Nixed-Amazon-Linux"/>
   <updated>2013-02-28T00:00:00-05:00</updated>
   <id>http://obeautifulcode.com/Cloud/Nixed-Amazon-Linux</id>
   <content type="html">&lt;p&gt;I&amp;rsquo;m using EC2,  I just can&amp;rsquo;t use Amazon Linux.  Let me explain…&lt;/p&gt;

&lt;h2 id=&quot;solve-for-devprod-parity&quot;&gt;Solve for Dev/Prod Parity&lt;/h2&gt;
&lt;p&gt;My company is committed to the principal of &lt;a href=&quot;http://www.12factor.net/dev-prod-parity&quot;&gt;dev/prod parity&lt;/a&gt; to minimize deployment failures.  That means our development environment is as close as possible to our production environment.  Unfortunately, Amazon Linux is not super accessible.  It&amp;rsquo;s not published such that it can be installed on desktops, laptops, or local virtualization environments like &lt;a href=&quot;https://www.virtualbox.org/&quot;&gt;VirtualBox&lt;/a&gt;.  It seems there are ways to hack it, but it isn&amp;rsquo;t an out-of-the-box sort of thing.&lt;/p&gt;

&lt;p&gt;So, there are two options for selecting an OS to run in the cloud:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Use Amazon Linux.  Develop, test and deploy in EC2 (always-on instance).&lt;/li&gt;
  &lt;li&gt;Use a more accessible distro (e.g. Ubuntu which has local and &lt;a href=&quot;http://cloud-images.ubuntu.com/releases/precise/release/&quot;&gt;cloud images&lt;/a&gt;).  Develop and test locally and deploy in EC2.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;#1 is costly where micro or small instances can&amp;rsquo;t do the job.  Also, I&amp;rsquo;m uncomfortable being perpetually on-the-clock (until the day cloud computing becomes a dirt-cheap commodity).&lt;/p&gt;

&lt;p&gt;So that leaves #2!&lt;/p&gt;

&lt;h2 id=&quot;not-a-great-pitch&quot;&gt;Not a Great Pitch&lt;/h2&gt;

&lt;p&gt;Amazon advertises some &lt;a href=&quot;https://aws.amazon.com/amazon-linux-ami/&quot;&gt;key features&lt;/a&gt; of Amazon Linux.  I see little value-add:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;em&gt;AWS Integration&lt;/em&gt; -  It&amp;rsquo;s plenty easy to install the AWS API tools on other distros.  Also, CloudInit supports a particular configuration management strategy, which is not the one we use.  Also, CloudInit fails dev/prod parity =)&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;Secure Configuration&lt;/em&gt; - The advertised security features (i.e. disable root, enable SSH key pairs) are basic.  Those should be implemented always.  I can reduce non-critical packages to limit the threat surface just as well as Amazon can.  Also, it&amp;rsquo;s straight-forward to automatically apply security updates.&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;Package repository access&lt;/em&gt; - ok, Amazon Linux wins.  It will be blazing fast to hit their package repo.  I don&amp;rsquo;t understand their point about data transfer charges.  The last I checked there is no fee for data transferred &lt;em&gt;into&lt;/em&gt; EC2 from the internet.  So it should be mostly free to pull packages from non-Amazon repositories.&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;Lightweight&lt;/em&gt; - Here they just repeat their point about security and non-critical packages.&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;Available in All Regions, Formats, and Architectures&lt;/em&gt; - It seems the Ubuntu &lt;a href=&quot;http://cloud-images.ubuntu.com/releases/precise/release/&quot;&gt;cloud images&lt;/a&gt; are just as broadly available.&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;AWS Support&lt;/em&gt; - Clearly they will be better at supporting their OS on their own platform.  The value is a bit hard to quantify, though.  And of course you have to pay extra for support.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;ideally&quot;&gt;Ideally&amp;hellip;&lt;/h2&gt;

&lt;p&gt;The theoretically best solution to dev/prod parity is to use a cloud that you can install locally such as  &lt;a href=&quot;http://www.openstack.org/&quot;&gt;OpenStack&lt;/a&gt;.  I think this platform lacks the maturity of AWS.&lt;/p&gt;

</content>
   <rights>&#169; 2012 Suraj Gupta</rights>
 </entry>
 
 <entry>
   <title>Simple Software, a Tutorial</title>
   <link href="http://obeautifulcode.com/Craftsmanship/Simple-Software-A-Tutorial"/>
   <updated>2013-02-25T00:00:00-05:00</updated>
   <id>http://obeautifulcode.com/Craftsmanship/Simple-Software-A-Tutorial</id>
   <content type="html">&lt;div class=&quot;floatImageLeft&quot;&gt;
    &lt;img src=&quot;/img/simple-software-a-tutorial/rich-hickey.png&quot; alt=&quot;Rabbit Hole&quot; /&gt;
&lt;/div&gt;

&lt;p&gt;I&amp;rsquo;m a compulsive note-taker (using OneNote, which I think tops all other products in the category).  I like to condense information, in &lt;a href=&quot;https://en.wikipedia.org/wiki/Plain_English&quot;&gt;plain english&lt;/a&gt;, and codify insights so I can refer to them later when I need a refresher.&lt;/p&gt;

&lt;p&gt;Here&amp;rsquo;s my notes from &lt;a href=&quot;https://twitter.com/richhickey&quot;&gt;Rich Hickey&lt;/a&gt;&amp;rsquo;s 2011 talk &amp;ldquo;&lt;a href=&quot;http://www.infoq.com/presentations/Simple-Made-Easy&quot;&gt;Simple Made Easy&lt;/a&gt;&amp;rdquo; (augmented with bits from his 2012 &lt;a href=&quot;https://www.youtube.com/watch?v=rI8tNMsozo0&quot;&gt;RailsConf talk&lt;/a&gt;).  This is extraordinary!  It&amp;rsquo;s near the top of my list of software engineering  resources (across all formats - talks, books, etc.).  If you haven&amp;rsquo;t seen the talk, go watch it and I promise you will be so much wiser for it.&lt;/p&gt;

&lt;p&gt;It was a small bit of effort to find the &lt;a href=&quot;/doc/Simple-Made-Easy.pdf&quot;&gt;deck&lt;/a&gt;, so I&amp;rsquo;m hosting it for your convenience.  My notes are a synthesis of the talk, the deck, and some of my own material where I felt it aided in understanding.&lt;/p&gt;

&lt;h2 id=&quot;the-simple&quot;&gt;The Simple&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;A &lt;em&gt;simple&lt;/em&gt; piece of software means that there is no interleaving.  It doesn&amp;rsquo;t combine things.  It has focus.  Something is simple when it addresses:  one role, one task, one concept, one dimension&lt;/li&gt;
  &lt;li&gt;Simple doesn&amp;rsquo;t mean there is only one of them.  Its not about an interface with one operation or a class with one instance.  &lt;em&gt;It&amp;rsquo;s not about cardinality&lt;/em&gt;.&lt;/li&gt;
  &lt;li&gt;By contrast, a &lt;em&gt;complex&lt;/em&gt; system is braided or folded together.  There are interleaving roles, tasks, concepts, or dimensions.&lt;/li&gt;
  &lt;li&gt;Identifying a system as simple or complex is thus objective.  If there are twists, its complex.  If there is no interleaving, it&amp;rsquo;s simple.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;the-easy&quot;&gt;The Easy&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Simple is often (disadvantageously) confused with &lt;em&gt;easy&lt;/em&gt;.  Easy means &lt;em&gt;near&lt;/em&gt;:
    &lt;ul&gt;
      &lt;li&gt;near at hand - ability to start using it locally.  Hickey says, &amp;ldquo;We are infatuated with this aspect of easy.  Can I get this instantly and start installing it in 5 seconds.  It could be this giant hairball…&amp;rdquo; &lt;/li&gt;
      &lt;li&gt;my take:  It&amp;rsquo;s almost a requirement these days that a new framework or library boasts a 4-lines-of-code quick-start.  The implicit message is &amp;ldquo;this is so easy, it must be good.&amp;rdquo;  A year later you wonder if you should have explored it some more before taking the dependency and now you&amp;rsquo;re tangled in the technology.&lt;/li&gt;
      &lt;li&gt;near to our understand or skillset.  It&amp;rsquo;s familiar.  It&amp;rsquo;s like something we already know, plus one new thing.  Hickey says, &amp;ldquo;If you want everything to be familiar you will never learn anything new because it cannot be significantly different from what you already know and not drift away from the familiarity.&amp;rdquo;&lt;/li&gt;
      &lt;li&gt;near to our capabilities,  within our mental capacity, not overly taxing on our brain.  Hickey says, &amp;ldquo;this starts trampling on our egos&amp;hellip;due to combination of hubris and insecurity we never talk about something being outside of our capabilities&amp;rdquo;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Easy is &lt;em&gt;relative&lt;/em&gt; to a specific person.  Easy for whom?  Simple is objective.&lt;/li&gt;
  &lt;li&gt;We are too focused on the experience of programming rather than the artifacts - the programs that gets produced and run.  We should assess quality based on our artifacts, not on the experience of typing programs into an editor!&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;reasoning&quot;&gt;Reasoning&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Humans have limitations:
    &lt;ul&gt;
      &lt;li&gt;If we can&amp;rsquo;t understand our software, we can&amp;rsquo;t make it reliable.  As we make things more flexible and dynamic, we tradeoff ability to understand their nature and ensure they work properly.  &lt;/li&gt;
      &lt;li&gt;my take: We&amp;rsquo;ve all been there - you&amp;rsquo;re traversing a hierarchy of abstract classes and interfaces and the &lt;a href=&quot;https://en.wikipedia.org/wiki/Inversion_of_control&quot;&gt;control was certainly inverted&lt;/a&gt; and the &lt;a href=&quot;https://en.wikipedia.org/wiki/Dependency_injection&quot;&gt;dependencies were injected&lt;/a&gt; and various &lt;a href=&quot;https://en.wikipedia.org/wiki/Design_Patterns&quot;&gt;design patterns&lt;/a&gt; were used and you were just trying to figure out how this one thing works and you&amp;rsquo;re asking yourself &amp;ldquo;why is this so complicated?&amp;rdquo;&lt;/li&gt;
      &lt;li&gt;If two things are intertwined, you have the burden of taking both of them into your mind when you are working on one.  &lt;/li&gt;
      &lt;li&gt;my take: Again, we&amp;rsquo;ve all been there - system A depends on system B being in a certain state, which in turn depends on certain features in system C which behave differently based on the configuration file in system A and luck would have it you&amp;rsquo;ve long forgotten how system C works.&lt;/li&gt;
      &lt;li&gt;Humans are limited in the number of things they can hold in their head.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;We need to reason about our programs to change them - to add new capabilities and to fix bugs.  This is not about &lt;a href=&quot;https://en.wikipedia.org/wiki/Formal_verification&quot;&gt;proving programs&lt;/a&gt; nor category theory, just informal reasoning.
    &lt;ul&gt;
      &lt;li&gt;&amp;ldquo;Apparently I heard in a talk today that Agile and Extreme programming have shown that refactoring and tests allow us to make change with zero impact.  I never knew that.  I still do not know that.  That&amp;rsquo;s not a knowable thing.  That&amp;rsquo;s fooey!  If you&amp;rsquo;re going to change software you need to analyze what it does and make decisions about what it ought to do…if you can&amp;rsquo;t reason about your program you can&amp;rsquo;t make these decisions&amp;rdquo;&lt;/li&gt;
      &lt;li&gt;Beware guardrail programming.  The idea that you can make changes because you have tests.  &amp;ldquo;Who drives their car around banging against the guardrail saying woah I&amp;rsquo;m glad I&amp;rsquo;ve got these guardrails because I&amp;rsquo;d never make it to the show on time&amp;hellip;do the guardrails help you get to where you want to go?&amp;rdquo;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;agility&quot;&gt;Agility&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Developing easy systems (i.e. using &lt;a href=&quot;https://en.wikipedia.org/wiki/Agile_software_development&quot;&gt;Agile&lt;/a&gt;) is faster than developing simple systems at first.  Simple systems require deliberation and thought.  My take: easy systems tend start at a frantic pace.  But if you ignore complexity you will slow down over the long-haul.  In contrast, simple systems accelerate progress over time:
    &lt;ul&gt;
      &lt;li&gt;complex constructs are often easy - they are familiar, available, etc.&lt;/li&gt;
      &lt;li&gt;what matters is the complexity they &lt;em&gt;yield&lt;/em&gt;.  Easy constructs often yield &lt;em&gt;incidental complexity&lt;/em&gt;.  &lt;/li&gt;
      &lt;li&gt;my take: You didn&amp;rsquo;t mean to take on the complexity, but it came for the ride and now what was easy has become a burden.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;em&gt;Architectural agility&lt;/em&gt; is the agility you get from building a system that is fundamentally simple.   This kind of agility dominates all other kinds of agility.  Hickey says, &amp;ldquo;Simplicity enables change&amp;hellip;it’s the primary source of real agility.  Agility means to do something.  It doesn&amp;rsquo;t mean to do it over, it doesn&amp;rsquo;t mean to redo it, it doesn&amp;rsquo;t mean to undo it….It means to directly do.  That&amp;rsquo;s agility.  If you&amp;rsquo;re dragging an elephant around you&amp;rsquo;re never going to be agile&amp;rdquo;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;examples&quot;&gt;Examples&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Lets make this more concrete (I tried linking to resources that explain &lt;em&gt;why&lt;/em&gt; a construct is better or worse, with a fallback to definitions):&lt;/li&gt;
&lt;/ul&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;complex&lt;/th&gt;
      &lt;th style=&quot;text-align: right&quot;&gt;simple&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;a href=&quot;http://stackoverflow.com/questions/844536/advantages-of-stateless-programming&quot;&gt;state&lt;/a&gt;,objects&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;&lt;a href=&quot;http://www.infoq.com/presentations/Value-Values&quot;&gt;values&lt;/a&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;methods&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;&lt;a href=&quot;http://www.defmacro.org/ramblings/fp.html&quot;&gt;functions&lt;/a&gt;, namespaces&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;a href=&quot;http://clojure.org/Vars&quot;&gt;vars&lt;/a&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;&lt;a href=&quot;http://clojure.org/refs&quot;&gt;managed references&lt;/a&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;inheritance, switch statements, pattern matching&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;&lt;a href=&quot;http://stackoverflow.com/questions/13553100/what-is-polymorphism-a-la-carte-and-how-can-i-benefit-from-it&quot;&gt;polymorphism a la carte&lt;/a&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Domain-specific_language&quot;&gt;syntax&lt;/a&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;data&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;imperative loops, &lt;a href=&quot;https://en.wikipedia.org/wiki/Fold_(higher-order_function)&quot;&gt;fold&lt;/a&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Set_function&quot;&gt;set functions&lt;/a&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Actor_model#Fundamental_concepts&quot;&gt;actors&lt;/a&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Message-oriented_middleware#Advantages&quot;&gt;queues&lt;/a&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Object-relational_mapping&quot;&gt;ORM&lt;/a&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Datalog&quot;&gt;declarative data manipulation&lt;/a&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;conditionals&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;&lt;a href=&quot;http://blog.decaresystems.ie/2006/02/03/declarative-programming-business-rules/&quot;&gt;rules&lt;/a&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;inconsistency (&lt;a href=&quot;http://www.allthingsdistributed.com/2008/12/eventually_consistent.html&quot;&gt;eventually consistent&lt;/a&gt;)&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Transaction_processing&quot;&gt;consistency&lt;/a&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;ul&gt;
  &lt;li&gt;What makes each construct in the table above complex?  The deck contains a table with two columns: the constructs and the concepts that each construct intertwines.  He discusses each one briefly in the talk.  I&amp;rsquo;m omitting the table.  It wasn&amp;rsquo;t as helpful for me as researching each construct and reasoning about its complexity.&lt;/li&gt;
  &lt;li&gt;How do you leverage the simple constructs in the table above?  Links from the first table also apply here:&lt;/li&gt;
&lt;/ul&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;construct&lt;/th&gt;
      &lt;th style=&quot;text-align: right&quot;&gt;get it via…&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;values&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;final, persistent collections, immutables&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;functions&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;stateless methods&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;namespaces&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;language support&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;data&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;maps, arrays, sets, JSON, etc.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;polymorphism a la carte&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;protocols, type classes&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;managed refs&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;clojure/haskell refs&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;set functions&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;libraries&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;queues&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;libraries&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;declarative data manipulation&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;SQL/LINQ/Datalog&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;rules&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;libraries, prolog&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;consistency&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;transactions, values&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;designing-simple-systems&quot;&gt;Designing Simple Systems&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Composing simple components is the key to robust software.  Composing is about placing things together, not interleaving  them.  Modules and layers don&amp;rsquo;t imply simplicity (but are enabled by it).&lt;/li&gt;
  &lt;li&gt;Architecture and design has become demonized.  People think it means making grand plans and thus equate it with heavy, failure-prone models such as &lt;a href=&quot;https://en.wikipedia.org/wiki/Waterfall_model&quot;&gt;waterfall&lt;/a&gt;.  Good design is actually about taking things apart.&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;Abstract&lt;/em&gt; means to &lt;em&gt;draw something away&lt;/em&gt;, usually from the physical nature of something.
    &lt;ul&gt;
      &lt;li&gt;It&amp;rsquo;s not about hiding stuff&lt;/li&gt;
      &lt;li&gt;it&amp;rsquo;s is the job of designing and as such its hard to explain how to do it properly.&lt;/li&gt;
      &lt;li&gt;One way to create abstractions is to ask: who, what, when, where, why, how?  Rich explores each of these but I&amp;rsquo;m skipping this because it&amp;rsquo;s partially a repeat of the above and also presented with too broad a brushstroke.&lt;/li&gt;
      &lt;li&gt;Another way is to say &amp;ldquo;I don&amp;rsquo;t know, I don&amp;rsquo;t want to know.&amp;rdquo;&lt;/li&gt;
      &lt;li&gt;my take: If you&amp;rsquo;re working on System A and the topic of System B comes up, you say &amp;ldquo;I don&amp;rsquo;t know, I don&amp;rsquo;t want to know.&amp;rdquo;  As soon as you want to know, you risk interleaving the two systems.  System A plays a role independent of System B.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Is your system simple?  You can apply a litmus test:  Ask the question, &amp;ldquo;Can I move my subsystems around without changing much?&amp;rdquo;  Can you move it out of process?  To a different language?  To a different thread?&lt;/li&gt;
  &lt;li&gt;Information is simple, don&amp;rsquo;t ruin it.  Represent data as data, don&amp;rsquo;t hide it behind classes and methods.  His talk on the &amp;ldquo;&lt;a href=&quot;http://www.infoq.com/presentations/Value-Values&quot;&gt;Value of Values&lt;/a&gt;&amp;rdquo; delves deep into this and is chock-full of value =)&lt;/li&gt;
  &lt;li&gt;Simplicity doesn&amp;rsquo;t fall out of tooling.  You have to do the work!  You need to gain sensibilities around what is simple and what is complex.  Don&amp;rsquo;t lean on tools such as testing, refactoring, or type systems.  They don&amp;rsquo;t care if your program is simple or not.&lt;/li&gt;
&lt;/ul&gt;

</content>
   <rights>&#169; 2012 Suraj Gupta</rights>
 </entry>
 
 <entry>
   <title>A Warning About warning()</title>
   <link href="http://obeautifulcode.com/R/A-Warning-About-Warning"/>
   <updated>2012-05-02T00:00:00-04:00</updated>
   <id>http://obeautifulcode.com/R/A-Warning-About-Warning</id>
   <content type="html">&lt;p&gt;Avoid R&amp;rsquo;s &lt;code&gt;warning&lt;/code&gt; feature.  This is particularly important if you use R in production; when you regularly run R scripts as part of your business process.  This is also important if you author R packages.  Don&amp;rsquo;t issue warnings in your own code and treat warnings in 3rd party code as errors that are explicitly suppressed.  I&amp;rsquo;ll discuss a strategy to implement this in a little bit, but first lets discuss the &lt;code&gt;warning&lt;/code&gt; feature and the justification for this advice.&lt;/p&gt;

&lt;h2 id=&quot;the-warning&quot;&gt;The Warning&lt;/h2&gt;
&lt;p&gt;A warning cautions users without halting the execution of a function. It basically says &amp;ldquo;&lt;em&gt;although I can and will give you an answer, there might be a problem with your inputs.  Thus, the computation could be flawed.&lt;/em&gt;&amp;rdquo;  For example, the correlation function issues a warning when an input vector has a standard deviation of 0.  Rather than raising an error and halting execution, the function issues a warning and returns NA.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;r&quot;&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; cor&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; c&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; c&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;NA&lt;/span&gt;
Warning message&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;
In cor&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;c&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; c&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; the standard deviation is zero
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;To issue a warning simply call the &lt;code&gt;warning()&lt;/code&gt; function&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;r&quot;&gt;Division &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; x &lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; y &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kr&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; y &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; warning&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;Divide by 0&amp;quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;kr&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; x &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; y &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; Division&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;Inf&lt;/span&gt;
Warning message&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;
In Division&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; Divide by &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Multiple warnings can be issued during a function call.  They can originate from the function itself or from child functions.  R stores all warnings and prints them out after the function returns.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;r&quot;&gt;ParentFunction &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    warning&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;warning from parent&amp;quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    ChildFunction1&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    ChildFunction2&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    warning&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;second warning from parent&amp;quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;kr&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;TRUE&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

ChildFunction1 &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;  warning&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;warning from child&amp;quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
ChildFunction2 &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;  warning&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;second warning from child&amp;quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; ParentFunction&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;TRUE&lt;/span&gt;
Warning messages&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; In ParentFunction&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; warning from parent
&lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; In ChildFunction1&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; warning from child
&lt;span class=&quot;m&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; In ChildFunction2&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; second warning from child
&lt;span class=&quot;m&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; In ParentFunction&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; second warning from parent
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h2 id=&quot;avoid-warning&quot;&gt;Avoid Warning&lt;/h2&gt;
&lt;p&gt;You should avoid warnings (as a producer and a consumer) for three reasons:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;It adds unnecessary complexity. &lt;br /&gt;
Functions should either succeed or fail and it should be as simple as that.  Warnings introduce a third state.  A function author must consider when to issue a warning, how to message it, and how the function will proceed in the warning/caution state.  Its difficult enough to write a good function without this burden.  On the flip side, a function consumer must decide how to handle a warning.  Do you continue or halt?  Does it depend on the warning?  If so, do you switch on specific warning messages or keywords within the message?  What if the warning message changes with an updated package?  It&amp;rsquo;s far less complex when a function just fails; there&amp;rsquo;s less for both parties to do.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Most warnings are actually errors. &lt;br /&gt;
This is anecdotal, but I&amp;rsquo;d bet it generalizes well: 80% of the time I see warnings when there&amp;rsquo;s a bug in the inputs to a function call.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;You will miss critical errors. &lt;br /&gt;
In an automated/production environment dealing with warnings is a pain.  You might miss warnings outright because no human will see it.  Per #2 above, this could mean bad things.  Otherwise, you will have to pipe a script&amp;rsquo;s output to a file, parse it to detect the warning and notify someone.  Lets say you pull that off.  You are still betting that someone will heed the warnings in a timely manner.  Also, you&amp;rsquo;re left with a time-consuming and ambiguous task of deciding which warnings to addressed.  If, however, your script just failed, I&amp;rsquo;m guessing you will find out pretty quick and you will have to pay attention to the issue.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;step1-stop-warning&quot;&gt;Step1: Stop Warning&lt;/h2&gt;
&lt;p&gt;Instead of issuing a warning, do one of the following:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Call &lt;code&gt;stop(&quot;error message here&quot;)&lt;/code&gt;.  This will generate an error and halt execution.&lt;/li&gt;
  &lt;li&gt;Add a function parameter that specifies the action to take when the inputs are malformed.  This is mostly likely a boolean (logical) parameter that indicates whether the function should fail or gives the function permission to proceed.  Use a descriptive parameter.  Some good prefixes are &lt;code&gt;ignore&lt;/code&gt; and &lt;code&gt;fail&lt;/code&gt;.  For example, I&amp;rsquo;ve added parameters &lt;code&gt;failSd0&lt;/code&gt; and &lt;code&gt;ignoreSd0&lt;/code&gt; below.&lt;/li&gt;
&lt;/ol&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;r&quot;&gt;Cor&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; x &lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; y &lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; failSd0 &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kr&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; failSd0 &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; equals&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; sd&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; x &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; stop&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;the standard deviation of x is 0&amp;quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;kr&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; failSd0 &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; equals&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; sd&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; y &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; stop&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;the standard deviation of y is 0&amp;quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;kc&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

Cor&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; x &lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; y &lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; ignoreSd0  &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kr&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;ignoreSd0 &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; equals&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; sd&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; x &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; stop&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;the standard deviation of x is 0&amp;quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;kr&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;ignoreSd0 &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; equals&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; sd&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; y &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; stop&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;the standard deviation of y is 0&amp;quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;kc&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Just deciding between approach #1 and approach #2 is a useful exercise.  It challenges you to judge the utility of a function in the face of specific categories of input (i.e. correlation when an input vector has standard deviation of 0).  You might admit that a function&amp;rsquo;s outputs have little to no value when the inputs have a particularly quality.  In that case, you would use approch #1 and just &lt;code&gt;stop()&lt;/code&gt; the execution.  This is the least complex thing to do.&lt;/p&gt;

&lt;p&gt;Although solution #2 is a bit more work, it provides a wonderful benefit to your users.  It gives them information about how &lt;em&gt;not&lt;/em&gt; call your function.  In other words, it helps them minimize failure.  This information is nicely embedded into the parameter list.  When a user sees the parameter &lt;code&gt;failSd0&lt;/code&gt; it triggers the most obvious question: &amp;ldquo;Is it possible that my &lt;code&gt;x&lt;/code&gt; or &lt;code&gt;y&lt;/code&gt; has a standard deviation of 0?  Maybe I should check the code that populates &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt; just in case.&amp;rdquo;&lt;/p&gt;

&lt;h2 id=&quot;step2-strategically-maneuver-3rd-party-warnings&quot;&gt;Step2: Strategically Maneuver 3rd Party Warnings&lt;/h2&gt;
&lt;p&gt;To consume 3rd party code (i.e. CRAN packages) that issues warnings, do the following:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;Treat all warnings are errors. &lt;br /&gt;
Take the complexity out of the equation by first assuming that all warnings are in fact errors.  Simply call &lt;code&gt;options( warn = 2 )&lt;/code&gt;.  R will convert the first warning it encounters into an error and halt execution.  I recommend putting this into your &lt;code&gt;Rprofile.site&lt;/code&gt; file so that this treatment is always applied.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Selectively suppress warnings. &lt;br /&gt;
There are times when a function issues a warning and you are certain that your inputs are correct and you are happy with the results.  In this case, wrap the function call in &lt;code&gt;suppressWarnings()&lt;/code&gt;.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;r&quot;&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; options&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; warn &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; ParentFunction&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
Error &lt;span class=&quot;kr&quot;&gt;in&lt;/span&gt; ParentFunction&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;converted from warning&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; warning from parent
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; suppressWarnings&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; ParentFunction&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;TRUE&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This is a case-by-case opt-in.  Don&amp;rsquo;t wrap everything in &lt;code&gt;suppressWarnings()&lt;/code&gt;!  Be sure to add a clear justification in the comments:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;r&quot;&gt;&lt;span class=&quot;c1&quot;&gt;# FictitiousFunction warns when myValue is 0.  myValue will always be 0.  &lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# This only affects result$ABC output which is fixed at NA.  We don&amp;#39;t use this output.  &lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# All other elements of the result list are as expected.&lt;/span&gt;
suppressWarnings&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; FictitiousFunction&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; myValue &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Going-forward, your code could find itself in a state that triggers a 3rd party warning that you&amp;rsquo;ve never seen before.  Congrats!  This is a quality problem.  Your code will raise an error.  You can investigate the issue and in turn learn more about the package you depend on and about the behavior of your own code.  And you can always &lt;code&gt;suppressWarnings()&lt;/code&gt;. Your code will become more robust and more resilient.&lt;/p&gt;

&lt;h2 id=&quot;practice-what-you-preach&quot;&gt;Practice What You Preach&lt;/h2&gt;
&lt;p&gt;I have many thousands of lines of R code in production and I employ the strategy described above to avoid warnings.  It&amp;rsquo;s a beautiful thing - I have never seen a single raw warning from R.  My code simply passes or fails; there&amp;rsquo;s no third-state to deal with.  Also, I sleep well knowing that my nightly processes don&amp;rsquo;t mask errors that are expressed as warnings.&lt;/p&gt;

</content>
   <rights>&#169; 2012 Suraj Gupta</rights>
 </entry>
 
 <entry>
   <title>Version Control - the Host Matters</title>
   <link href="http://obeautifulcode.com/VersionControl/Version-Control-The-Host-Matters"/>
   <updated>2012-04-19T00:00:00-04:00</updated>
   <id>http://obeautifulcode.com/VersionControl/Version-Control-The-Host-Matters</id>
   <content type="html">&lt;p&gt;We&amp;rsquo;re moving from internally hosted SVN to &lt;a href=&quot;http://git-scm.com/&quot;&gt;Git&lt;/a&gt;/&lt;a href=&quot;https://github.com/&quot;&gt;GitHub&lt;/a&gt; for the typically reasons (resounding hallelujah).  &lt;a href=&quot;http://mercurial.selenic.com/&quot;&gt;Mercurial&lt;/a&gt; was a contender.  We surveyed the available hosts and choose Git &lt;em&gt;because&lt;/em&gt; of GitHub.  Its an old adage at play here - betting on a superior toolset is just as important as betting on a technology.  The benefits of Git and Mercurial largely overlap since they are distributed.  Had a GitHub-like site existed for Mercurial then likely we would have sided with Mercurial for its simplicity.  It&amp;rsquo;s possible to use Mercurial to push/pull from a Git repository through the &lt;a href=&quot;http://hg-git.github.com/&quot;&gt;Hg-Git plugin&lt;/a&gt;, but this scenario will always be at risk as the respective technologies evolve.  Also, intentionally taking-on an additional dependency this early in the game seems awkward and unwise when it can be avoided.  &lt;/p&gt;

&lt;p&gt;The host is not the only tool in the chain, but for our purposes it&amp;rsquo;s the most impactful variable.   Both Mercurial and Git have decent &lt;a href=&quot;http://stackoverflow.com/questions/931105/tortoisegit-tortoisebzr-tortoisehg-are-any-solid-enough-to-switch-from-tortoi&quot;&gt;shell extensions&lt;/a&gt; with &lt;a href=&quot;http://tortoisehg.bitbucket.org/&quot;&gt;TortoiseHg&lt;/a&gt; and &lt;a href=&quot;http://code.google.com/p/tortoisegit/&quot;&gt;TortoiseGit&lt;/a&gt;.  These are enhanced by replacing the &lt;a href=&quot;http://stackoverflow.com/questions/12625/best-diff-tool&quot;&gt;diff tool&lt;/a&gt; with &lt;a href=&quot;http://www.scootersoftware.com/&quot;&gt;Beyond Compare&lt;/a&gt;.  So the non-command-line-use-case is off the table.  I consider the command-line tools to be a part of the core technology, but regardless hg.exe and git.exe are straight-forward for the usual tasks.&lt;/p&gt;

&lt;h2 id=&quot;hosts&quot;&gt;Hosts&lt;/h2&gt;
&lt;p&gt;I surveyed quite a few hosts including GitHub, Kiln, Java.net, Gitorious, SourceForge, BitBucket, RepositoryHosting, CodePlex, and Google Code.  I was looking for a simple and clean UI with fluid code navigation, quick discovery and triage of code changes (code review), and ease of capturing and organizing bugs .  And of course the repository needs to be private and secure.  A few themes emerged:&lt;/p&gt;

&lt;h2 id=&quot;evaluation&quot;&gt;Evaluation&lt;/h2&gt;
&lt;p&gt;It was either easy or difficult to evaluate products.  The best software is usually the easiest to evaluate.  The publisher wants desperately for you use their stuff.  Once you try it you&amp;rsquo;ll just have to buy it because its clearly fantastic.   On GitHub (and others), its hard NOT to land on a public repository in 2 clicks .  Kiln was a disappointment.  I had to signup for a trial, clone a public repository, and push it to my private account just to discover that I didn&amp;rsquo;t care for their Windows Explorer-like code navigation nor the admin drop-downs.  Screenshots don&amp;rsquo;t cut it.  Publisher will display their snazziest UI (like Kiln&amp;rsquo;s branch and merge).  There&amp;rsquo;s no substitute for getting your hands dirty with software and any barriers to doing that is a red flag in my opinion.&lt;/p&gt;

&lt;h2 id=&quot;homepage&quot;&gt;Homepage&lt;/h2&gt;
&lt;p&gt;The repository homepage is either a dashboard of relevant/new information, the source tree, or change/commit information.  The best choice is up for debate and depends on preference.  I can see the argument for change/commit.  A healthy codebase evolves over time and its useful to focus on what&amp;rsquo;s happening.  Dashboards are terribly difficult to get right without personalization which itself complicates the experience and adds another &amp;ldquo;to-do&amp;rdquo; to the never ending to-do list.  I prefer to land at the root of my source tree. Developers live inside source.  When I come home, I want to see my home before I&amp;rsquo;m told what&amp;rsquo;s been fixed inside and before someone nags me about what broke when I was gone.&lt;/p&gt;

&lt;h2 id=&quot;code&quot;&gt;Code&lt;/h2&gt;
&lt;p&gt;I love that GitHub displays a folder&amp;rsquo;s README file under the code navigation block and that it renders &lt;a href=&quot;http://daringfireball.net/projects/markdown/&quot;&gt;Markdown&lt;/a&gt;.  At the root/hompage, I intend to seed a README with my company&amp;rsquo;s tenants of maintaining a quality codebase.  This will be reinforced every time a developer browses to the repository.  READMEs at various levels of depth beneath the root are a great way to capture high visibility organizational and project-specific information.  GitHub&amp;rsquo;s slide-screen navigation is slick as heck.  Its easy to pop in and out of folders with the forward and back buttons on the mouse.  I think they nailed the navigation.  I also like they way they render code; its easy on the eye and they seems to support lots of languages through the &lt;a href=&quot;http://pygments.org/&quot;&gt;Pygments&lt;/a&gt; library.&lt;/p&gt;

&lt;h2 id=&quot;organization&quot;&gt;Organization&lt;/h2&gt;
&lt;p&gt;You get it&amp;hellip;less is more.  We live in an Apple world of design.  A example of poorly designed navigation is SourceForge.  I&amp;rsquo;ve seen projects with 9 tabs, some of which drop-down to reveal yet more options!  &lt;/p&gt;

&lt;p&gt;It seems that repository hosts are trying to serve two populations with somewhat competing needs: open source projects and businesses.  Take GitHub.  I would happily trade the &amp;ldquo;Network&amp;rdquo; and &amp;ldquo;Stats &amp;amp; Graphs&amp;rdquo; tabs for a &amp;ldquo;Downloads&amp;rdquo; section.  The former are fun and interesting, but in my opinion lack business value.  If you are in need of these metrics then likely you are disconnected from the real progress of your organization or you&amp;rsquo;re a micromanager.  Aside from a &amp;ldquo;Code&amp;rdquo; tab (why would you call it anything but that??) I think all high-level units of navigation should be optional (think checkboxes in admin section).  This forces some hard thinking about how best to group features.&lt;/p&gt;

&lt;h2 id=&quot;bug-tracker-wiki&quot;&gt;Bug Tracker, Wiki&lt;/h2&gt;
&lt;p&gt;Some hosts have built-in bug trackers, some provide this functionality as an &amp;ldquo;add-on&amp;rdquo;, and some let you hook into other bug tracking systems.  I&amp;rsquo;ll take a no-frills, built-in system any day.  Psychologically, there&amp;rsquo;s something valuable about seeing bugs aside source.  The dev team/test team dichotomy is under attack for good reasons.  Developers need easy access to the bug database.  They should be filing bugs and they should treat bugs as normal process in their coding workflow.  The bug tracker is as much a first-class citizen as the source itself.  The two are interrelated and it makes tons of sense to manifest that in the repository.  Besides, its time-consuming managing multiple systems.&lt;/p&gt;

&lt;p&gt;A wiki is natural place to put documentation.  If you&amp;rsquo;re not using a wiki, then likely you&amp;rsquo;re hosting docs on some server.  It could be as low-tech as a  document fileshare.  Maybe you installed SharePoint (sorry, I just puked.  When I left Microsoft I vowed never to use two products.  SharePoint was one.  I&amp;rsquo;ll let you guess the other).  In 2007 I would have accepted the usability argument against wikis - that its simply easier to create documentation in a rich-client like Word.  But times have changed and these systems have gotten better.  GitHub uses &lt;a href=&quot;https://github.com/github/gollum&quot;&gt;gollum&lt;/a&gt; for wikis.  Its simple and effective and doubly intuitive since the wiki is just a rendered Git repository.  As I said before, its time-consuming managing multiple systems.  Why not put your technical documentation next to the &amp;ldquo;technical&amp;rdquo; that&amp;rsquo;s being documented? =)&lt;/p&gt;

&lt;h2 id=&quot;user-adoption&quot;&gt;User Adoption&lt;/h2&gt;
&lt;p&gt;Lets face it, that new open source project you read about on &lt;a href=&quot;http://news.ycombinator.com/&quot;&gt;Hacker News&lt;/a&gt; has a 80% chance of being hosted on GitHub.  It&amp;rsquo;s the wisdom of crowds.  More and more my Google searches land on a GitHub repository.  There are lots of people invested in GitHub&amp;rsquo;s success.  &lt;/p&gt;

</content>
   <rights>&#169; 2012 Suraj Gupta</rights>
 </entry>
 
 <entry>
   <title>IRTFMAIS</title>
   <link href="http://obeautifulcode.com/Funny/IRTFMAIS"/>
   <updated>2012-04-12T00:00:00-04:00</updated>
   <id>http://obeautifulcode.com/Funny/IRTFMAIS</id>
   <content type="html">&lt;p&gt;&lt;br /&gt;&lt;/p&gt;
&lt;center&gt;&lt;h1&gt;I Read The F***ing Manual And It Sucks&lt;/h1&gt;
&lt;br /&gt;
&lt;a href=&quot;http://xkcd.com/942/&quot;&gt;&lt;img src=&quot;http://imgs.xkcd.com/comics/juggling.png&quot; alt=&quot;juggling&quot; /&gt;&lt;/a&gt;
&lt;br /&gt;
&lt;a href=&quot;http://xkcd.com/942/&quot;&gt;from xkcd&lt;/a&gt;
&lt;/center&gt;
&lt;p&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;
</content>
   <rights>&#169; 2012 Suraj Gupta</rights>
 </entry>
 
 <entry>
   <title>R Source on GitHub</title>
   <link href="http://obeautifulcode.com/R/R-Source-On-GitHub"/>
   <updated>2012-04-10T09:50:00-04:00</updated>
   <id>http://obeautifulcode.com/R/R-Source-On-GitHub</id>
   <content type="html">&lt;ul&gt;
  &lt;li&gt;I added R source code v0.49 to v2.15.0 to a GitHub repository: &lt;a href=&quot;https://github.com/SurajGupta/r-source&quot;&gt;r-source&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Each release is &lt;a href=&quot;https://github.com/SurajGupta/r-source/tags&quot;&gt;tagged&lt;/a&gt; by version number.&lt;/li&gt;
  &lt;li&gt;This is an easy and accessible way to browse R source and diff with prior version.  I couldn&amp;rsquo;t find a suitable alternative.&lt;/li&gt;
  &lt;li&gt;A tarball via Apache directory listing via CRAN mirror via the &lt;strong&gt;__&lt;/strong&gt;__ (insert sarcastic adjective) R-project website is not a suitable alternative.&lt;/li&gt;
  &lt;li&gt;I will keep the repository updated as new versions of R are released.&lt;/li&gt;
&lt;/ul&gt;

</content>
   <rights>&#169; 2012 Suraj Gupta</rights>
 </entry>
 
 <entry>
   <title>R, I Love You</title>
   <link href="http://obeautifulcode.com/R/R-I-Love-You"/>
   <updated>2012-04-05T00:00:00-04:00</updated>
   <id>http://obeautifulcode.com/R/R-I-Love-You</id>
   <content type="html">&lt;p&gt;It is easier to critique than it is to create.  I write this post with much gratitude for R, the R community and particularly R-Core who are paid $0 to bring us R.  I&amp;rsquo;d like to offer an idea and I&amp;rsquo;m wondering if people are interested in rallying around it.&lt;/p&gt;

&lt;h2 id=&quot;julia-im-in-committed-relationship&quot;&gt;Julia, I&amp;rsquo;m in committed relationship&lt;/h2&gt;
&lt;p&gt;You might have caught the post titled &amp;ldquo;&lt;a href=&quot;http://www.johnmyleswhite.com/notebook/2012/03/31/julia-i-love-you/&quot;&gt;Julia, I Love You&lt;/a&gt;&amp;rdquo;.  It&amp;rsquo;s the top article on &lt;a href=&quot;http://www.r-bloggers.com/&quot;&gt;Rbloggers&lt;/a&gt;.  Perhaps you had the same reaction I did.  I read the material, repeated &amp;ldquo;wow&amp;rdquo; a few times, and slipped into a contemplative space.  Am I betting on an outdated technology?  I slapped myself (figuratively) and snapped back to reality.    I vaguely remember when &lt;a href=&quot;http://www.revolutionanalytics.com/&quot;&gt;Revolution Analytics&lt;/a&gt; released &lt;a href=&quot;http://www.revolutionanalytics.com/why-revolution-r/benchmarks.php&quot;&gt;side-by-side performance&lt;/a&gt; figures there was pushback about an apples-to-oranges comparison.  Some tests had to be reworked (or am I just make that up?).  People have added comments to the Julia post with performance fixes to the R code used to benchmark against Julia.  And in the end, languages come and go but R has withstood the test of time. &lt;/p&gt;

&lt;h2 id=&quot;i-use-r--julia-because--&quot;&gt;I use R | Julia because. . .&lt;/h2&gt;
&lt;p&gt;Why do people use R?  In my (informal, anecdotal, not rigorous, no medals of honor conferred) survey, the reasons people use R are:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;It&amp;rsquo;s free&lt;/li&gt;
  &lt;li&gt;There are lots of packages on CRAN&lt;/li&gt;
  &lt;li&gt;Its easy to code&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Food for thought:: Julia has #1 and #3 covered and #2 is just a matter of time if the adoption curve is upward sloping.  All things being equal&amp;hellip;&lt;/p&gt;

&lt;h2 id=&quot;performance-is-an-issue&quot;&gt;Performance IS an issue&lt;/h2&gt;
&lt;p&gt;Something is bugging me.  What&amp;rsquo;s bugging me is how defeated I feel when I see R benchmarked against anything.  I see the figures and I think, &amp;ldquo;it is what it is.&amp;rdquo;  I&amp;rsquo;m sure the R-defenders will point to all sorts of stats and tell me that I&amp;rsquo;m wrong.  I&amp;rsquo;ll still shrug.  I use R on a daily basis and it consistently feels like an order of magnitude slower than my internal benchmark (lots of hand-waving there).  Yeah, I know I can use C++.  Its not my cup of tea.  If it were I would just use C++ and I wouldn&amp;rsquo;t try to shim it into R.  I&amp;rsquo;m using R because I want to write in R.  I do byte-compile all packages, which helps a little.&lt;/p&gt;

&lt;p&gt;Also, R is lacking on multicore.  On Windows its not possible to run tasks in parallel, in-process (if you have gotten this to work, please let me know).  Out-of-process communication is slow and there&amp;rsquo;s the memory tax of maintaining multiple instances of R.  Most of the time all cores but one are idle.  Tech moves fast.  If we don&amp;rsquo;t challenge ourselves then we decay.&lt;/p&gt;

&lt;h2 id=&quot;r-i-love-you-so-heres-my-idea&quot;&gt;R, I Love You, so here&amp;rsquo;s my idea&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Stop writing new features&lt;/strong&gt;.  R has enough features.  Lets make 2.16.0 a performance fix release.  This is not  an uncommon practice.  Instead of treating performance as something we tackle incrementally, lets steer our open source mindshare towards the single goal of performance for a release or two.  Lets excite developers who get their jollies from optimization.  &lt;/p&gt;

&lt;p&gt;Heck, lets pay for it if we have to.  There&amp;rsquo;s an &lt;a href=&quot;http://www.r-project.org/foundation/main.html&quot;&gt;R Foundation&lt;/a&gt;, right?  &lt;a href=&quot;http://www.kickstarter.com/&quot;&gt;Kickstarter&lt;/a&gt; has been enormously successful in raising funds for projects that people care about.  If a team on Kickstarter can raise $1M for a video game that has a limited shelf life, surely we can raise capital for a tool that some of us practically live in.  People will donate because they stand to benefit.&lt;/p&gt;

&lt;p&gt;We can organize around 3 broad categories:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;High performance, multi-core math libraries&lt;/li&gt;
  &lt;li&gt;JIT&lt;/li&gt;
  &lt;li&gt;Lightweight, in-process task parallization on all major platforms&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So that&amp;rsquo;s my idea.  Thoughts?&lt;/p&gt;

</content>
   <rights>&#169; 2012 Suraj Gupta</rights>
 </entry>
 
 <entry>
   <title>How R Searches and Finds Stuff</title>
   <link href="http://obeautifulcode.com/R/How-R-Searches-And-Finds-Stuff"/>
   <updated>2012-03-29T00:00:00-04:00</updated>
   <id>http://obeautifulcode.com/R/How-R-Searches-And-Finds-Stuff</id>
   <content type="html">&lt;div class=&quot;floatImageRight&quot;&gt;
    &lt;img src=&quot;/img/how-r-searches-and-finds-stuff/rabbit-hole.jpg&quot; alt=&quot;Rabbit Hole&quot; /&gt;
&lt;/div&gt;
&lt;p&gt;Or&amp;hellip;&lt;br /&gt;
&lt;strong&gt;How to push oneself down the rabbit hole of environments, namespaces, exports, imports, frames, enclosures, parents, and function evaluation?&lt;/strong&gt;&lt;/p&gt;

&lt;h2 id=&quot;motivation&quot;&gt;Motivation&lt;/h2&gt;
&lt;p&gt;There are a few reasons to bother reading this post:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;em&gt;Rabbit hole avoidance&lt;/em&gt; &lt;br /&gt;
You have avoided the above mentioned topics thus far, but now it&amp;rsquo;s time to dive in.  Unfortunately you speak English, unlike the R help manuals which speak &amp;ldquo;Hairy C&amp;rdquo;  (imagine a somewhat hairy native C coder from the 80s who&amp;rsquo;s really smart but grunts a lot&amp;hellip;not the best communicator).&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;em&gt;R is acting a fool&lt;/em&gt; &lt;br /&gt;
Your function used to work, now it spits an error.  Absolutely nothing about this particular function has changed.  You vaguely remember installing a new package, but what does that matter?  Unfortunately my friend, it does matter.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;em&gt;R is finding the wrong thing&lt;/em&gt; &lt;br /&gt;
You attached the &lt;a href=&quot;http://cran.r-project.org/web/packages/matlab/index.html&quot;&gt;matlab package&lt;/a&gt; and call &lt;code&gt;sum()&lt;/code&gt; on a numeric matrix.  The result is a vector of column sums, not a length 1 numeric.  This messes up everything.  What were you thinking trying to make R act like Matlab?  Matlab is for losers (and rich people).&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;em&gt;You want R to &lt;a href=&quot;http://stackoverflow.com/questions/8204008/redirect-intercept-function-calls-within-a-package-function&quot;&gt;find something else&lt;/a&gt;&lt;/em&gt; &lt;br /&gt;
You like a package&amp;rsquo;s plotting function.  If you could intercept one call within the function and use your own calculation, it would be perfect.  This seems like black magic to you, but something is strange about maintaining a full copy of the function just to apply your little tweak.  Welcome to the dark arts.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;em&gt;Package authoring&lt;/em&gt;  &lt;br /&gt;
You have authored a package.  How does your kid plays with the other kids in the playground?&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;where-does-r-put-things&quot;&gt;Where does R put things?&lt;/h2&gt;
&lt;p&gt;Everything is R lives in an &lt;strong&gt;environment&lt;/strong&gt;.  Ultimately, we&amp;rsquo;re trying to figure out which environment something lives in, and how we get there (or how we got there).  An environment, like  everything else in R is an object.  Objects hold stuff.  Environments are specialized, they can only hold two things:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;A &lt;strong&gt;frame&lt;/strong&gt;  &lt;br /&gt;
This is just a collection of named objects.  What&amp;rsquo;s a named object?  Everything in R is an object, so it can be a function, a numeric, a character, a logical, etc.  And these objects have names like &lt;em&gt;myFunction&lt;/em&gt;, &lt;em&gt;myNumeric&lt;/em&gt; , &lt;em&gt;myCharacter&lt;/em&gt;, &lt;em&gt;myLogical&lt;/em&gt;.  When you type  &lt;code&gt;myVar = &quot;charlie&quot;&lt;/code&gt; at the command prompt you create a character object that holds the string &lt;code&gt;&quot;charlie&quot;&lt;/code&gt; and you&amp;rsquo;re calling that object &lt;code&gt;myVar&lt;/code&gt;.  Since everything in R lives in an environment and the frame is the place where an environment holds objects, then the object you just created called &lt;code&gt;myVar&lt;/code&gt; lives in a frame within some environment.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;The environment&amp;rsquo;s owner aka the &lt;strong&gt;enclosing environment&lt;/strong&gt;.   &lt;br /&gt;
This is just a reference to another environment.  &lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;img src=&quot;/img/how-r-searches-and-finds-stuff/environment-structure.png&quot; alt=&quot;environment structure&quot; /&gt;&lt;/p&gt;

&lt;p&gt;As you can see from the visualization above, the chain of enclosing environments stops at a special environment called the &lt;strong&gt;Empty Environment&lt;/strong&gt;.  You can access this object by executing &lt;code&gt;emptyenv()&lt;/code&gt; in R.  And given an environment object, you can query the object for the two things that matter: the environment&amp;rsquo;s owner and the objects in the frame. &lt;/p&gt;

&lt;h3 id=&quot;a-fib-about-owners-and-pointers&quot;&gt;A Fib About Owners and Pointers&lt;/h3&gt;
&lt;p&gt;Before you hit send on that flame mail I acknowledge a technical misdirection that I have and will continue to make.  I use the concepts of &lt;em&gt;ownership&lt;/em&gt; and &lt;em&gt;containment&lt;/em&gt; loosely.  I will say &lt;em&gt;own&lt;/em&gt; or &lt;em&gt;contain&lt;/em&gt; when I really mean &lt;em&gt;pointer&lt;/em&gt;.  If &lt;em&gt;pointer&lt;/em&gt; means nothing to you then &lt;strong&gt;skip to the next section&lt;/strong&gt;.  &lt;/p&gt;

&lt;p&gt;I will continue to talk about environments as &lt;em&gt;owning&lt;/em&gt; objects, particularly functions.  In truth functions are instructions stored somewhere in memory, and they are accessed by symbols in lookup tables that, when found, give a pointer to them.  The core concepts of this article are agnostic to pointers and understanding pointers is unnecessary to achieve mastery of the search mechanism in R.  In fact, R tries really hard to hide pointers.  So yes it pains me to be technically imprecise, but I&amp;rsquo;m trying to keep things simple because most people can understand an ownership relationship and we have a lot of ground to cover.&lt;/p&gt;

&lt;h2 id=&quot;play-time-with-environments-dont-skip-me&quot;&gt;Play time with Environments (don&amp;rsquo;t skip me)&lt;/h2&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;r&quot;&gt;&lt;span class=&quot;c1&quot;&gt;# environments are just objects.  lets create one.&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; myEnvironment &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; new.env&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; 

&lt;span class=&quot;c1&quot;&gt;# print it out...&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; myEnvironment 
&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;environment&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x0000000006ce0920&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# every environment (except R_EmptyEnv) has an enclosure.&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# Who&amp;#39;s myEnvironment&amp;#39;s enclosure?  It&amp;#39;s &amp;quot;R_GlobalEnv&amp;quot; - find out using parent.env()&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; parent.env&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; myEnvironment &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; 
&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;environment&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; R_GlobalEnv&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Who&amp;#39;s R_GlobalEnv&amp;#39;s enclosing environment? &lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# Its the environment called &amp;quot;package:stats&amp;quot; (in my installation, might be different on yours)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; parent.env&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; parent.env&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; myEnvironment &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; 
&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;environment&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; package&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;stats&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
attr&lt;span class=&quot;p&quot;&gt;(,&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;name&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;package:stats&amp;quot;&lt;/span&gt;
attr&lt;span class=&quot;p&quot;&gt;(,&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;path&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;C:/R/R-2.14.1/library/stats&amp;quot;&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Here&amp;#39;s two other ways to ask the same question. &lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# This R_GlobalEnv must be special if it can retrieved using the identifier&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# .GlobalEnv AND a function globalenv().  We&amp;#39;ll discuss R_GlobalEnv later.&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; parent.env&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; .GlobalEnv &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; 
&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;environment&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; package&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;stats&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
attr&lt;span class=&quot;p&quot;&gt;(,&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;name&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;package:stats&amp;quot;&lt;/span&gt;
attr&lt;span class=&quot;p&quot;&gt;(,&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;path&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;C:/R/R-2.14.1/library/stats&amp;quot;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; parent.env&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; globalenv&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;environment&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; package&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;stats&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
attr&lt;span class=&quot;p&quot;&gt;(,&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;name&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;package:stats&amp;quot;&lt;/span&gt;
attr&lt;span class=&quot;p&quot;&gt;(,&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;path&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;C:/R/R-2.14.1/library/stats&amp;quot;&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# The empty environment is accessed using emptyenv() &lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; emptyenv&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;environment&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; R_EmptyEnv&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Why does myEnvironment have a funky name 0x0000000006ce0920? &lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# That&amp;#39;s just the location of the environment in memory.&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# We can add a friendly name by assigning a &amp;quot;name&amp;quot; attribute.&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# Unfortunately R doesn&amp;#39;t replace the funky name with the friendly name when printing.&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# We can use the environmentName() function to verify our cool name &lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; attr&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; myEnvironment &lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;name&amp;quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;Cool Name&amp;quot;&lt;/span&gt; 
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; myEnvironment 
&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;environment&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x0000000006ce0920&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
attr&lt;span class=&quot;p&quot;&gt;(,&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;name&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;Cool Name&amp;quot;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; environmentName&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; myEnvironment &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; 
&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;Cool Name&amp;quot;&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# lets create a numeric object&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; myValue &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;5&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Unless you try hard, when you create an object it is automatically placed in the &lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# &amp;quot;current&amp;quot; or &amp;quot;local&amp;quot; environment, accessible using environment()&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; environment&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; 
&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;environment&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; R_GlobalEnv&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# And we can query an environment for all objects in the frame using ls().&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# Here we verify that objects myEnvironment and myValue are both placed in the local environment, R_GlobalEnv&lt;/span&gt;
ls&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; envir &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; environment&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;myEnvironment&amp;quot;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;myValue&amp;quot;&lt;/span&gt;      

&lt;span class=&quot;c1&quot;&gt;# We can override the default behavior and create an object in an environment other than the local environment.  &lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# To do this use the assign() function.  Here we create variable &amp;quot;myLogical&amp;quot; inside myEnvironment.&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# We use ls() to verify that there was nothing in myEnvironment before the assignment, &lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# and again ls() verifies that &amp;quot;myLogical&amp;quot; is inside myEnvironment after the assignment&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; ls&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; envir &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; myEnvironment &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
character&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; assign&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;myLogical&amp;quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; c&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;FALSE&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;TRUE&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; envir &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; myEnvironment &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; ls&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; envir &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; myEnvironment &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;myLogical&amp;quot;&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# We can retrieve any named object from any given environment using the get() function&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; get&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;myLogical&amp;quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; envir &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; myEnvironment &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;FALSE&lt;/span&gt;  &lt;span class=&quot;kc&quot;&gt;TRUE&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# How could I have known that myEnvironment&amp;#39;s enclosure would be R_GlobalEnv before I created the object? &lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# Once again, R uses the local environment as the default value.  &lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# You can change an environment&amp;#39;s enclosure using the replacement form of parent.env().&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; myEnvironment2 &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; new.env&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; parent.env&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; myEnvironment2 &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;environment&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; R_GlobalEnv&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; parent.env&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; myEnvironment2 &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; myEnvironment
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; parent.env&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; myEnvironment2 &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;environment&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x0000000006ce0920&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
attr&lt;span class=&quot;p&quot;&gt;(,&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;name&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;Cool Name&amp;quot;&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Here&amp;#39;s another way to understand the &amp;quot;current&amp;quot; or &amp;quot;local&amp;quot; environment&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# We create a function that calls environment() to query for the local environment. &lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# When R executes a function it automatically creates a new environment for that function.&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# This is useful - variables/objects created inside the function will live in the new local environment.&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# We call Test() to verify this.  We can see that Test() does NOT print R_GlobalEnv. &lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# We didn&amp;#39;t created any objects within Test().  If we had, they would live in the &amp;quot;0x0000000006ce9b58&amp;quot;&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# environment while Test() is running.  When the function completes executing, the environment dies.&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; Test &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; print&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; environment&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; 
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; environment&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;environment&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; R_GlobalEnv&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; Test&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; 
&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;environment&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x0000000006ce9b58&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# And why not...who&amp;#39;s the enclosing environment?&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# There&amp;#39;s more than meets the eye here.  We&amp;#39;ll go deeper in a bit...&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; Test &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; print&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; parent.env&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; environment&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; Test&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;environment&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; R_GlobalEnv&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h2 id=&quot;short-answer-how-r-searches-and-finds-stuff&quot;&gt;Short Answer: How R Searches and Finds stuff&lt;/h2&gt;
&lt;p&gt;Have you followed along with the code above?  It seems that environments are not just static repositories of objects.  When R executes an expression, there is always one &lt;em&gt;local&lt;/em&gt; or &lt;em&gt;current&lt;/em&gt; environment.  Maybe its easier to think of this as the &lt;em&gt;active&lt;/em&gt; environment, in contrast to the other environments which are &lt;em&gt;inactive&lt;/em&gt;.  Or perhaps its more intuitive to think of an expression executing &amp;ldquo;within&amp;rdquo; a particular environment.  The point here is that at any moment R can ask &amp;ldquo;hey, what&amp;rsquo;s the local environment.&amp;rdquo;  R asks this questions a lot.  In fact, it asks this question every time it needs to find a named object.   We saw that R creates a new local environments every time it runs a function.  So when we run any decently involved piece of code, functions call other function and environments spawn and die.&lt;/p&gt;

&lt;p&gt;Imagine we just freeze the system at any one expression.  When R goes searching for the names in that expression, it first looks at the objects within the local environment.  If the object is not found by name in that environment, then R searches the enclosing environment of the local environment.  If the object is not in the enclosure, then R searches the enclosure&amp;rsquo;s enclosure, and so on.  That&amp;rsquo;s how R searches and finds stuff; it traverses the enclosing environments and stops at the first environment that contains the named object.&lt;/p&gt;

&lt;p&gt;Satisfied?  I didn&amp;rsquo;t think so.  Lets roll&amp;hellip;&lt;/p&gt;

&lt;h2 id=&quot;map-of-the-world-follow-the-purple-line-road&quot;&gt;Map of the World (follow the purple line road)&lt;/h2&gt;
&lt;p&gt;We just said that R searches through the chain of enclosing environments to find named objects.  Its sort of like a treasure hunt that is limited to a single direction.  What we need for this treasure hunt is a &lt;strong&gt;map of the world&lt;/strong&gt;!
&lt;img src=&quot;/img/how-r-searches-and-finds-stuff/map-of-the-world.png&quot; alt=&quot;map of the world&quot; /&gt;&lt;/p&gt;

&lt;p&gt;This graphic shows the state of all environments when you first startup R.  Each box represents a unique environment.  The solid purple line represents the enclosing environment relationship.  I&amp;rsquo;ll explain the dotted purple line in a bit.  For now, consider it a relationship that&amp;rsquo;s similar to the enclosing environment.&lt;/p&gt;

&lt;h2 id=&quot;the-global-environment&quot;&gt;The Global Environment&lt;/h2&gt;
&lt;p&gt;I said that R_GlobalEnv is a special environment and you can see that it is colored green in the map.  Green means &lt;em&gt;start&lt;/em&gt;.  The global environment is precisely the environment that you start at when you launch R.  It is your &lt;em&gt;current&lt;/em&gt; or &lt;em&gt;local&lt;/em&gt; environment when R launches.  If you make an assignment at the prompt, the named object is stored in  R_GlobalEnv.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;r&quot;&gt;&lt;span class=&quot;c1&quot;&gt;# the ls() function shows us all objects defined in a given environment.&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# In this case we&amp;#39;re using the identifier .GlobalEnv to refer to the global environment&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# Here we can see that upon startup the global environment contains no objects&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# but after we assign myVariable, the global environment contains an object with that name&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; ls&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; envir &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; .GlobalEnv &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; 
character&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; myVariable &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt; 
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; ls&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; envir &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; .GlobalEnv &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;myVariable&amp;quot;&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# be careful with the environment() function.  It might seem wrong that this returns NULL&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# but if you read the documentation you&amp;#39;ll see that environment() takes a function as input.&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# myVariable is not a function, its a numeric.  The purpose of environment() is not to tell you&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# an object&amp;#39;s owner.  More to come...&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; environment&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; myVariable &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h2 id=&quot;the-search-list&quot;&gt;The Search List&lt;/h2&gt;
&lt;p&gt;In R, the &amp;ldquo;search list&amp;rdquo; is the chain of enclosing environments starting with R_GlobalEnv and ending with R_EmptyEnv.  I like to think of it as the main highway on our map.  This is the highway that R drives down when we start in R_GlobalEnv.  All roads in our world eventually lead to this highway.  You can obtain the search list by typing &lt;code&gt;search()&lt;/code&gt; at the prompt:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;r&quot;&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; search&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; 
&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;.GlobalEnv&amp;quot;&lt;/span&gt;        &lt;span class=&quot;s&quot;&gt;&amp;quot;package:stats&amp;quot;&lt;/span&gt;   &lt;span class=&quot;s&quot;&gt;&amp;quot;package:graphics&amp;quot;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;package:utils&amp;quot;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;package:datasets&amp;quot;&lt;/span&gt; 
&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;package:grDevices&amp;quot;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;package:methods&amp;quot;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;Autoloads&amp;quot;&lt;/span&gt;        &lt;span class=&quot;s&quot;&gt;&amp;quot;package:base&amp;quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h2 id=&quot;package-v-namespace-v-imports-environments&quot;&gt;Package v. Namespace v. Imports Environments&lt;/h2&gt;
&lt;p&gt;If you stare at our map above for enough time you might notice that every R package has 3 associated environments.  If you think that this is confusing then you are blessed with common sense.  This drove me crazy when I first encountered it.  Trust me for now that the only tricky part about this is the naming convention, otherwise this trifecta is useful and well-designed.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/how-r-searches-and-finds-stuff/package-namespace-imports.png&quot; alt=&quot;package namespace imports&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Here&amp;rsquo;s a breakdown, left to right:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;package environment&lt;/strong&gt; &lt;br /&gt;
This is where a package&amp;rsquo;s &lt;em&gt;exported&lt;/em&gt; objects go.  Simply put, these are the objects that the package author wants you to see.  These are most likely functions.  Typically a package is published that provides useful functions related to some topic or domain.  In traditional Object Oriented Programming (OOP), this is analogous  to a &amp;ldquo;public&amp;rdquo; class or method.  If that means nothing to do you then ignore it.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;namespace environment&lt;/strong&gt; &lt;br /&gt;
This is where &lt;em&gt;all&lt;/em&gt; objects in a package go.  This includes objects the package author wants you see.  It also includes objects that are not meant to be accessed by the end-user.  The latter, the &amp;ldquo;hidden&amp;rdquo; objects (they are not really hidden, you can access them if you&amp;rsquo;d like) facilitate the &amp;ldquo;visible&amp;rdquo; ones.  For example, a function &lt;code&gt;HardCalculation()&lt;/code&gt; might offload some complicated text formatting tasks to function &lt;code&gt;MakeResultsPretty()&lt;/code&gt;.  The author doesn&amp;rsquo;t want you to call &lt;code&gt;MakeResultsPretty()&lt;/code&gt;, its sole purpose is to format the results that are idiosyncratic to &lt;code&gt;HardCalculation()&lt;/code&gt;.  In OOP this is analogous to a &amp;ldquo;private&amp;rdquo; or &amp;ldquo;internal&amp;rdquo; class or method.&lt;/p&gt;

    &lt;p&gt;You might be thinking &amp;ldquo;wait, so objects the author wants me to see are in BOTH the package environment and the namespace environment?&amp;rdquo;  Yes and No.  Yes, both environments have a frame that lists objects of the same name, but no there is not two copies.  Both environments have &lt;a href=&quot;http://stackoverflow.com/questions/9278715/value-reference-equality-for-same-named-function-in-package-namespace-environmen&quot;&gt;pointers to the same function&lt;/a&gt;.  If that makes no sense to you then think of it as two copies - it honestly doesn&amp;rsquo;t matter.  This may seem like an odd arrangement (two pointers, two copies - your pick) but its use will become apparent shortly.  This is also why there is no easy way to query an object for the environment that owns it.  Its possible that two or more environments own the same object.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;imports environment&lt;/strong&gt; &lt;br /&gt;
This environment contains objects from other packages that are explicitly stated requirements for a package to work properly.  Most packages published on CRAN are not islands; they build on functionality provided in other packages.  Take &lt;a href=&quot;http://cran.r-project.org/web/packages/ggplot2/index.html&quot;&gt;ggplot2&lt;/a&gt; for example.  You can see on the CRAN page in the &amp;ldquo;Imports&amp;rdquo; section that it requires &lt;code&gt;plyr&lt;/code&gt; among other packages.  I suggest using the screenshot below since the package could change in a way that breaks my example.  The &lt;code&gt;imports:ggplot2&lt;/code&gt; environment contains all objects in the &lt;code&gt;plyr&lt;/code&gt; package.
&lt;img src=&quot;/img/how-r-searches-and-finds-stuff/ggplot2.png&quot; alt=&quot;ggplot2 cran&quot; /&gt;&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;imports-v-depends&quot;&gt;Imports v Depends&lt;/h2&gt;
&lt;p&gt;You might have been confused seeing a &lt;code&gt;Depends&lt;/code&gt; and an &lt;code&gt;Imports&lt;/code&gt; section.  If &lt;code&gt;Imports&lt;/code&gt; states a package&amp;rsquo;s requirements, then what does &lt;code&gt;Depends&lt;/code&gt; do?  This is a poor naming convention.  The &lt;code&gt;Depends&lt;/code&gt; section &lt;em&gt;also&lt;/em&gt; lists packages that ggplot2 requires.  The difference between &lt;code&gt;Imports&lt;/code&gt; and &lt;code&gt;Depends&lt;/code&gt; is where the requirement is placed on our map of the world.  Because our map specifies the path R takes to find objects, there are consequences to specifying a requirement in &lt;code&gt;Imports&lt;/code&gt; versus &lt;code&gt;Depends&lt;/code&gt; in terms of how R finds the dependency.&lt;/p&gt;

&lt;p&gt;If the package is specified in &lt;code&gt;Imports&lt;/code&gt;, then the package contents will go into the &amp;ldquo;imports&amp;rdquo; environment.  In the case of ggplot2, the objects in the &lt;code&gt;plyr&lt;/code&gt; package will appear in the &lt;code&gt;imports:ggplot2&lt;/code&gt; environment.  Notice also that &lt;code&gt;plyr&lt;/code&gt; does not have a package environment.  Its nicely tucked away inside the environment &lt;code&gt;imports:ggplot2&lt;/code&gt;.  The dotted purple line will be explained later.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/how-r-searches-and-finds-stuff/imports-depends-plyr.png&quot; alt=&quot;imports depends plyr&quot; /&gt;&lt;/p&gt;

&lt;p&gt;If a package is specified in &lt;code&gt;Depends&lt;/code&gt; (i.e. &lt;code&gt;reshape&lt;/code&gt; package), then the package is loaded as it would be if you called &lt;code&gt;library()&lt;/code&gt; or &lt;code&gt;require()&lt;/code&gt; from the R prompt.  That is, the package, namespace, and imports environments are created for the dependency and placed on our map.  The reshape package is attached &lt;em&gt;before&lt;/em&gt; &lt;code&gt;ggplot2&lt;/code&gt; and the &lt;code&gt;package:reshape&lt;/code&gt; environment becomes  &lt;code&gt;package:ggplot2&lt;/code&gt;&amp;rsquo;s enclosing environment.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/how-r-searches-and-finds-stuff/imports-depends-reshape.png&quot; alt=&quot;imports depends reshape&quot; /&gt;&lt;/p&gt;

&lt;p&gt;So who cares?  Is the choice between Depends and Imports arbitrary?  Its not.  The &lt;code&gt;library()&lt;/code&gt; command (or generally attaching a library) places the package environment under &lt;code&gt;R_Global&lt;/code&gt;.  More precisely, the package environment becomes &lt;code&gt;R_Global&lt;/code&gt;&amp;rsquo;s enclosing environment.  &lt;code&gt;R_Global&lt;/code&gt;&amp;rsquo;s old enclosure now encloses the package environment.  You can see this in the diagram below where we have loaded the package &lt;a href=&quot;http://cran.r-project.org/web/packages/reshape2/index.html&quot;&gt;reshape2&lt;/a&gt; which is a re-write/upgrade of the original &lt;code&gt;reshape&lt;/code&gt; package.&lt;/p&gt;

&lt;p&gt;Both &lt;code&gt;reshape&lt;/code&gt; and &lt;code&gt;reshape2&lt;/code&gt; contain the function &lt;code&gt;cast&lt;/code&gt;.  Lets say (I&amp;rsquo;m making this up) that &lt;code&gt;ggplot2&lt;/code&gt; has a function called &lt;code&gt;FunctionThatCallsCast()&lt;/code&gt;.  As you can guess, this function calls the &lt;code&gt;cast()&lt;/code&gt; function.  Without knowing any details of how R finds stuff, lets just follow the &amp;ldquo;purple line road&amp;rdquo;.  We travel from to 1 and 2 and find &lt;code&gt;FunctionThatCallsCast()&lt;/code&gt;.  Remember, the package and namespace environments both reference a package&amp;rsquo;s public-facing functions.  We execute that function and now we need to find &lt;code&gt;cast&lt;/code&gt;.  We travel from 3 to 5 searching for &lt;code&gt;cast&lt;/code&gt;.  We find &lt;code&gt;cast&lt;/code&gt; at 6 and stop.  But this is the &lt;em&gt;wrong&lt;/em&gt; &lt;code&gt;cast&lt;/code&gt;.  This is &lt;code&gt;cast&lt;/code&gt; in package &lt;code&gt;reshape2&lt;/code&gt;, but &lt;code&gt;ggplot&lt;/code&gt; depends on the &lt;code&gt;cast&lt;/code&gt; in &lt;code&gt;reshape&lt;/code&gt;.  This could have dire consequences depending on the differences between &lt;code&gt;cast&lt;/code&gt; in &lt;code&gt;reshape&lt;/code&gt; and &lt;code&gt;reshape2&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/how-r-searches-and-finds-stuff/reshape-reshape2-cast-conflict.png&quot; alt=&quot;reshape reshape2 cast conflict&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The better solution would have been to stuff &lt;code&gt;reshape&lt;/code&gt;&amp;rsquo;s &lt;code&gt;cast()&lt;/code&gt; function into &lt;code&gt;imports:ggplot2&lt;/code&gt; using the &lt;code&gt;Imports&lt;/code&gt; feature.  In that case, we would have travelled from 2 to 3 and stopped.  Now you can see why the choice between &lt;code&gt;Imports&lt;/code&gt; and &lt;code&gt;Depends&lt;/code&gt; is not arbitrary.  With so many packages on CRAN and so many of us working in related disciplines its no surprise that same-named functions appear in multiple packages.  &lt;code&gt;Depends&lt;/code&gt; is less safe.  &lt;code&gt;Depends&lt;/code&gt; makes a package vulnerable to whatever other packages are loaded by the user.&lt;/p&gt;

&lt;h2 id=&quot;namespacebase&quot;&gt;namespace:base&lt;/h2&gt;
&lt;p&gt;We haven&amp;rsquo;t mentioned the fact that all &lt;code&gt;imports:&amp;lt;name&amp;gt;&lt;/code&gt; environments have &lt;code&gt;namespace:base&lt;/code&gt; as their enclosure.  Think of this a freebie for creating a package.  Since the base functions are used frequently, they are most likely a dependency for any package (or a package&amp;rsquo;s imports).  Without &lt;code&gt;namespace:base&lt;/code&gt; where it is, R would have to go hunting quite far to find &lt;code&gt;package:base&lt;/code&gt;.  There&amp;rsquo;s a big risk that another package has a function of the same name as a base function.  A package author cannot know a-prior when you intend to attach her package nor that you have decided to write your own version of a base function.  So do as you like, a package author can expect that R will find the base functions immediately after &lt;code&gt;Imports&lt;/code&gt;.  There&amp;rsquo;s no chance of corruption.&lt;/p&gt;

&lt;h2 id=&quot;the-curveball-the-dotted-purple-lines&quot;&gt;The Curveball (the dotted purple lines)&lt;/h2&gt;
&lt;p&gt;Functions, like all objects, are housed inside environments.  However, functions themselves have a property which is a pointer to the environment in which they should run.  When you create a function, that property is automatically set to the environment in which the function was created.  So the environment that houses a function and the environment that the function will run in is one and the same.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/how-r-searches-and-finds-stuff/environment-property.png&quot; alt=&quot;environment property&quot; /&gt;&lt;/p&gt;

&lt;p&gt;What do we mean by &amp;ldquo;the environment that a function will run in?&amp;rdquo;  We said earlier that executing a function creates a new environment specifically for that function.  We also said that all environments have an enclosing environment.  So what environment is the enclosure of the function&amp;rsquo;s new environment?  This is what is specified by the function&amp;rsquo;s environment property.  This is &amp;ldquo;the environment that a function will run in.&amp;rdquo;  Its not necessarily the environment that owns the function.  It is controlled by the function&amp;rsquo;s environment property.&lt;/p&gt;

&lt;p&gt;We can get a function object&amp;rsquo;s environment property using the &lt;code&gt;environment()&lt;/code&gt; function.  For example:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;r&quot;&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; MyFunction &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{}&lt;/span&gt; 
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; environment&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; MyFunction &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; 
&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;environment&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; R_GlobalEnv&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;And when we run &lt;code&gt;MyFunction()&lt;/code&gt; and R is executing lines of codes inside that function, the environments look like this:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/how-r-searches-and-finds-stuff/environment-execution.png&quot; alt=&quot;environment execution&quot; /&gt;&lt;/p&gt;

&lt;p&gt;By default, R sets a function&amp;rsquo;s environment property equal to the environment where the function was created (the environment that owns the function).  However, its not necessary that a function&amp;rsquo;s executing environment and the environment that owns the function are one and the same.  In fact, we can change the environment to our liking:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;r&quot;&gt;&lt;span class=&quot;c1&quot;&gt;# notice how environment(MyFunction) no longer returns R_GlobalEnv&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; MyFunction &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; 
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; newEnvironment &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; new.env&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; 
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; environment&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; MyFunction &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; newEnvironment 
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; environment&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; MyFunction &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; 
&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;environment&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x000000000e895628&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Another way to see a function&amp;#39;s environment property is to just print &lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# the function. The environment will appear at the bottom of the printed function &lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; MyFunction
&lt;span class=&quot;kr&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; 
&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;environment&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x000000000e895628&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Here we do the same for the standard deviation function&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; environment&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; sd &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; 
&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;environment&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; namespace&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;stats&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; 
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; sd 
&lt;span class=&quot;kr&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; na.rm &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;FALSE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; 
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; 
  &lt;span class=&quot;kc&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;removed &lt;span class=&quot;kr&quot;&gt;for&lt;/span&gt; brevity&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; 
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; 
&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;bytecode&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;000000000E7&lt;/span&gt;F2EA0&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;environment&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; namespace&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;stats&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Can you figure out what&amp;#39;s going on here? &lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# When run, FromLocal&amp;#39;s enclosing environment is the MyFunction environment.  That&amp;#39;s where &lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# FromLocal was created and that&amp;#39;s what R does by default.&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# When R searches for the object &amp;quot;age&amp;quot; within FromLocal it looks to the MyFunction environment,&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# picks-up age = 22 and adds 1 to that. &lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# -&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# When run, FromGlobal&amp;#39;s enclosing environment is R_GlobalEnv because &lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# we assigned the function&amp;#39;s environment property to R_GlobalEnv.&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# When R searches for &amp;quot;age&amp;quot; within FromGlobal, it looks at the enclosing environment which is &lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# The Global environment, and picks up age = 32 and adds 1 to that.  &lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# -&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# The environment of NoSearch() already has &lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# the age object and does not need to search its enclosing environment(s).&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; age &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;32&lt;/span&gt; 
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; MyFunction &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; 
&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; 
&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;     age &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;22&lt;/span&gt; 
&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;     FromLocal &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; print&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; age &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; 
&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;     FromGlobal &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; print&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; age &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; 
&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;     NoSearch &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;  &lt;span class=&quot;kr&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; age &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;11&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; print&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; age &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; 
&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;     environment&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; FromGlobal &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; .GlobalEnv 
&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;     FromLocal&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; 
&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;     FromGlobal&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; 
&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;     NoSearch&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; 
&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; 
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; MyFunction&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; 
&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;23&lt;/span&gt; 
&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;33&lt;/span&gt; 
&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;12&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This explains the dotted purple lines in our map.  If you inspect the environment property of the functions within the &lt;code&gt;package:&amp;lt;name&amp;gt;&lt;/code&gt; environments you&amp;rsquo;ll see that they all point to the &lt;code&gt;namespace:&amp;lt;name&amp;gt; environment&lt;/code&gt;.  Check it out:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;r&quot;&gt;&lt;span class=&quot;c1&quot;&gt;# get the standard deviation function within package:stats and&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# inspect the function&amp;#39;s environment property.&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# Notice that it points to the namespace:stats environment&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; statsPackageEnv &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; as.environment&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;package:stats&amp;quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; 
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; sdFunc &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; get&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;sd&amp;quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; envir &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; statsPackageEnv &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; 
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; environment&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; sdFunc &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; 
&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;environment&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; namespace&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;stats&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; 
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; statsNamespaceEnv &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; environment&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; sdFunc &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; 
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; sdFunc2 &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; get&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;sd&amp;quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; envir &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; statsNamespaceEnv &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; 
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; environment&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; sdFunc2 &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; 
&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;environment&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; namespace&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;stats&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# An easier way to get a namespace environment&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; statsNamespaceEnv &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; asNamespace&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;stats&amp;quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; 
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; statsNamespaceEnv 
&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;environment&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; namespace&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;stats&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;So in essence, the package environment is just a pass-thru to the namespace environment.  The package environment says &amp;ldquo;I don&amp;rsquo;t know what to do, ask my functions&amp;rdquo;.  And when we ask the functions they all say  &amp;ldquo;when you execute us create a new environment whose enclosure is the namespace environment.&amp;rdquo;  More precisely, the functions are just offering up their environment property .  We might as well make those dotted lines solid:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/how-r-searches-and-finds-stuff/map-of-the-world-complete.png&quot; alt=&quot;map of the world complete&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Incidentally, this is another explanation for why there&amp;rsquo;s no easy way to query an object for the environment that owns it.  When we are executing in an environment, we are interested in the objects it owns because we might be looking for one of them.  When we find a function we need to know which environment to execute it within.  But its not important in our workflow to identify an arbitrary object&amp;rsquo;s owning environment.&lt;/p&gt;

&lt;p&gt;If your head is spinning then I &lt;strong&gt;encourage you to pause and re-read this entire section&lt;/strong&gt;.  Function execution is the most complex piece of the puzzle.&lt;/p&gt;

&lt;h2 id=&quot;passing-functions&quot;&gt;Passing Functions&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Feel free to skip this section&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Its because functions have an environment property that they can be passed around.  Passing a function to another function is a mind-boggling (albeit powerful) feature.  I&amp;rsquo;m not going to explore this too much.  At a high level you can think of it as follows.  If a function &lt;code&gt;FunctionA( someOtherFunction )&lt;/code&gt; takes another function &lt;code&gt;someOtherFunction&lt;/code&gt; as a parameter then &lt;code&gt;FunctionA&lt;/code&gt;  must have some variability in the way it runs.  That variability is governed by the implementation of &lt;code&gt;someOtherFunction&lt;/code&gt;.  When we construct &lt;code&gt;someOtherFunction&lt;/code&gt;, we expect it to run in a particular way.  &lt;code&gt;someOtherFunction&lt;/code&gt; should have access to the objects in the environment in which it was constructed.  That expectation doesn&amp;rsquo;t change when the function is handed-off to &lt;code&gt;FunctionA&lt;/code&gt; .  But R creates a new environment for &lt;code&gt;FunctionA&lt;/code&gt;.  Thankfully that&amp;rsquo;s not a problem.  When &lt;code&gt;someOtherFunction&lt;/code&gt; is finally run R looks to the function&amp;rsquo;s environment property and executes within &lt;em&gt;that&lt;/em&gt; environment, not within &lt;code&gt;FunctionA&lt;/code&gt;&amp;rsquo;s environment.  So the integrity of our expectation is upheld.  In fact, &lt;code&gt;FunctionA&lt;/code&gt; can pass &lt;code&gt;someOtherFunction&lt;/code&gt; to &lt;code&gt;FunctionB&lt;/code&gt; which in turn can pass the function to &lt;code&gt;FunctionC&lt;/code&gt; and it has no consequence on how &lt;code&gt;someOtherFunction&lt;/code&gt; will run.   That&amp;rsquo;s the magic of a function&amp;rsquo;s environment property.&lt;/p&gt;

&lt;h2 id=&quot;that-creepy-caller&quot;&gt;That Creepy Caller&lt;/h2&gt;
&lt;p&gt;The search mechanism &lt;strong&gt;does not use the call stack&lt;/strong&gt;.  The call stack is the sequence of function calls that has gotten you to wherever you currently are in the calculation.  For example, &lt;code&gt;FunctionA&lt;/code&gt; calls &lt;code&gt;FunctionB&lt;/code&gt; which in turn calls &lt;code&gt;FunctionC&lt;/code&gt;.   The call stack just places each of those functions on top of one another in the order in which they were called.  Lets say &lt;code&gt;FunctionC&lt;/code&gt; needs to execute &lt;code&gt;FunctionD&lt;/code&gt;.  The &lt;em&gt;wrong&lt;/em&gt; way to think about the search mechanism is to follow the callers.  That is, if &lt;code&gt;FunctionD&lt;/code&gt; is not defined in &lt;code&gt;FunctionC&lt;/code&gt;&amp;rsquo;s executing environment, then look at &lt;code&gt;FunctionB&lt;/code&gt;&amp;rsquo;s executing environment and if not found there then look at &lt;code&gt;FunctionA&lt;/code&gt;&amp;rsquo;s executing environment.  The &lt;em&gt;right&lt;/em&gt; way to think about the search mechanism is to ask &amp;ldquo;who owns Function C?&amp;rdquo;  If the owner knows nothing about &lt;code&gt;FunctionD&lt;/code&gt;, then maybe the owner&amp;rsquo;s owner does, and so on.&lt;/p&gt;

&lt;p&gt;Unfortunately, the call stack is more intuitive than the chain of enclosing environments.  Just remember, whenever R is evaluating a statement the system is simultaneously at the top (or bottom if it&amp;rsquo;s easier to visualize that way) of &lt;em&gt;two&lt;/em&gt; important chains of environments.  One is the chain of enclosing environments which is involved in the task of scoping (i.e. where to look next for variable names not found in the frame of the current environment).  This is the chain we care about.  The other chain is the call stack, which is produced by the sequence of function calls.  You can ignore this chain.  There &lt;em&gt;are&lt;/em&gt; scenarios where it&amp;rsquo;s necessary to look for a variable via the call stack, but to accomplish that you have to use some special functions in R.  Those scenarios are beyond the scope of this article.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A word of caution:&lt;/strong&gt; R (and some R literature) uses the term &amp;ldquo;parent&amp;rdquo; in context of both chains.  There&amp;rsquo;s the function &lt;code&gt;parent.env()&lt;/code&gt; which we already know and &lt;code&gt;parent.frame()&lt;/code&gt; which is used to interrogate the call stack.  This is certainly confusing and its a historic slip-up.  The term &amp;ldquo;parent&amp;rdquo; should &lt;em&gt;not&lt;/em&gt; be used as a substitute for enclosing environments.  It should only be used with the call stack.&lt;/p&gt;

&lt;h2 id=&quot;finally-how-r-searches-and-finds-stuff&quot;&gt;Finally, How R searches and finds stuff&lt;/h2&gt;
&lt;p&gt;So, finally, how does R search and find stuff?  &lt;strong&gt;R just follows the purple line road&lt;/strong&gt; in our map above.  Lets follow along with an example&lt;/p&gt;

&lt;p&gt;Lets say we&amp;rsquo;re looking for function &lt;code&gt;ggplot&lt;/code&gt;.  We start at &lt;code&gt;R_GlobalEnv&lt;/code&gt;.  If &lt;code&gt;ggplot&lt;/code&gt; is not in the global environment, then  it must be in a package.  So R travels down the search list looking for &lt;code&gt;ggplot&lt;/code&gt;. This is simply the chain of enclosing environments starting with &lt;code&gt;R_Global&lt;/code&gt;.  R ultimately find the function  in one of the package environments.  Although &lt;code&gt;ggplot&lt;/code&gt; is found within the &lt;code&gt;package&lt;/code&gt; environment, R executes &lt;code&gt;ggplot&lt;/code&gt; within the &lt;code&gt;namespace&lt;/code&gt; environment as described in the prior section.  In this case, we&amp;rsquo;ve found &lt;code&gt;ggplot&lt;/code&gt; in &lt;code&gt;package:ggplot2&lt;/code&gt; and we execute the function within &lt;code&gt;namespace:ggplot2&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Lets say &lt;code&gt;ggplot&lt;/code&gt; calls another function &lt;code&gt;MyFunction&lt;/code&gt;.  A few things can happen:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;If &lt;code&gt;MyFunction&lt;/code&gt; is defined within &lt;code&gt;ggplot&lt;/code&gt;, then we find it immediately since R checks the local environment first.  In this case the local environment is the environment created to run &lt;code&gt;ggplot&lt;/code&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;If not found, then R looks to the enclosing environment of &lt;code&gt;MyFunction&lt;/code&gt;&amp;rsquo;s executing environment which is &lt;code&gt;namespace:ggplot2&lt;/code&gt;.  If we find &lt;code&gt;MyFunction&lt;/code&gt; here, then it&amp;rsquo;s a case of a package function calling another function in the same package.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;If &lt;code&gt;MyFunction&lt;/code&gt; is not in the &lt;code&gt;namespace:ggplot2&lt;/code&gt;, then R checks the enclosing environment of the namespace environment which is the imports environment.  This gives &lt;code&gt;ggplot&lt;/code&gt; an opportunity to find &lt;code&gt;MyFunction&lt;/code&gt; within a set of explicitly defined package dependencies.  This is like ggplot finding a plyr function in our example above.  &lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;If &lt;code&gt;MyFunction&lt;/code&gt; is not in the imports environment, then we check the enclosing environment of the imports environment which is &lt;code&gt;namespace:base&lt;/code&gt;.  A base function (i.e. sd() for standard deviation) would be found here and the search would be complete.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;If &lt;code&gt;MyFunction&lt;/code&gt; is not found in &lt;code&gt;namespace:base&lt;/code&gt;, then we are back to the search list.  We start by checking &lt;code&gt;R_GlobalEnv&lt;/code&gt;.  Its unlikely that &lt;code&gt;MyFunction&lt;/code&gt; is in &lt;code&gt;R_GlobalEnv&lt;/code&gt;.  It would be poor practice for a package to expect the user to define some function in the global environment.  However, the user could take this as an opportunity to intercept the search by defining her own version of &lt;code&gt;MyFunction&lt;/code&gt; in the global environment.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;If &lt;code&gt;MyFunction&lt;/code&gt; is within a package that&amp;rsquo;s a dependency of &lt;code&gt;ggplot2&lt;/code&gt; and that dependency is specified in&lt;code&gt; Depends&lt;/code&gt; rather than &lt;code&gt;Imports&lt;/code&gt;, then the search list is where we would find &lt;code&gt;MyFunction&lt;/code&gt;.  This is like &lt;code&gt;ggplot&lt;/code&gt; looking for a function in the &lt;code&gt;reshape&lt;/code&gt; package in our example above.  We would hope that no other package has defined the same function and is attached closer to the global environment (as in our &lt;code&gt;reshape2&lt;/code&gt; example above)&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;All-in-all  you just have to determine what the &amp;ldquo;current&amp;rdquo; or &amp;ldquo;local&amp;rdquo; environment is and following the enclosing environments (the purple arrows) until you find the object you are looking for.  Rinse and repeat.&lt;/p&gt;

&lt;h2 id=&quot;qualitative-comments&quot;&gt;Qualitative Comments&lt;/h2&gt;
&lt;p&gt;I believe that the search and find mechanism is an adequate design given that R is an interpreted, weakly typed language that supports attaching multiple packages at-will.  If we are executing outside of a package (as in &lt;code&gt;R_GlobalEnv&lt;/code&gt;) it enables us to find functions inside packages.  If we are inside a package it allows the package functions to find the specified dependencies.  If we are inside a package or a package&amp;rsquo;s imports (dependencies), then we have a buffer of base functions before we plunging into the search list.   Also, the design ensures that we terminate at &lt;code&gt;R_EmptyEnv&lt;/code&gt; if a named object cannot be found, no matter where on the map we are.&lt;/p&gt;

&lt;p&gt;All of that said, &lt;strong&gt;its still complicated&lt;/strong&gt;.  When I&amp;rsquo;m debugging a search-and-find issue it takes a lot of brainpower to figure out what&amp;rsquo;s going on.  Don&amp;rsquo;t beat yourself up if the same is happening to you.&lt;/p&gt;

&lt;h2 id=&quot;skip-the-search-and-find&quot;&gt;Skip the search-and-find&lt;/h2&gt;
&lt;p&gt;If you know exactly which package contains the object desired then you can reference it directly using the &lt;code&gt;::&lt;/code&gt; operator.  Simply place the package name before the operator and the name of the object after the operator to retrieve it.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;r&quot;&gt;&lt;span class=&quot;c1&quot;&gt;# use :: to get sd&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; stats&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;sd 
&lt;span class=&quot;kr&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;x&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; na.rm &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;FALSE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; 
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; 
    &lt;span class=&quot;kc&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; omitted &lt;span class=&quot;kr&quot;&gt;for&lt;/span&gt; brevity &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; 
&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;bytecode&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;000000000511&lt;/span&gt;D608&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; 
&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;environment&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; namespace&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;stats&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;If the object is not exported or you are unsure, then you can use the &lt;code&gt;:::&lt;/code&gt; operator (notice the extra colon).&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;r&quot;&gt;&lt;span class=&quot;c1&quot;&gt;# use ::: to get Wilks&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; Wilks 
Error&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; object &lt;span class=&quot;s&quot;&gt;&amp;#39;Wilks&amp;#39;&lt;/span&gt; not found 
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; stats&lt;span class=&quot;o&quot;&gt;:::&lt;/span&gt;Wilks 
&lt;span class=&quot;kr&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;eig&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; q&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; df.res&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; 
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; 
    &lt;span class=&quot;kc&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; omitted &lt;span class=&quot;kr&quot;&gt;for&lt;/span&gt; brevity &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; 
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; 
&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;bytecode&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;00000000050&lt;/span&gt;FE280&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; 
&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;environment&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; namespace&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;stats&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This operator searches the namespace environment for the given object (as we discussed, non-exported objects do not appear in the package environment, only in the namespace environment).  You can validate that by looking at the definition of &lt;code&gt;:::&lt;/code&gt;  (remember to include the backticks).&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;r&quot;&gt;&lt;span class=&quot;c1&quot;&gt;# view the ::: operator function&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;sb&quot;&gt;`:::`&lt;/span&gt; 
&lt;span class=&quot;kr&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;pkg&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; name&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; 
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; 
    pkg &lt;span class=&quot;o&quot;&gt;&amp;lt;-&lt;/span&gt; as.character&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;substitute&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;pkg&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; 
    name &lt;span class=&quot;o&quot;&gt;&amp;lt;-&lt;/span&gt; as.character&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;substitute&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;name&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; 
    get&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;name&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; envir &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; asNamespace&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;pkg&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; inherits &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;FALSE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; 
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; 
&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;bytecode&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;00000000073&lt;/span&gt;BAEA8&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;environment&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; namespace&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;base&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h2 id=&quot;thanks&quot;&gt;Thanks&lt;/h2&gt;
&lt;p&gt;I&amp;rsquo;d like to thank &lt;a href=&quot;http://stackexchange.com/users/955440/josh-obrien&quot;&gt;Josh O&amp;rsquo;Brien&lt;/a&gt; who reviewed a draft version of this post and provided solid feedback.  His comments and challenges directly improved the quality of this article.  In some cases I lifted text verbatim from his emails (with his permission of course).  I am grateful to him for being so generous with his time.  I&amp;rsquo;d also like to thank the R community on &lt;a href=&quot;http://stackoverflow.com/questions/tagged/r&quot;&gt;StackOverflow&lt;/a&gt; for being patient with numerous questions that I&amp;rsquo;ve posted about topics herein discussed.  That community continues to be the absolute best way to get answers about R.  Finally, I thank John Chambers for writing the R programmer&amp;rsquo;s must-have book &lt;a href=&quot;http://www.amazon.com/Software-Data-Analysis-Programming-Statistics/dp/0387759352&quot;&gt;Software for Data Analysis&lt;/a&gt;.&lt;/p&gt;

</content>
   <rights>&#169; 2012 Suraj Gupta</rights>
 </entry>
 
 
</feed>
