Knitting documents containing View()


This came up yet again in my workshop-- the View() function is great to launch a preview of your data in a spreadsheet-like format. But, it’s the only function I know of that works in the Console but totally borks your knitted document. Even ? and help() can be put in a chunk, and your document will still knit. The error message is hard to interpret, because new users probably don’t know what X11 is.

This should be fixed, but I don’t know where to submit an issue

  • base R, because View() is from utils (i.e., say it should provide better error catching or error message?)
  • knitr (special conditional behavior in the knitting just for View, either just make a tibble and display, or pass along a better error message, like “remove View from your chunk”)
  • RStudio IDE (don’t know what this would be)



I second all you said. Many of my students confront this issue and it’s nearly impossible for them to diagnose.


To make matters worse, View() causes a fatal error or not, depending on the type of input. In my current example, View() on a nested list causes an error, but View() on an integer vector, for example, does not. So you also can’t come up with universal advice re: its use.


I think the R Markdown package might be a good place to start:, in case the solution is outside of knitr.


@AmeliaMN I’ve had many an issues with this as well, and I hope the issue gets some :heart: in the near future and View() in an Rmd gives a nice error.

In the meantime, my solution is to tell my students to never type View() anywhere. Call me crazy, but it’s what has worked for me for keeping View() out of Rmd documents. Tibbles help in this regard because now if you write the name of a tibble in the Console it doesn’t try to print out the whole thing. And if you want to launch the viewer, you can just click on the object in the environment. The one challenging bit of this is data loaded as a promise, but I believe RStudio will soon allow you to click on those to open them in the viewer as well (see here for the relevant update to the IDE, though I’m not sure when this will make it to a stable release of the IDE).


On @garrett’s suggestion, I did submit an issue on rmarkdown. I try to encourage students not to write View(), as well, but since I usually teach file reading using the interface first, it’s hard to underscore. If you are copying the library(readr) line and the dataset <- read_csv("dataset.csv") line, why can’t you copy the View(dataset) line? Maybe it would be better if the IDE didn’t run the View() by default when loading in a dataset, to help separate it.


How about teaching something like this (works interactively, in knitr, and in reprex):

v <- function(x, title = as.character(substitute(x)), n = 20) {
  if(interactive()) {
    View(x, title = title)
  } else {
    knitr::kable(head(x, n = n), caption = title)


I’ve added a note on this here.


Some related musing: