Analysis

Topics

  1. Code Chunks
  2. Inline Code
  3. Sourcing Scripts

Code Chunks

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

Useful code chunk options:

  • #| eval: true: Evaluates code chunk.
  • #| echo: true: Includes source code in output.
  • #| output: true: Includes results of code execution in output.
  • #| warning: true: Includes warnings in output.
  • #| include: false: Catch all for preventing anything from being included.
  • #| label: something-meaningful: Good practice to always include!

Options for figure-creating code chunks:

  • #| fig-cap: "your figure's caption" : caption of the figure
  • #| fig-width: 8 : width of the figure
  • #| fig-height: 6 : height of the figure

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: Type it out

Option 2: Visual editor: Insert > Code cell

Option 3: Shortcuts:

  • RStudio: Ctrl+Alt+I
  • VS Code: Ctrl+Shift+I
  • Jupyter Lab: B

Exercise

Go to the Code Chunks chapter in the workshop book and 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: 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.


In Python (Quarto >= 1.4)1

```{python}
x = 5 + 5
```
The answer to 5 + 5 is `{python} x`

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 one of them to include the code in your Quarto document.

Go to the Sourcing code chapter in the workshop book and complete the exercise!

10:00

Other options

Other options not discussed here:

  1. Use parts of an external script within the Quarto document, for example to only run a few selected lines.
  2. Run a script before or after rendering a Quarto document to a manuscript: Quarto Docs: Project Scripts
  3. Use code chunks from existing Quarto documents or Jupyter notebooks: Quarto v1.4 Release
  4. Include entire Quarto documents or Jupyter notebooks within your current Quarto document: Quarto Docs: Includes