R Markdown lets you mix text, code, and results in one file. It helps you make reports that update automatically when your data changes.
R Markdown document creation in R Programming
--- title: "Document Title" author: "Your Name" date: "2024-06-01" output: html_document --- # Section Title Some text here. ```{r} # R code chunk summary(cars) ```
The top part between --- is called the YAML header. It sets the document title, author, date, and output format.
Code chunks start with ```{r} and end with ```. Inside, you write R code that runs and shows results.
--- title: "My First Report" output: html_document --- This is a simple R Markdown document. ```{r} print("Hello, world!") ```
--- title: "Analysis" author: "Alex" date: "2024-06-01" output: pdf_document --- # Data Summary ```{r} summary(mtcars) ```
--- title: "Presentation" output: ioslides_presentation --- # Slide 1 Welcome to my presentation. ```{r} plot(cars) ```
This R Markdown document creates an HTML report with a title, author, date, and a summary of the iris dataset.
--- title: "Simple R Markdown Example" author: "Friend" date: "2024-06-01" output: html_document --- # Introduction This document shows a summary of the <code>iris</code> dataset. ```{r} summary(iris) ```
You can change the output format in the YAML header to create HTML, PDF, Word, or slides.
Make sure to save your file with the .Rmd extension to use R Markdown.
Use the Knit button in RStudio to run the document and see the final report.
R Markdown combines text and R code in one file for easy reports.
Use YAML header to set title, author, date, and output format.
Write R code inside chunks marked by ```{r} and ```.
Practice
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 DQuick Check:
R Markdown = Text + R code for reports [OK]
- Thinking R Markdown is only for code
- Confusing with Shiny apps
- Assuming it's for data storage
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 CQuick Check:
Code chunk start = ```{r} [OK]
- Placing {r} after backticks incorrectly
- Using angle brackets instead of backticks
- Missing curly braces
```{r}
x <- 5
x * 2
```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 AQuick Check:
5 * 2 = 10 [OK]
- Expecting code to print the expression itself
- Confusing variable assignment with output
- Thinking error occurs due to chunk
```{r}
print('Hello World'
```
What is the problem?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 BQuick Check:
print('text') needs closing ) [OK]
- Ignoring missing parenthesis errors
- Thinking chunk delimiters cause error
- Assuming quotes are missing
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 AQuick Check:
YAML header with html_document output [OK]
- Using pdf_document or word_document instead of html_document
- Missing quotes causing parsing errors
- Incorrect output format name
Ready to prove it?
Pass the test and this pattern moves unknown
