By Riddhiman
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: ...read more
Source:: r-bloggers.com