📖 Scenario: You want to create a simple report using R Markdown. This report will have a title, author, and a short introduction paragraph. R Markdown lets you combine text and R code in one document to create reports easily.
🎯 Goal: Create a basic R Markdown document with a title, author, date, and a short introduction paragraph.
📋 What You'll Learn
Create an R Markdown document with YAML header
Add title, author, and date in the YAML header
Write a short introduction paragraph in the document body
Include a simple R code chunk that prints a message
💡 Why This Matters
🌍 Real World
R Markdown is widely used to create reports that combine text, code, and output in one file. This helps share analyses clearly.
💼 Career
Data analysts, statisticians, and researchers use R Markdown to document their work and create reproducible reports.
Progress0 / 4 steps
1
Create the YAML header
Write the YAML header for an R Markdown document with the title "My First Report", author "Alex", and date "2024-06-01". Start the document with three dashes --- and end the header with three dashes ---.
R Programming
Hint
The YAML header starts and ends with three dashes ---. Inside, write the title, author, and date exactly as shown.
2
Add an introduction paragraph
Below the YAML header, write a short introduction paragraph that says: "This is my first R Markdown report."
R Programming
Hint
Just write the sentence exactly as shown below the YAML header. No quotes needed.
3
Add a simple R code chunk
Add an R code chunk below the introduction paragraph that prints the message "Hello, R Markdown!". Use the chunk header ```{r} to start and ``` to end the chunk. Inside the chunk, write print("Hello, R Markdown!").
R Programming
Hint
Start the code chunk with ```{r} and end with ```. Inside, use print() to show the message.
4
Render and display the output
Print the entire R Markdown document content to the console using cat() so you can see the full text including YAML, paragraph, and code chunk.
R Programming
Hint
Use cat() with triple quotes and escaped newlines to print the full document text exactly.
Practice
(1/5)
1. What is the main purpose of an R Markdown document?
easy
A. To store data in a spreadsheet format
B. To write only R code without any text
C. To create interactive web applications
D. To combine text and R code in one file for easy report generation
Solution
Step 1: Understand R Markdown's function
R Markdown allows mixing text and R code in one document.
Step 2: Identify the purpose
This combination is mainly used to create reports easily.
Final Answer:
To combine text and R code in one file for easy report generation -> Option D
Quick Check:
R Markdown = Text + R code for reports [OK]
Hint: Remember: R Markdown = text + code for reports [OK]
Common Mistakes:
Thinking R Markdown is only for code
Confusing with Shiny apps
Assuming it's for data storage
2. Which of the following is the correct way to start an R code chunk in an R Markdown document?
easy
A. <r>```
B. ```r{
C. ```{r}
D. {r}```
Solution
Step 1: Recall R code chunk syntax
R code chunks start with three backticks followed by {r} exactly.
Step 2: Match the correct syntax
```{r} matches the correct syntax: ```{r}
Final Answer:
```{r} -> Option C
Quick Check:
Code chunk start = ```{r} [OK]
Hint: Code chunks start with three backticks and {r} [OK]
Common Mistakes:
Placing {r} after backticks incorrectly
Using angle brackets instead of backticks
Missing curly braces
3. Given this R Markdown snippet, what will be the output in the final document?
```{r}
x <- 5
x * 2
```
medium
A. 10
B. x * 2
C. 5
D. Error: object 'x' not found
Solution
Step 1: Understand the code chunk execution
The code assigns 5 to x, then calculates x * 2, which is 10.
Step 2: Determine output shown in document
R Markdown shows the result of the last expression, which is 10.
Final Answer:
10 -> Option A
Quick Check:
5 * 2 = 10 [OK]
Hint: Last expression in chunk shows output [OK]
Common Mistakes:
Expecting code to print the expression itself
Confusing variable assignment with output
Thinking error occurs due to chunk
4. You wrote this R Markdown chunk but get an error when knitting:
```{r}
print('Hello World'
```
What is the problem?
medium
A. Wrong chunk start syntax
B. Missing closing parenthesis in print function
C. Missing quotes around Hello World
D. Code chunk not closed with backticks
Solution
Step 1: Check the print statement syntax
The print function is missing a closing parenthesis.
Step 2: Verify chunk syntax
The chunk start and end are correct; quotes are present.
Final Answer:
Missing closing parenthesis in print function -> Option B
Quick Check:
print('text') needs closing ) [OK]
Hint: Count parentheses carefully in code chunks [OK]
Common Mistakes:
Ignoring missing parenthesis errors
Thinking chunk delimiters cause error
Assuming quotes are missing
5. You want to create an R Markdown document with title "My Report", author "Alex", and output as HTML. Which YAML header is correct?
hard
A. ---
title: "My Report"
author: "Alex"
output: html_document
---
B. ---
title: My Report
author: Alex
output: pdf_document
---
C. ---
title: "My Report"
author: "Alex"
output: word_document
---
D. ---
title: My Report
author: Alex
output: html
---
Solution
Step 1: Identify correct YAML format for title, author, output
YAML requires --- lines, key: value pairs, and output format as html_document for HTML output.
Step 2: Match options with correct YAML
---
title: "My Report"
author: "Alex"
output: html_document
--- uses quotes for strings and output: html_document, which is correct.
Final Answer:
---
title: "My Report"
author: "Alex"
output: html_document
--- -> Option A
Quick Check:
YAML header with html_document output [OK]
Hint: Use output: html_document for HTML in YAML [OK]
Common Mistakes:
Using pdf_document or word_document instead of html_document
Missing quotes causing parsing errors
Incorrect output format name
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.