Get startedGet started for free

Introduction to R Markdown

1. Introduction to R Markdown

Hi and welcome to the course. My name is Amy and, in this course, we'll learn how to create reports using R Markdown.

2. R Markdown

R Markdown is a tool we use to create efficient reports to summarize analyses and communicate results to an audience. We can create HTML and PDF documents with R Markdown using only R code. R Markdown is also a way to ensure that the results are reproducible, which is important to guarantee when creating reports.

3. Reproducible results

Consider the following scenario: you've conducted your analyses and created a presentation to demonstrate the findings.

4. Reproducible results

You give the presentation to stakeholders, and they request some modifications.

5. Reproducible results

However, you can't remember each step you went through

6. Reproducible results

and aren't able to reproduce your work! By generating the results and report from code with R Markdown, you, and others, can always reproduce your results.

7. R Markdown elements

An R Markdown document is made of three components: the code, the text of the report, and the metadata for the file.

8. R Markdown file

The YAML header containing the metadata appears at the top of the file, followed by the contents that make up the report, including the text and code in code chunks. YAML is a syntax for hierarchical data structures that is commonly used for configuration files.

9. Knit

When we finish modifying a file and are ready to see the report, we'll need to knit it. Knitting a file is how we generate an output file from the R markdown file. When a file is knit, R Markdown runs the chunks of R code contained in the file and combines them with the text in the document into an HTML file. For example, when the R Markdown file shown on the left is knit, it will create the output shown on the right. We'll learn more about output types later in the chapter.

10. Course overview

In this course, we'll cover the various elements that make up an R markdown document. Then, we'll learn how to add plots and analyses to a report, organize and improve a report, and customize a report by specifying fonts and colors that reflect a brand.

11. Investment annual summary

Throughout the course, we'll build a report using real-world datasets from the World Bank International Finance Corporation or IFC. The first dataset, investment annual summary, provides a summary of the dollars in millions provided to each region for each fiscal year, from 2012 to 2018.

12. Investment services projects

The second dataset, investment services projects, displays more specific information about each investment project from the 2012 to 2018 fiscal years. Note that the fiscal year starts on July 1st of the previous year and ends on June 30th of the year of interest. Information listed includes the project name, company name, sector, project status, and investment amounts.

13. Code chunks

To get started, we'll add some code by adding a code chunk. A code chunk is a section that contains the code that will either render output in the report or display the code itself as part of the report. Chunks are what separate the text of the report from the code.

14. Adding code chunks

Code chunks start and end

15. Adding code chunks

with a set of three backticks. The first set of backticks signify the beginning of the code chunk

16. Adding code chunks

and are followed by curly braces that include the letter r, to specify that we are adding r code. Within the curly braces, we can specify a number of other chunk options. We'll be discussing the details of this later in the course.

17. Adding code chunks

In the code chunk shown, we've specified the investment annual summary dataset name in the code chunk. When we knit the file,

18. Adding code chunks

we see the dataset included in the document.

19. Let's practice!

Let's practice!