Rmarkdown chunk code options

Hide code, text output, messages, or plots

Posted by Yuan on August 31, 2022

This blog is mainly from rmarkdown-cookbook/ and Yihui Xie’s Book

Settings in chunk code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
## Global Settings:

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
knitr::opts_chunk$set(message = FALSE)

#Settings
options(stringsAsFactors=FALSE)
options(warn=-1)
```

## Hide all output:
This will run the code, but not the chunk output in the output document
```{r, include=FALSE}
1 + 1
```

## Hide source code:
This will hide source code in the output
```{r, echo=FALSE}
1 + 1
```

## Hide text output (you can also use `results = FALSE`):

```{r, results='hide'}
print("You will not see the text output.")
```

## Hide messages:

```{r, message=FALSE}
message("You will not see the message.")
```

## Hide warning messages:

```{r, warning=FALSE}
# this will generate a warning but it will be suppressed
1:2 + 1:3
```

## Hide plots:

```{r, fig.show='hide'}
plot(cars)
```

Note that the plot will be generated in the above chunk. It is
just not displayed in the output.

## More specific settings:

```{r, echo=c(4, 5), message=c(1, 4), warning=2:3}
# one way to generate random N(0, 1) numbers
x <- qnorm(runif(10))
# but we can just use rnorm() in practice
x <- rnorm(10)
x

for (i in 1:5) message('Here is the message ', i)

for (i in 1:5) warning('Here is the warning ', i)
```

Only the 4th and 5th expressions are shown/echoed (note that a comment also counts as one expression). For the messages, only the 1st and 4th were shown, etc.

## chunk options could also written at the begining 
```{r}
#| my-chunk, echo = FALSE, fig.width = 10,
#| fig.cap = "This is a long long
#|   long long caption."

plot(cars)
```

Using variables in chunk code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
Use variables in chunk options
```{r}
my_width <- 7
```

```{r, fig.width=my_width}
plot(cars)
```

#If-else statement
```{r}
fig_small <- FALSE  # change to TRUE for larger figures
width_small <- 4
width_large <- 8
```

```{r, fig.width=if (fig_small) width_small else width_large}
plot(cars)
```
#Run a code chunk only if a required package is available
```{r, eval=require('leaflet')}
library(leaflet)
leaflet() %>% addTiles()
```
# Set root.dir
```{r}
#Set working directory
#The working directory starts to change in the following chunks after the chuck that contains knitr::opts_knit$set(root.dir = ...).
knitr::opts_knit$set(root.dir = params$wdir)

```