Embedding R code chunks
In an R Markdown report, one can embed R code chunks into the report with the knitr
syntax. In this recipe, we introduce how to create and control the output with different code chunk configurations.
Getting ready
Ensure you have installed the latest version of R and RStudio on your operating system. Also, you need to have created and opened a new R Markdown (.rmd
) file in RStudio.
How to do it…
Please perform the following steps to create an R code chunk in the markdown report:
First, create a basic code chunk with the
knitr
syntax:Markdown
Preview
```{r}
# code block
a <- 3
b <- 2
a + b
```
We can hide the script by setting
echo=FALSE
:Markdown
Preview
```{r, echo=FALSE}
# code block
a <- 3
b <- 2
a + b
```
Alternatively, we can stop evaluating the code by setting
eval=FALSE
:Markdown
Preview
```{r, eval=FALSE}
# code block
a <- 3
b <- 2
a + b
```
Moreover, we can choose not to render both evaluation result and script by setting
include...