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:

  1. First, create a basic code chunk with the knitr syntax:

    Markdown

    Preview

    ```{r}

    # code block

    a <- 3

    b <- 2

    a + b

    ```

    How to do it…
  2. We can hide the script by setting ...

Get R for Data Science Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.