In R with ggplot2, these two lines seem to do the same thing:
qplot(data=diamonds, carat, price)
qplot(data=diamonds, get("carat"), get("price"))
but I don’t understand how they work…
How does R understand what carat refers to in the first case and what get("carat") refers to in the second? If I just try to access carat or get("carat") on their own I get (unsurprisingly)
> carat
Error: object 'carat' not found
> get("carat")
Error in get("carat") : object 'carat' not found
Under the hood, how does ggplot2 set up the bindings such that these elegant calling semantics “just work” and more specifically, how would I go about implementing this sort thing myself? I’ve tried to read the source but am having trouble understanding it.
Hadley, the creator of ggplot2, has a wonderful entry on the devtools wiki about building your own version of the
subsetfunction–which takes a data argument as well. I learned a whole lot reading it, and I’d be willing to bet the mechanics built up in the article are very similar to whatggplotdoes.https://github.com/hadley/devtools/wiki/Evaluation