1. R code in RMarkdown documents
In the last exercises you've looked at Markdown and how it can help with formatting prose. Now let's look at something that's probably even more important – R code.
2. <<<New Slide>>>
Before we get down to the nitty-gritty of R code chunks, let's look at a concrete example. This is an excerpt from the RMarkdown report you are going to create in the next few exersises.
It starts with a second-level header named Analysis, followed by a third-level header, Data. Then comes some prose with a link, and the first actual R code chunk, where the data is loaded from a URL. This code chunk doesn't produce any output, it just loads the data into the workspace.
The second code chunk though produces an output, namely a data frame with the average hourly compensation and weekly working hours per year. This output is automatically shown in the RMarkdown document. So embedding R code chunks in documents can both be helpful with showing how the code is written, but also with displaying the results it produces.
3. Adding R chunks
Adding R chunks to RMarkdown is pretty straightforward. Just add ```, followed by the letter r in curly brackets, followed by your R code, and then conclude the chunk with three backticks again.
RMarkdown will evaluate the code snippet for you, in this case, it just returns the number 4.
You can also add so-called inline R statements within your prose. For example, you write "2 plus 2 equals", and then a backtick and the r letter, and the R expression that will be evaluated, followed by another backtick. This results in this normal sentence. People looking at your final report will not even know there's R behind it.
4. The `knitr` package
But what's actually responsible for executing R code? Let's have a look back at the behind the scences diagram from the first video of this chapter. As you can see, in the first step, an RMarkdown document is converted to an ordinary Markdown document. In this course, all R code chunks are evaluated by the `knitr` package. Pandoc then converts this plain text file to other output formats. Let's have a closer look at knitr.
5. R chunk options
Knitr allows us to specify many different options for R code chunks. For example, you can hide your code chunk from the knitted report, but still execute the R code. For this, you need to add include = FALSE to the curly brackets in your r code chunk. If you refer back to the variable in another code chunk, you see that the above code was actually executed.
6. More R chunk options
There are a couple of other options: These all take either FALSE or TRUE as values. If, for instance, include is set to FALSE, neither the code chunk nor its output is shown in the knitted Rmarkdown, as you have just seen.
If only echo is set to FALSE, the chunk is shown, but its output is hidden.
If message and warning are set to FALSE, output messages and warnings, respectively, are not shown. This comes in handy as some functions produce a lot of messages or neglectable warnings that are not really important for your report.
Lastly, setting eval to FALSE still displays your code chunk in the knitted RMarkdown, but doesn't evaluate that chunk.
If you want to know more about all the available chunk options, have a look at the help page of the knitr package.
7. Let's try this out!
Now let's try some examples.