CODE CHUNKS

Dr. Ajay Kumar Koli, PhD | SARA Institute of Data Science, India

Code Chunk


A code chunk is a section of code inside your Quarto (.qmd) document that gets executed when you render the document. The output (like a table or plot) is shown right in the final HTML, PDF, or Word file.

Basic Syntax of an R Code Chunk


```{r}

```
  • Starts and ends with triple back ticks (```).

  • {r} tells Quarto it’s an R code chunk.

Simple Calculation


```{r}

5 + 7

```

This will print the result 12 in the rendered document.

Plot


```{r}

plot(cars)

```

This shows a plot of the built-in cars dataset.

Code chunks (or cells or blocks)

Three ways to insert code chunks in the quarto file:

  1. Keyboard shortcuts:

    • for Option + Cmd + i.
    • for ctrl + alt + i.
  2. Insert Chunk button in the editor toolbar.

  3. Manually type the chunk delimiters ```{r} and ```.

Code chunks (or cells or blocks)

Two ways to run code chunks:

  1. Use the Run Current Chunk or Run All Chunks Above buttons.

  2. Run the current code chunk with Cmd/Ctrl + Shift + Enter.

🤯 Your Turn

🤩 Your Turn Answers

Chunk Options


```{r}
#| echo: FALSE     
#| message: F

summary(mtcars)

```

Chunk Options

Option What it does
echo=FALSE Hides the code in output
eval=FALSE Shows the code but doesn’t run it
message=FALSE Hides messages (like from packages)
warning=FALSE Hides warnings

R
packages

R packages


“An R package is a collection of functions, data, and documentation that extends the capabilities of base R. Using packages is key to the successful use of R.”

Metacran

R Packages

  1. Install the R package

  2. Call the R package

  3. Update the R package

  4. Remove the R package

Install Packages

Name of the Packages

Installed Packages

Function to Install Packages


```{r}

install.packages("tidyverse")

```

Function to Call Package


```{r}

library(tidyverse)

```

Using Packages

  • You need to install package only once like:

    • 📚 We buy books once and use them again and again

    • 💡 Fix the bulb once and use it again and again.

Using Packages

  • In every R document you need to call once the package using function library(), for example library(ggplot2).

  • Once in a while, you need to update the installed packages as well.

  • If you un-install R or RStudio, you will lose all installed packages.

Tools \(\rightarrow\) Package Updates

Select Packages to Update

Click Install Updates

To Remove Packages

🤯 Your Turn

1. What symbol is used to start a code chunk in a Quarto document?

  1. ##
  2. ---
  3. ```{r}
  4. %%r

🤯 Your Turn

2. What is an R package?

  1. A shortcut to open RStudio
  2. A collection of R functions, data, and documentation
  3. A file format used in Quarto
  4. A type of plot

🤯 Your Turn

3. Which function is used to install a new R package?

  1. install.package()
  2. add.library()
  3. library()
  4. install.packages()

🤯 Your Turn

4. What does echo=FALSE do in a code chunk?

  1. Skips the chunk
  2. Hides the code but shows the result
  3. Stops the chunk from running
  4. Shows both code and output

🤯 Your Turn

  1. dplyr
  2. ggplot2
  3. readr
  4. shiny

🤩 Your Turn Answers

  1. Correct answer: C) ```{r}

  2. Correct answer: B) A collection of R functions, data, and documentation

  3. Correct answer: D) install.packages()

  4. Correct answer: B) Hides the code but shows the result

  5. Correct answer: B) ggplot2