(Automatically show output)

A neat trick with parentheses to print the contents of an object you just created, without running another line of code.

Kaija Gahm true
07-22-2018

This post has been slightly modified from its original form on woodpeckR.

Problem

It’s annoying to have to type the name of an object I just created in order to print its output.

Context

A certain lightsaber-wielding stats professor of mine liked to point out that R doesn’t go out of its way to be helpful. If you write a line of code that creates an object and then run that line of code, there’s no message to tell you that the object has been successfully created. R doesn’t say “Task complete! What’s next?” or otherwise give you any indication that anything has happened. To actually view the object you just created, you have to type its name or run some other command on it.

Once in a while, this lack of transparency can be frustrating. What if I want to save objects and also view them in real time as they are created? Say I’ve used the handy prop.table function to transform a frequency table into a proportion table. I’d like to be able to view prop, prop.1 and prop.2 without typing their names and adding extra lines of code.

Solution

The same lightsaber-wielding stats professor who wished R would be a little more communicative taught me a trick to do just this: encase a command in parentheses to automatically print its output when it runs. Hence,

# Load data from GitHub
library(dplyr)
polygon <- read.csv("https://tinyurl.com/rta6hkbo")

(prop <- with(polygon, table(revetment, pool)) 
 %>% prop.table())
         pool
revetment          4          8         13
        0 0.13472486 0.37760911 0.19544592
        1 0.10815939 0.10056926 0.08349146

…returns the same thing as leaving out the parentheses and typing the name of the object, prop, on a new line:

prop <- with(polygon, table(revetment, pool)) %>%
  prop.table()

prop
         pool
revetment          4          8         13
        0 0.13472486 0.37760911 0.19544592
        1 0.10815939 0.10056926 0.08349146

Also note that this is different (better) than just running the command without the assignment arrow, like this:

with(polygon, table(revetment, pool)) %>% 
  prop.table()
         pool
revetment          4          8         13
        0 0.13472486 0.37760911 0.19544592
        1 0.10815939 0.10056926 0.08349146

…because the above doesn’t save the table you created, it just shows it to you once.

Outcome

Create objects and view them at the same time, while saving some typing. This is also great for use in RMarkdown, because it will print the output below the code chunk without your having to add another line of code.

Corrections

If you see mistakes or want to suggest changes, please create an issue on the source repository.

Citation

For attribution, please cite this work as

Gahm (2018, July 22). Kaija Gahm: (Automatically show output). Retrieved from https://kaijagahm.netlify.app/posts/2018-07-22-automatically-show-output/

BibTeX citation

@misc{gahm2018(automatically,
  author = {Gahm, Kaija},
  title = {Kaija Gahm: (Automatically show output)},
  url = {https://kaijagahm.netlify.app/posts/2018-07-22-automatically-show-output/},
  year = {2018}
}