Code chunks let you write and run small pieces of R code inside documents. They help you show your code and its results together.
Code chunks and output in R Programming
Introduction
Syntax
R Programming
```{r} # Your R code here ``` # To show output, just run the chunk.
Use three backticks ``` to start and end a code chunk.
Inside the curly braces, write r to tell the document this is R code.
Examples
summary() function on the cars dataset and shows the output.R Programming
```{r} summary(cars) ```
R Programming
```{r, echo=FALSE} plot(cars) ```
R Programming
```{r, eval=FALSE} mean(c(1, 2, 3)) ```
Sample Program
This code chunk creates a list of numbers, calculates their average, and shows the result.
R Programming
```{r} # Calculate the mean of numbers numbers <- c(4, 7, 10) mean_value <- mean(numbers) mean_value ```
Important Notes
You can control if code or output shows using chunk options like echo and eval.
Output can be text, tables, or plots depending on the code.
Code chunks help keep your work organized and easy to understand.
Summary
Code chunks let you mix R code and results in one place.
Use backticks and {r} to create chunks.
Chunk options control what code and output appear.
Practice
1. What is the purpose of an R code chunk in an R Markdown document?
easy
Solution
Step 1: Understand R Markdown structure
R Markdown uses code chunks to embed R code inside text documents.Step 2: Purpose of code chunks
Code chunks run R code and display results inline, mixing code and output.Final Answer:
To run R code and show its output inline with text -> Option AQuick Check:
Code chunk = run code + show output [OK]
Hint: Code chunks run code and show output together [OK]
Common Mistakes:
- Thinking code chunks only hold text
- Confusing code chunks with document sections
- Assuming code chunks add images automatically
2. Which of the following is the correct way to start an R code chunk in R Markdown?
easy
Solution
Step 1: Recall R Markdown chunk syntax
R code chunks start with three backticks followed by {r} exactly.Step 2: Check each option
Only ```{r} correctly starts a chunk; others are invalid syntax.Final Answer:
```{r} -> Option CQuick Check:
Chunk start = ```{r} [OK]
Hint: Start chunks with three backticks and {r} [OK]
Common Mistakes:
- Omitting curly braces around r
- Using only two backticks
- Confusing HTML tags with chunk syntax
3. What will be the output of this R code chunk?
```{r}
x <- 5
x * 2
```medium
Solution
Step 1: Understand the code execution
Variable x is assigned 5, then multiplied by 2, resulting in 10.Step 2: R output format
R prints numeric output with index [1] before the value.Final Answer:
[1] 10 -> Option AQuick Check:
5 * 2 = 10 with [1] prefix [OK]
Hint: R prints numeric output with [1] prefix [OK]
Common Mistakes:
- Ignoring the [1] prefix in output
- Expecting just '10' without index
- Thinking code prints the expression itself
4. Identify the error in this R code chunk:
```{r}
print("Hello)
```medium
Solution
Step 1: Check the string syntax
The string starts with a double quote but lacks the closing double quote.Step 2: Consequence of missing quote
This causes a syntax error because the string is not properly closed.Final Answer:
Missing closing quote in the string -> Option BQuick Check:
Unclosed string causes syntax error [OK]
Hint: Check all quotes are closed in strings [OK]
Common Mistakes:
- Assuming chunk braces cause error
- Thinking print is misspelled
- Ignoring missing quote errors
5. You want to run an R code chunk but hide the code and only show the output. Which chunk option should you use?
hard
Solution
Step 1: Understand chunk options
echo=FALSE hides the code but runs it, showing output only.Step 2: Compare other options
eval=FALSE skips running code, include=FALSE hides both code and output, message=FALSE hides messages only.Final Answer:
```{r, echo=FALSE} -> Option DQuick Check:
echo=FALSE hides code, shows output [OK]
Hint: Use echo=FALSE to hide code but show output [OK]
Common Mistakes:
- Using eval=FALSE hides output too
- Using include=FALSE hides everything
- Confusing message=FALSE with code hiding
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
