0
0
R Programmingprogramming~5 mins

Code chunks and output in R Programming

Choose your learning style9 modes available
Introduction

Code chunks let you write and run small pieces of R code inside documents. They help you show your code and its results together.

When you want to explain your data analysis step by step with code and results.
When creating reports that include both R code and its output.
When sharing your work so others can see how you got your results.
When testing small parts of your code inside a bigger document.
When making presentations that include live R code examples.
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
This chunk runs the summary() function on the cars dataset and shows the output.
R Programming
```{r}
summary(cars)
```
This chunk creates a plot but hides the code, showing only the plot output.
R Programming
```{r, echo=FALSE}
plot(cars)
```
This chunk shows the code but does not run it, so no output appears.
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
```
OutputSuccess
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.