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 GitHublibrary(dplyr)
Attaching package: 'dplyr'
The following objects are masked from 'package:stats':
filter, lag
The following objects are masked from 'package:base':
intersect, setdiff, setequal, union
…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.
Citation
BibTeX citation:
@online{gahm2018,
author = {Gahm, Kaija},
title = {Automatically Show Output},
date = {2018-07-22},
url = {https://kaijagahm.github.io/posts/2018-07-22-automatically-show-output},
langid = {en}
}