Code

Topics

  1. Code chunks
  2. Inline code
  3. Sourcing scripts

What are code chunks?

  • Specified place for code
  • Executable
  • Weaved between between text
  • May print output or create new variables, for example


```{{r}}
#| echo: true
1993 + 2023
```
[1] 4016

Anatomy of a code chunk

  1. On each end of the code chunk: 3 backticks: ``` **
  2. Curly brackets: engine: {r} or {python} **
  3. Top of chunk: Code chunk options, with hashpipe: #|
```{r}
#| label: cars-r
#| echo: false
mtcars %>% 
  distinct(cyl)
```


```{python}
#| label: car-py
#| echo: false
distinct_cyl = mtcars.drop_duplicates(subset=['cyl'])
```

** not needed in a code cell in a Jupyter notebook!

Code chunk options

Code Chunk Options

Options for all code chunks: include in yaml (top of the document).

For example…

---
format: html
execute:
  echo: false
---

… hides all code in the output!

Inserting Code Chunks

Option 1: Source editor: Type it out

Option 2: Visual editor: Insert > Code cell

Option 3: Shortcuts:

  • RStudio: Ctrl+Alt+I

Demo: echo option

echo hides/shows code

Code

```{r}
#| label: ex-orig
#| echo: true 
x = 1:10; y = 11:20
plot(x, y)
```

Output

ex-original Output:

x = 1:10; y = 11:20
plot(x, y)

Demo: echo option

Code

```{r}
#| label: ex-hide-code
#| echo: false
x = 1:10; y = 11:20
plot(x, y)
```

Output

ex-hide-code Output:

Demo: eval option

eval creates results

Code

```{r}
#| label: ex-orig
#| eval: true
x = 1:10; y = 11:20
plot(x, y)
```

Output

ex-original Output:

Demo: eval option

eval shows code, but does not create results.

Code

```{r}
#| label: ex-not-run
#| eval: false
x = 1:10; y = 11:20
plot(x, y)
```

Output

ex-not-run Output:

x = 1:10; y = 11:20
plot(x, y)

Code Fold

Code
x = 1:10; y = 11:20
plot(x, y)

Exercise

Go to the analysis section on the website do the exercise.

You will be pasting some code from a dummy script into code chunks in your document!

15:00

Inline Code

Inline code: what & why?

Insert results or (quick) calculations into running text, without additional text formatting.

Why?

  • report and describe results reproducibly
  • minimizes errors: no manual copy-pasting results
  • automatic updating when data or code changes

Inline code

Enclose the R expression using `r `.

Code:


There are `r nrow(cars)` observations in our data. 

Output:

There are 50 observations in our data.

Important

If using Visual or Source mode, be advised the R expression will only substitute the value held by the variable when the Quarto document is rendered. That is, the value contained within the expression only appears in the output file.

Reference code chunk variables inline

Code:

```{{r}}
#| label: calc-values
#| echo: false
x = 1:10
x_mu = mean(x)
x_sd = sd(x)
```

The _mean_ of **x** is  `r x_mu` and
the _standard deviation_ is `r x_sd`.

Output:

The mean of x is 5.5 and the standard deviation is 3.02765.

Inline code: how?

In R

There are `r sum(penguins$Species=='Adelie')` 
penguins of the Adelie species in the dataset.
There are 152 penguins of the "Adelie" species in the dataset.

Exercise

Take a couple of minutes to play around with the possibilities of inline code!

Go to the Inline code chapter in the workshop book to complete the exercise.

10:00

Sourcing Scripts

Sourcing external files: what & why?

For example: R and Python scripts, .qmd or .ipynb files.


Why separate scripts from the manuscript?

  • minimizes errors: no manual copy-pasting code
  • easier code debugging
  • making the Quarto document easier to digest
  • modular coding = good coding practice

Sourcing external files: how?

.qmd files: use the chunk option #| file: "your-script.ext".
.ipynb files: import the script as module or run it.

R:

```{{r}}
#| file: "your-script.R"
#| eval: true 
```

Python:

```{{python}}
#| echo: false
%run your-script.py
```


Or: Add an empty __init__.py file to your scripts folder and run:

```{{python}}
#| echo: false
from your-script import function
```

Exercise

  • The workshop-materials contain 2 example scripts: do_addition.R and do_addition.py. You will use the R script to include the code in your Quarto document.

    • Go to the analysis section on the website and complete the exercise!
10:00