Bird
Raised Fist0
R Programmingprogramming~7 mins

R Markdown document creation in R Programming

Choose your learning style10 modes available
Introduction

R Markdown lets you mix text, code, and results in one file. It helps you make reports that update automatically when your data changes.

You want to write a report that includes R code and its output together.
You need to share your data analysis with others in a clear, readable format.
You want to create documents that combine explanations, code, and graphs.
You want to make presentations or websites from your R code and results.
You want to keep your work organized and reproducible.
Syntax
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.

Examples
A basic document with a title and a code chunk that prints text.
R Programming
---
title: "My First Report"
output: html_document
---

This is a simple R Markdown document.

```{r}
print("Hello, world!")
```
This document sets author and date, and will create a PDF report with a data summary.
R Programming
---
title: "Analysis"
author: "Alex"
date: "2024-06-01"
output: pdf_document
---

# Data Summary

```{r}
summary(mtcars)
```
Example of making a presentation slide with a plot from R code.
R Programming
---
title: "Presentation"
output: ioslides_presentation
---

# Slide 1

Welcome to my presentation.

```{r}
plot(cars)
```
Sample Program

This R Markdown document creates an HTML report with a title, author, date, and a summary of the iris dataset.

R Programming
---
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)
```
OutputSuccess
Important Notes

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.

Summary

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

(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

  1. Step 1: Understand R Markdown's function

    R Markdown allows mixing text and R code in one document.
  2. Step 2: Identify the purpose

    This combination is mainly used to create reports easily.
  3. Final Answer:

    To combine text and R code in one file for easy report generation -> Option D
  4. 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

  1. Step 1: Recall R code chunk syntax

    R code chunks start with three backticks followed by {r} exactly.
  2. Step 2: Match the correct syntax

    ```{r} matches the correct syntax: ```{r}
  3. Final Answer:

    ```{r} -> Option C
  4. 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

  1. Step 1: Understand the code chunk execution

    The code assigns 5 to x, then calculates x * 2, which is 10.
  2. Step 2: Determine output shown in document

    R Markdown shows the result of the last expression, which is 10.
  3. Final Answer:

    10 -> Option A
  4. 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

  1. Step 1: Check the print statement syntax

    The print function is missing a closing parenthesis.
  2. Step 2: Verify chunk syntax

    The chunk start and end are correct; quotes are present.
  3. Final Answer:

    Missing closing parenthesis in print function -> Option B
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    --- title: "My Report" author: "Alex" output: html_document --- -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard