Code

Topics

  1. Code chunks
  2. Inline code

What are code chunks?

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


1993 + 2023
[1] 4016
number=1993+2023
number
[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

Option Description
eval Evaluate the code chunk.
echo Include the source code in output
output `Include code output results (true, false, or asis)`
warning Include warnings in the output.`
error Include errors in the output (continues execution if error present).
include Catch all for preventing any output (code or results) from being included.
label Please make sure to label your code chunks! It helps with debugging.
fig-cap caption your figure
fig-width width of the figure
fig-height 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: Source editor: Type it out

Option 2: Visual editor: Insert > Code cell

Option 3: Shortcuts:

  • RStudio: Ctrl+Alt+I (or Cmd + Option + I on Mac)

Demo: echo option

echo hides (FALSE)/shows (TRUE) code and outputs results

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 controls whether the code is actually executed (TRUE) or just displayed without running (FALSE).

Code

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

Output

ex-original Output:

Demo: eval option

eval controls whether the code is actually executed (TRUE) or just displayed without running (FALSE).

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)

Code Chunk Exercise

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.

Inline Code Exercise