The best thing I ever did with R

1 minute read

Published:

A few years back, I created an RStudio shortcut that has ended up being my most-used function in R, probably by far. What does the shortcut do? It wraps the current line in the RStudio editor (or the console, if the cursor is there) in a View() function, which opens the data.frame in RStudio’s built-in data viewer.

What’s so special about that? Aren’t there some shortcuts that already do that? There are, but they’re not as good. Here is why:

There are two shortcuts (that I know of) to view data in RStudio - F2 and Ctrl + left-click. These do work when you have a data.frame/data.table/tibble as environment variable in your editor. But, they only work on an un-altered version of the object. As someone who does a lot of data wrangling and exploratory analysis and data cleaning, I like to go through my data.tables in the Viewer while I am running queries on them. This won’t work with F2 or Ctrl + left-click:

What you seeIs not what you get
what you seewhat you get

For the built-in RStudio shortcuts, they just grab iris and open it in the Viewer, ignoring the rest of the query (note the presence of setosa in the right panel where the query filters for virginica).

My shortcut, on the other hand, wraps the entire line in View(), using RStudio’s API functions, and returns the whole dang query in the Viewer. As someone who does most of their work with data.table, this is extremely helpful for the long transformations that don’t wrap lines:

What you seeIs what you get!
what you seewhat you get

You can see the query that is run in the console at bottom.

Try it yourself

And now, this shortcut is available in my own R package, viewDat, available on Github: github.com/t-morrison/viewDat

I have the shortcut bound to Ctrl + ,. It was not otherwise occupied and easy to reach. The shortcut won’t currently work on piped queries wrapping multiple lines, but could probably be extended with some extra effort (I don’t use pipes much, so haven’t gotten to this). Give it a try!

Check it out in-action: