Customize Code Chunks

In this class, you’ve learned how to use R Notebooks to “knit” together plain text and code. By default R Notebooks are quite verbose - you get a lot of output including many things that are useful for you when you are conducting analysis. As you work to polish notebooks for dissemination, however, you will want more control over exactly what is displayed or output into your knitted notebooks.

R notebooks allow you to specify chunk options that can help you control what output makes it into your knitted output document.

A default code chunk containing R code starts like this ```{r} and ends like this ```.

# This is a code chunk.

You can add additional chunk options to the beginning of your code chunk in order to control what aspects are evaluated and included in your notebook’s output.

The R Markdown Reference Guide contains a list of many code chunk options. For a more detailed description, please see knitr’s documentation. Some of the common chunk options you can use to control what is evaluated and what is included in your rendered output are the following:

  • include = FALSE: Run the code chunk but do not include the chunk in the rendered document
  • echo = FALSE: Do not display the code chunk in the rendered document, but show the output from the code
  • eval = FALSE: Display the code chunk but do not evaluate the code contained within the chunk
  • error = FALSE: Do not display any error messages generated by the code in your rendered document
  • message = FALSE: Do not display any messages generated by the code in your rendered document
  • results = FALSE: Do not display the code results in your rendered document
  • warning = FALSE: Do not display any warnings generated by the code

It is likely that you will have to play with these options in order to identify the correct one to produce the output you want in your rendered document.