Section 3 The code chunks

We have already seen that we can highlight any code with triple backticks and the language keyword in the first section of the book. The Knitr R package provides a mechanism for including live code into the document instead. This means that apart from code, but can see and interact with the results of such code. This is extremely useful for research communication and reproducibility.

For this part of the book, we will need to install a few additional packages. Copy and paste install packages code into your RStudio console

install.packages(c("dplyr", "DT", "ggplot2"))

3.1 Introduction

Each code block is referred to as “code chunk”. You can have as many code chunks as you like. The code chunk is essentially what we have already been using, i.e triple backticks on either side of the code blob, but now add curly brackets at the end of the first triplet e.g

```{r}

```

Look for green “insert” button at the top of the text editor pane, in order to insert R chunk. Alternatively use keyboard shortcut alt+shift+i

Each chunk is highly configurable via chunk options, using key value pairs once again. The very first chunk option is engine i.e which language is included in the code chunk. Yes Rmarkdown is capable of doing many different languages. Use drop down arrow on the green “insert” button to explore some of the most common languages.

N.B: Name of the engine i.e the language name, always position first in the R chunk, usually key “engine” is omitted.

Let’s include our first bit of R code. We are going to load a library magrittr, to get a pipe operator (it isn’t overly important to understand the pipe operator for this course), then we are going to call base function avaliable.packages() and store results in the variable avail_packages and Knit our document once again. Note that I’m also including new section header.

# R code

```{r}
library(magrittr)

avail_packages <- available.packages(contriburl = contrib.url("https://mirror.aarnet.edu.au/pub/CRAN/"))

#head(avail_packages)

avail_packages %>% head
```

— It’s computing !

3.2 Chunk name

It is very useful to name your code chunks, they can server multiple purposes such as cross-referencing and debugging. Names are optional, but if you are including them, they must be in a second position.

General layout of any chunk is

```{r chunk_name, options}
```

Let’s add a get_data name to our code chunk. We also going to wrangle our data a little bit to get most relevant information for us and press Knit

```{r get_data}

library(magrittr)
library(dplyr)

avail_packages <- available.packages(contriburl = contrib.url("https://mirror.aarnet.edu.au/pub/CRAN/")) %>% 
                    as.data.frame() %>% 
                    as_tibble() %>% 
                    rowwise() %>% 
                    mutate(Depend = length(unlist(strsplit(Imports, split = ",")))) %>% 
                    ungroup() %>% 
                    select(Package, Version, Depend, Imports, License)
```

— How is it looking?

3.3 Chunk options

For comprehensive overview of code chunks that knitr refer to definitive guide from the author of Knitr and Rmarkdown R packages.

Here is a short list of the most common that tend to be used by me.

name value type description
child NULL code_evaluation A character vector of filenames. Knitr will knit the files and place them into the main document.
engine ‘R’ code_evaluation Knitr will evaluate the chunk in the named language, e.g. engine = ‘python’. Run names(knitr::knit_engines$get()) to see supported languages.
eval TRUE code_evaluation If FALSE, knitr will not run the code in the code chunk.
include TRUE code_evaluation If FALSE, knitr will run the chunk but not include the chunk in the final document.
fig.align ‘default’ plots How to align graphics in the final document. One of ‘left’, ‘right’, or ‘center’.
fig.cap NULL plots A character string to be used as a figure caption in LaTex.
fig.height 7 plots The height to use in R for plots created by the chunk (in inches).
fig.width 7 plots The width to use in R for plots created by the chunk (in inches).
echo TRUE results If FALSE, knitr will not display the code in the code chunk above it’s results in the final document.
results ‘markup’ results If ‘hide’, knitr will not display the code’s results in the final document. If ‘hold’,knitr will delay displaying all output pieces until the end of the chunk. If ‘asis’,knitr will pass through results without reformatting them (useful if results return raw HTML,etc.)
message TRUE results If FALSE, knitr will not display any messages generated by the code.
warning TRUE results If FALSE, knitr will not display any warning messages generated by the code.

— There are way too many of them, good luck !

3.4 Message and warning options

It is great that we have live code in our document, but results seems to be popluted with additional text. Those are “messages” from the package loading and some could be additional warning about casting values to different type. It depends, but generally suppressing message really helps with document apperance. I also tend to suppresss warnings, but this can cause issue in downstread analysis, be sure you understand your warnings before suppressing them.

Let’s add a couple of options to our code chunk and press Knit

```{r get_data, message = FALSE, warning = FALSE}

— Much better, yeah?

3.5 More code chunks and data wragnling

Let’s add a couple more chunks and wrangle our data a little more.

It is okay if you don’t understand some of R code in this example. The main point of these R chunks is to illustrate what can be done

## Getting and wrangling the data

```{r get_data}
make_url <- function(package) {
  paste0('<a href="https://cran.r-project.org/web/packages/', package, '">', package, '</a>')
}

library(magrittr)
library(dplyr)

avail_packages <- available.packages(contriburl = contrib.url("https://mirror.aarnet.edu.au/pub/CRAN/")) %>% 
                    as.data.frame() %>% 
                    as_tibble() %>% 
                    rowwise() %>% 
                    mutate(Depend = length(unlist(strsplit(Imports, split = ",")))) %>% 
                    ungroup() %>% 
                    select(Package, Version, Depend, Imports, License) %>% 
                    mutate(Package = make_url(Package),
                           Depend = ifelse(is.na(Imports), -1, Depend)) 
```

## Displaying table

```{r make_table, message=FALSE, warning=FALSE}
library(DT)
avail_packages %>% datatable(escape = FALSE)
```

## Plot the data

```{r plot}
library(ggplot2)
avail_packages %>% ggplot(aes(Depend)) + geom_bar()
```

— Amazing !

3.6 Session info

Let’s include session info

sessionInfo()

3.7 Setup chunk

While it is great to be able to configure each chunk individually, but sometimes setting up a “global” chunk can help with making document chunks more manageable and easier to read.

Let’s include a “setup” chunk (name of the chunk can be anything) and set some of the options globally for the entire document. Note that we can also move all of our libraries into that chunk. This may or may not be a good idea, once again all depends what you need to show the person you are communicate this too. Don’t forget to press Knit

Note that include option here set to FALSE meaning that chunk won’t be included at all into the final document, as oppose to eval = FALSE where chunk is included but not evaluated.

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

library(magrittr)
library(dplyr)
library(DT)
library(ggplot2)
```

— How about that?

3.8 Final look

Just in case you’ve got lost, this is how our second section should look like

# R code

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

library(magrittr)
library(dplyr)
library(DT)
library(ggplot2)
```

## Getting and wrangling the data

```{r get_data}

make_url <- function(package) {
  paste0('<a href="https://cran.r-project.org/web/packages/', package, '">', package, '</a>')
}

library(magrittr)
library(dplyr)

avail_packages <- available.packages(contriburl = contrib.url("https://mirror.aarnet.edu.au/pub/CRAN/")) %>% 
                    as.data.frame() %>% 
                    as_tibble() %>% 
                    rowwise() %>% 
                    mutate(Depend = length(unlist(strsplit(Imports, split = ",")))) %>% 
                    ungroup() %>% 
                    select(Package, Version, Depend, Imports, License) %>% 
                    mutate(Package = make_url(Package),
                           Depend = ifelse(is.na(Imports), -1, Depend)) 
```

## Displaying table

```{r make_table, message=FALSE, warning=FALSE}
library(DT)
avail_packages %>% datatable(escape = FALSE)
```

## Plot the data

```{r plot}
library(ggplot2)
avail_packages %>% ggplot(aes(Depend)) + geom_bar()
```