- Mastering RStudio:Develop,Communicate,and Collaborate with R
- Julian Hillebrand Maximilian H. Nierhoff
- 2004字
- 2021-08-20 10:42:30
Advanced R Markdown documents
After compiling our first R Markdown report, we want to go ahead and look at the advanced options for embedding code chunks with the knitr
syntax. Also, you will learn how to brand your reports with custom style sheets.
Getting to know R code chunks
As we have already seen in our sample report, R Markdown uses so called code chunks to render R Code into reports.

This exemplary code chunk shows the most elementary way to include an R code snippet in our .Rmd
file. Just three back ticks, ```
at the beginning and end of the chunk, and the letter r
in curly brackets, {r}
.

Even if it is a short code chunk, the output includes an H1
heading, the used R code, and a complete plot.
Customizing R code chunks
R Markdown offers a lot of options to customize your code chunks. This is necessary because, on the one hand, R Markdown includes all code lines and even error and warning messages by default. On the other hand, including many lines of code may distract readers from your report, especially when they are not familiar with R. Therefore, let's learn the options and arguments that help us create an easy-to-read and clean R Markdown report.
Chunk options
First, we will learn how to avoid our report showing warnings, errors, and general messages.
Avoiding errors, warnings, and other messages
In the following example, we will be loading a library, which returns a package information message that gets fully printed within our report:
### Chunk option example ```{r} install.packages(forecast) library(forecast) ```
Your screen will look like the following screenshot:

So, to prevent all these messages, we need to provide our code chunk with an argument when it's loading packages. In this case, we add the argument, message=FALSE
, after the r
in the curly brackets.
The code chunk looks like this:
### Chunk option example ```{r message=FALSE} library(forecast) ```
Now, the output of our knitted file shows only the loading of the library.

In the same way, you can also prevent the output of warnings and errors by adding warning=FALSE
, error=FALSE
, or both to your code chunk. But please keep in mind that these warnings and errors are issued for a reason.
Hiding distracting lines of code
As already noted, including tons of lines of code in your report may disrupt the flow of reading and confuse people who have no idea of R code or programming. To solve this problem, a number of arguments can be added to the code chunks.
The three most popular chunk arguments are:
echo = FALSE
: On applying this argument, R Markdown will not print the lines of code of the chunk, but will still run the codeeval = FALSE
: Using this argument, R Markdown won't run the code or include the possible results, but it will show the code of the chunkresults = 'hide'
: With the addition of this option, R Markdown will not show any results, but will run and print the code of the chunk
Embedding R code inline
If you want to embed R code in the text of your report, just add the `r `
syntax. R markdown will run the code and print the results. Of course, it makes sense that you just embed results as a number, character string, or other similar things, and not as a full-blown table or something bigger.

The preceding code file results in the following R Markdown when knitted:

Labeling code chunks
When you create bigger reports with lots of code chunks, it is useful to label these chunks. First of all, a label helps you know what the code chunk is doing; secondly, by adding labels you can reuse the chunks in your report.

As you can see in the previous example, the first code chunk was labeled with the name, thePlot
. Furthermore, it was given the chunk argument, eval=FALSE
, and therefore, only the code gets shown in the final report. The second code chunk reuses the code of the first chunk by using the argument, ref.label="thePlot"
. In the report, only the plot gets shown since the argument, echo=FALSE,
was added to the reused code chunk.
Pandoc and knitr options
As we already know, R Markdown is a combination of many different techniques and technologies. When it comes to compiling Markdown-formatted text and code chunks into different output formats, a tool called pandoc is used.
If you need to convert files from one markup format into another, pandoc is your swiss-army knife.
--About pandoc (http://pandoc.org/index.html)
When you create a new R Markdown file in RStudio next to the prefilled text, there are also four lines of code between three dashes at the beginning of each new .Rmd
file. After knitting the file, this so-called YAML header will not appear in the output, but R Markdown will take some information out of it.

The final HTML file takes the given title, the author name, and the date out of the YAML header and includes them in the compiled report.
Output formats
You change the output format by clicking on the down-facing arrowhead next to the Knit button, and choosing HTML, PDF, or Word; you can also change it in the YAML header. To do so, write the format after the output argument, as shown as follows:
output: html_document
: This creates an HTML fileoutput: pdf_document
: This produces a PDF documentoutput: word_document
: This generates a Microsoft Word fileoutput: md_document
: This outputs a Markdown file
All output formats will be saved into your existing working directory.

Changing the look of the output
By overwriting and adding new arguments to the YAML header, you can easily change the final look of your report. A lot of change options can be made with the help of the respective output settings.

In the previous example, we changed the syntax highlighting to espresso, and we applied a CSS theme called journal. These default themes are collected from the well-known Twitter bootstrap theme. The effects of these small adjustments can be inspected in the following screenshot. On the left-hand side, we see the normal output with default syntax and the default CSS theme, and on the right-hand side is our new output with the changed syntax and theme.

There are countless options to modify the YAML header, and hence, the output of the R Markdown file. Of course, you can change everything in the header manually, but you need to pay attention to the right indentation:
--- title: "My Report" output: html_document: toc: yes number_sections: yes theme: united ---
The preceding header will generate a working HTML file with a table of contents and numbered headings; it uses a CSS theme called united.

The indentation for the YAML header is very strict. If you're using, for example, an incorrect indentation in the YAML header, an error message will appear in the R Markdown console. Thus, you can quickly find and fix those errors:
1 --- 2 title: "My Report" 3 output: 4 html_document: 5 toc: yes 6 number_sections: yes 7 theme: united 8 ---
The error message looks as follows:
Error in yaml::yaml.load(front_matter) : Scanner error: mapping values are not allowed in this context at line 5, column 22
You can learn more about YAML at http://yaml.org/.
Using a custom CSS style sheet
As already learned, for the HTML output format, you can change the whole look by applying one of the inbuilt CSS themes. But RStudio also makes it possible to use a custom CSS file. As a result, R Markdown reports can be adapted to an existing corporate design, for example.
In the settings window of the HTML output, which opens after a click on the gear icon, you need to uncheck the Apply theme box and check the Apply CSS file box instead. Then, you can add your custom CSS file.

Using R Markdown templates
RStudio offers the option to use completely custom templates for R Markdown. This makes sense if you want to use a template again and again. In contrast to the possibility of applying a style sheet with its own CSS file, you can adjust basically everything with a Markdown template. So, you can adjust not only the fonts and color schemes, but also the complete formatting and the corresponding special formats. In general, these templates are installed or distributed through R packages. RStudio has installed two templates by default. One is the package vignette, and the other the so-called Tufte Handout.
You can open these templates by creating a new R Markdown file and clicking on From Template in the selection menu on the left-hand side.

Package vignette
The Package Vignette provides an example of a format that customizes the base html_document with custom CSS and some other tweaks related to vignette authoring. The source code for the Package Vignette format and custom template is a good starting point to create your own HTML-based formats.
--R Markdown v2 Guide (http://rmarkdown.rstudio.com/developer_document_templates.html)
The package vignette
file also comes prefilled with Markdown-formatted text and example code chunks. It looks like this:

The Tufte handout
Edward Tufte is a statistician and artist, and Professor Emeritus of Political Science, Statistics, and Computer Science at Yale University. He wrote, designed, and self-published 4 classic books on data visualization.
--EdwardTufte.com (http://www.edwardtufte.com/tufte/)
The Tufte handout is adapted to the style that the famous statistician used in his well-known books. This refers to fonts, typography, the overall graphics integration in running text, and also the wide use of side notes are implemented in the prefilled template. When you knit the template, a PDF file is the output.

Compiling R Notebooks
R Markdown also offers a quick way to render a so-called R Notebook, which is in fact equal to the normal HTML output of a knitted .Rmd
file. Just type rmarkdown::render("yourfilename.R")
into the RStudio console and an HTML version of your R script will be generated.

Generating R Markdown presentations
In conjunction with R Markdown, RStudio also offers the possibility of building presentations. When you click on New File and choose R Markdown..., you need to select the item called Presentation in the popup window. RStudio offers three default output formats to choose from.

But you can also change the YAML header of existing R Markdown documents from this:
---
title: "My Report"
author: "Your Name"
output: html_document
---
To the following header, which for example, creates a slidy
presentation:
---
title: "My Report"
author: "Your Name"
output: slidy_presentation
---
The same applies to beamer_presentation
and ioslides_presentation
. All R Markdown presentations will not open in the Viewer pane, but in a new window.
ioslides
An ioslides presentation is an HTML document, and to start a new slide, you use the #
hash sign. If you use one hash sign to start a new slide, the background of the slide will be dark, and if you use two hash signs, the background looks bright. The content of the first slide will be taken from the YAML header. The following lines demonstrate this in code:
This will be the content of your first slide: -- title: "My Report" author: "Your Name" output: ioslides_presentation -- # Next slide (background will be dark) … content … ## Next slide (background will be bright) … content … # Next slide (background will be dark) … and so on …

Slidy
Slidy presentations are also HTML documents after knitting, but they are somewhat more sophisticated than ioslides. For example, slidy offers an inbuilt table of contents from the beginning. Moreover, you can see the number of total slides, and so, it is no blind flight for the viewers.
Again, you use hash signs to create a new slide. But, this time, you need to decide if you use one hash sign or two hash signs. Also, the background is always bright.

Beamer
Beamer presentations are compiled to PDF. To start a new slide or sheet, you use one or two hash signs, as we already know from slidy presentations. Beamer Presentations allow a lot of customizations with LaTeX, such as macros and others.

- Learning SAP Analytics Cloud
- Scala程序員面試算法寶典
- RabbitMQ Essentials
- Python算法詳解
- C#程序設計教程(第3版)
- Cocos2d-x Game Development Blueprints
- Practical Predictive Analytics
- Visual Basic 程序設計實踐教程
- Visual Basic語言程序設計上機指導與練習(第3版)
- 計算機系統解密:從理解計算機到編寫高效代碼
- Eclipse開發(學習筆記)
- Practical Time Series Analysis
- Getting Started with SQL Server 2014 Administration
- Learning Akka
- 青少年編程魔法課堂:Python圖形化創意編程