3.1 Code chunks and inline R code

An R Markdown document consists of intermingled prose (narratives) and code. There are two types of code in an Rmd document: code chunks and inline R code. Below is a quick example:

x <- 5  # radius of a circle

For a circle with the radius `r x`,
its area is `r pi * x^2`.

A code chunk usually starts with ```{} and ends with ```. You can write any number of lines of code in it. Inline R code is embedded in the narratives of the document using the syntax `r `. In the above example, we defined a variable x in a code chunk, which is the radius of a circle, and calculated its area in the next paragraph.

You can customize the behavior and output of code chunks through chunk options (provided inside the curly brackets {}). You will find several examples in Chapter 11. You may write code of other languages in code chunks, too (see Chapter 15).