02-Code chunks

Ottavia M. Epifania

Code chunk

Open a new chunk code: shift + ctrl + i or

Chunk options

echo: Control the code display

Code executed & Shown

```{r}
#| echo: true
head(rock)
```
  area    peri     shape perm
1 4990 2791.90 0.0903296  6.3
2 7002 3892.60 0.1486220  6.3
3 7558 3930.66 0.1833120  6.3
4 7352 3869.32 0.1170630  6.3
5 7943 3948.54 0.1224170 17.1
6 7979 4010.15 0.1670450 17.1

Code executed & Not Shown

```{r}
#| echo: false
head(rock)
```
head(rock)
  area    peri     shape perm
1 4990 2791.90 0.0903296  6.3
2 7002 3892.60 0.1486220  6.3
3 7558 3930.66 0.1833120  6.3
4 7352 3869.32 0.1170630  6.3
5 7943 3948.54 0.1224170 17.1
6 7979 4010.15 0.1670450 17.1

Code executed & entire chunk shown

```{r}
#| echo: fenced
head(rock)
```
```{r}
head(rock)
```
  area    peri     shape perm
1 4990 2791.90 0.0903296  6.3
2 7002 3892.60 0.1486220  6.3
3 7558 3930.66 0.1833120  6.3
4 7352 3869.32 0.1170630  6.3
5 7943 3948.54 0.1224170 17.1
6 7979 4010.15 0.1670450 17.1

eval & include: Control the execution of the code

The code is not executed (but it is shown)

```{r}
#| eval: false
#| echo: true
head(rock)
```
head(rock)

The code is executed, neither the code nor the results are show

```{r}
#| include: false
head(rock)
```

:::

Other options

warning: true: (default) the warnings obtained from the code executed in the chunk are displayed message: true: (default) the messages associated to the code (e.g., when you upload a package with library(package))

error: false: (default) If there is an error in the code, knitting stops and the document will not be generated until the error is fixed

error: true: If there is an error in the code, the error message is shown in the document and the knitting goes on

YAML: Overall chunk options

Defined in the YAML, they are applied to all code chunks unless otherwise specified:

[...]
execute: 
  echo: fenced
  message: true
  error: true

If in one specific code we don’t want to display the code:

```{r}
#| echo: false
head(rock)
```

YAML: Show all code

[...]
format: 
  html:
    code-tools: 
     source: true
     toggle: false
     caption: This is my code

YAML: Code Fold

[...]
execute: 
  echo: fenced
  message: true
  error: true
code-fold: true
code-summary: See this specific code

Images & Graphs

External images: .png and .jpg

@fig-peacock1 illustrates a peacock

```{markdown}
#| eval: false
#| out-width: 50%
#| fig-align: center
#| fig-cap: A peacock living the life
#| fig-cap-location: bottom
#| label: fig-peacock1
#| code-line-numbers: "|3|4|5|6|7"
knitr::include_graphics("img/peacock.png")
```

Figure 1 illustrates a peacock

```{r}
#| out-width: 50%
#| fig-align: center
#| fig-cap: A peacock living the life
#| fig-cap-location: bottom
#| label: fig-peacock
#| code-line-numbers: "|3|4|5|6|7"
knitr::include_graphics("img/peacock.png")
```
Figure 1: A peacock living the life

Graphics

@fig-mtcars1 illustrates a plot

```{markdown}
#| out-width: 50%
#| fig-align: center
#| fig-cap: A graph from `mtcars`
#| fig-cap-location: bottom
#| label: fig-mtcars1
#| code-line-numbers: "|2|3|4|5|6"
ggplot(mtcars, aes(hp, mpg, color = factor(am))) +
  geom_point() +
  geom_smooth(formula = y ~ x, method = "loess") +
  theme(legend.position = 'bottom')
```

Figure 2 illustrates a plot

Figure 2: A graph from mtcars

Tables

kable

kable(mtcars[1:6, 1:6])
1
Basic table
mpg cyl disp hp drat wt
Mazda RX4 21.0 6 160 110 3.90 2.620
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875
Datsun 710 22.8 4 108 93 3.85 2.320
Hornet 4 Drive 21.4 6 258 110 3.08 3.215
Hornet Sportabout 18.7 8 360 175 3.15 3.440
Valiant 18.1 6 225 105 2.76 3.460

kableExtra

install.packages("kableExtra")
library(kableExtra)
1
Install package
2
Upload package