R-Markdown and Knitr Tutorial (Part 2)

December 28, 2015
By

(This article was first published on Modern Data » R, and kindly contributed to R-bloggers)

In our last post we quickly went over how to create a new R-Markdown document and embed a Plotly graph in it. In this post we’ll get into more details around how to control code output using chunk options.

As shown in our previous post, for the embedded R code to be evaluated, it’ll need to be written inside a code-chunk as shown below.

  ```{r Code Chunk, chunk options here...}
  # R code here...
  ``` 

Chunk options

Knitr provides a lot of ways to control the output that shows up in the final document. We’ll highlight a few common ones.

Echo

echo allows control over visibility of the actual R code in a chunk. Set it to FALSE to hide the R code from showing up. Note that the code chunk will still be evaluated and any outputs mirrored in the final document. Use eval = FALSE if you do not need a code chunk to be evaluated at all.

  ```{r Code Chunk, echo = FALSE}
  # R code will be evaluated but not shown in the final HTML document
  # Useful when only plots / charts / text output needs to be shown and not the
  # code that generated it...
  ``` 

Message

Notice how if you use include() in a code chunk, messages from the R console are mirrored in the final document as well? By default, when knitr evaluates a code chunk, it mirrors any messages that show up in the console as well. We can turn that off using message = FALSE.

  ```{r Code Chunk, message = FALSE}
  # R code
  # Messages from R-Console will not show up in final document
  ``` 

With message = TRUE

library(plotly)
## Loading required package: ggplot2
## 
## Attaching package: 'plotly'
## 
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## 
## The following object is masked from 'package:graphics':
## 
##     layout

With message = FALSE

library(plotly)

Results

Controls the mirroring of output when a code chunk is evaluated. This includes text output as well as charts / graphs. Four values can be assigned: (see http://yihui.name/knitr/options/ for more details)

  • markup – markup output using output hook
  • asis – raw output
  • hold – shows output after all code is evaluated
  • hide – no output is shown
  ```{r Code Chunk, results = 'markup'}
  # R code will be evaluated but not shown in the final HTML document
  # Useful when only plots / charts / text output needs to be shown and not the
  # code that generated it...
  ``` 

With results = ‘markup’

str(mtcars)
## 'data.frame':    32 obs. of  11 variables:
##  $ mpg : num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
##  $ cyl : num  6 6 4 6 8 6 8 4 4 6 ...
##  $ disp: num  160 160 108 258 360 ...
##  $ hp  : num  110 110 93 110 175 105 245 62 95 123 ...
##  $ drat: num  3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
##  $ wt  : num  2.62 2.88 2.32 3.21 3.44 ...
##  $ qsec: num  16.5 17 18.6 19.4 17 ...
##  $ vs  : num  0 0 1 1 0 1 0 1 1 1 ...
##  $ am  : num  1 1 1 0 0 0 0 0 0 0 ...
##  $ gear: num  4 4 4 3 3 3 3 4 4 4 ...
##  $ carb: num  4 4 1 1 2 1 4 2 2 4 ...

With results = ‘asis’

str(mtcars)

‘data.frame’: 32 obs. of 11 variables: $ mpg : num 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 … $ cyl : num 6 6 4 6 8 6 8 4 4 6 … $ disp: num 160 160 108 258 360 … $ hp : num 110 110 93 110 175 105 245 62 95 123 … $ drat: num 3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 … $ wt : num 2.62 2.88 2.32 3.21 3.44 … $ qsec: num 16.5 17 18.6 19.4 17 … $ vs : num 0 0 1 1 0 1 0 1 1 1 … $ am : num 1 1 1 0 0 0 0 0 0 0 … $ gear: num 4 4 4 3 3 3 3 4 4 4 … $ carb: num 4 4 1 1 2 1 4 2 2 4 …

With results = ‘hide’

str(mtcars)