2.4 Code Chunks

Like Sweave, R Markdown has code chunks and the options are very similar. In R Markdown, chunk options can be set globally in the setup chunk. These options are over-ridden by any locally set options.

For example, to hide the code and suppress warnings and messages in the output you can set the options like this:

knitr::opts_chunk$set(echo = FALSE,warning=FALSE,message = FALSE)
library(reportRx)

Useful chunk options:

  • eval = FALSE prevents evaluation of the code. This can be useful for chunks of code that take a long time to run and you may want to run once and save to an R file. It replaces the need to source a separate R script for some code.

  • echo = FALSE hides the code, but prints any output.

  • warning = FALSE and message = FALSE suppress warnings and messages.

  • results = "hide" hides the output (can be used with echo=TRUE).

  • results = "asis" treats the output of your R code as literal Markdown. This is useful if you want to generate text from your R code, for example using cat.

  • fig.width = 5 and fig.height = 5 set the height and width of figures (in inches).

  • fig.cap ='A nice caption' will print a caption beneath a figure.

For other options see https://yihui.name/knitr/options.