One of My Favorite R Oddities: dump()

January 26, 2015
By

(This article was first published on librestats » R, and kindly contributed to R-bloggers)

R is full of things that make “real programmers” (I dislike this term) turn their noses up in disgust. One of my favorites is the dump() function. It is…odd. I think the best way to introduce it to people is without context, because it’s just so bizarre:

 
x <- runif(5)

x
# [1] 0.7169493 0.1615495 0.7741029 0.4234200 0.8732784

dump("x", stdout())
# x <-
# c(0.716949315741658, 0.161549518117681, 0.774102924391627, 0.423419966362417, 
# 0.873278449522331)

y <- matrix(1:30, 10)
dump("y", stdout())
# y <-
# structure(1:30, .Dim = c(10L, 3L))

That’s right; it’s actually dumping out R code that would allow you to generate the object. I have actually even found some intersting uses for this in my time developing R packages. But they are highly unusual, and generally I would recommend you rethink your strategy if you think this looks like a solution to your problems. Still, it’s pretty cool to have!

But this allows for another amusing oddity with R: you can modify R objects with a text editor inside the R terminal itself. Here’s a simple example:

x <- 1:5
vi(x)

Entering this into an R terminal will greet you with a vim editor containing the text:

1:5

Now if I change the 5 to a 6 and save/exit, back in R I see:

vi(x)
# [1] 1 2 3 4 5 6

There are also emacs() and xemacs() functions if you’re some kind of filthy emacs plebian. I mean, they probably work. I wouldn’t know:

$ apt-cache policy emacs
emacs:
  Installed: (none)
...

Now these editor commands (vi(), emacs(), …) sort of work in RStudio, but not really (you just get a plain editor window). So you will have to use your terminal to really enjoy this one.

Speaking of RStudio, did you know that it has a vim mode? It’s far from a full vim session, but it’s pretty neat. You can enable it by selecting

  • Tools
  • Global Options
  • Code Editing
  • Enable vim editing mode:

rstudio_vim

Of course, there’s a nice R plugin for vim itself.

To leave a comment for the author, please follow the link and comment on their blog: librestats » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)