Introduction to R and Rstudio

Session - quarto reporting

Zoë Turner

R Scripts v RMarkdown v Quarto

  • Scripts are very quick files of code with comments but RMarkdown and Quarto mix written text with code.

  • Quarto is a newer version of RMarkdown (2022) and is very similar for R users.

  • Quarto allows the possibility of working with colleagues who use other languages like Python.

Open a new Quarto file

Screenshot of File/New File/Quarto Document...

Render

Will open up a wizard to force the file to be saved.

As Quarto produces an output file it must have rights to save to the location.

Have a go!

  1. Change the title and then render.
  2. Go to the bottom of the existing code and in Visual mode create a header Introduction code and add a small table.
  3. Change the view to Source from Visual - what does the table look like in code?.
  4. Click on the wheel icon next to Render and in that menu select Preview in View Pane.
  5. Render the qmd.
08:00

Code chunks

The code sections in Quarto (and RMarkdown) are called chunks.

These are like smaller R scripts.

New code chunks

The default is for R chunks in R Studio but chunks can also be Python

  • Create a new R chunk either with the green button with a A picture of the green square with c button from RStudio to create a new chunk
  • Using Ctrl+Alt+i
  • In Quarto Visual type / and a drop down menu will appear, R chunk is the first so press Return
```{r}
37+9
```

Chunk options

Each chunk is a part of a longer script and needs to be run in order to render.

  • Switch off chunks To stop code running in any of the chunks code:
```{r}
#| eval: false

This will not run and break your code
```
  • Showing your code To include code in a report or presentation use #| echo: true

Have a go!

Let’s try to show code in a report:

  1. Create an R code chunk and type a sum like 37 * 6
  2. In the chunk add #| echo: false under the {r} and Render
  3. Change the code to true and Render

Extra time: Add another line for #| eval: false,
Render, change to true and Render

07:00

Global options settings

The default {r} settings are for echo: true and eval: true.

To switch these globally in YAML (yet another markup language) use execute and set the options to false

title: "My report"
format: html
execute:
    echo: true
    eval: true

Self contained

To create an html output that can be emailed without a folder with images and libraries:

title: "My report"
format: html
execute:
    echo: true
    eval: true 
embed-resources: true

Report output table of contents

Code can be added to the YAML to create an interactive table of contents (in html)

title: "My report"
format: 
  html:
    toc: true

Note that spaces in the YAML matter!

Naming chunks

Chunks can be named which makes them easier to navigate from the bottom left hand menu in the Editor.

#| label: my-chunk-name

Screenshot of RStudio bottom left corner of Editor with the table of contents and a the label code from a chunk behind the menu

End session