Introduction to Quarto and R

MVEN10 Risk Assessment in Environment and Public Health

Author

Ullrika Sahlin

Exercise overview

  • Work in pairs or alone.

Background

Calculations, simulations, data analysis and statistical analysis are common elements in risk assessments.

Being able to produce or read code supporting an assessment is a valuable skill when working as an expert or assessor.

Open source systems for coding and reporting are useful for collaborative work, reproducibility and external evaluation.

Purpose

To learn how to create a presentation in html format using Quarto and how to combine text, figures, equations and results from analysis into a report.

Content

  • Create a Quarto presentation in html format from R Studio cloud.

  • Use basic commands in R run from R Studio cloud.

  • Create a quarto report integrating text and results from running commands i R from R Studio cloud.

Duration

45 minutes

Reporting

No reporting required

References

https://quarto.org/

Quarto presentation

  1. Go to posit.cloud and register an account.

  2. Open a new project and call it “intro”.

  3. Click on the new file button (up to the left) and open a new Quarto Presentation.

  4. Assign a title, e.g. My report

  5. Assign yourself as author

  6. Click on Create

If you get a yellow ribbon asking you to install rmarkdown - Click install and wait. When finished, your window should look like this:

  1. Save the file as “testpresentation.qmd”

  2. Press Render

The program is running the qmd-file creating a html-file that is automatically opened in your browser.

Use the page down button or left/right arrow to change slide.

  1. Go back to the page with Your workspace intro

The code in testpresentation.qmd is currently shown as Visual.

  1. Change to Source.

You can enlarge the window by reducing the console window.

  1. Remove all text from line 8 and downwards

  2. Add the following text

## Slide 1

A probability is always between 0 and 1

## Slide 2

Probability can be interpreted as a

- theoretical probability

- frequency

- subjective probability

## Slide 3

Today we have learnt about 

### Uncertainty

It was *fun*

### Probability

It was even more **fun**

## Slide 4

Now I practise writing a math expression 

$\frac{m}{n}$

$\alpha$

I use two dollar signs to center it

$$X\cdot Y$$
  1. Press Render and look at the html-document for the presentation.

If you did not close it before, it should be in the browser.

Now you know how to create a presentation with headings, subheadings and bulletpoints.

Basic commands in R

Simple calculations

  1. Click on new file and open an R Script

R can work as a calculator

  1. On line 1, type 1 + 2 and press Run

You should see the result 3 in the Console

1 + 2
[1] 3

You can save the result from a calculation as an object

  1. Change the code on line 1 to be y = 1 + 2 and press Run

To see the value of y, you have to type it as well in the code an rerun or in the Console

y = 1 + 2
y
[1] 3

Simple plotting

Now let us look at how to create a plot

  1. Create a data frame consisting of two variables X and Y with 10 values each, where Y is positively associated with X.
Note

It is useful to denote variables with capital letters and use lower case letters for observations of this variable.

For example, x is an sample from X.

  1. Plot the sample from Y against the sample from X
x = runif(10)
y = 2*x + rnorm(10)
plot(x,y)

Nice visualisations of data makes a huge difference to a report. We will therefore demonstrate a way to generate the same plot using ggplot2.

  1. Install ggplot2 by typing install.packages(“tidyverse”) in the R Console. You might have to rewrite the quotation marks.

This installs several packages including ggplot2 that you will use later on. R-packages are libraries with functions and data sets designed for a specific purpose. Note that you will only have to do this one time in your work space.

  1. Load the library
library("ggplot2")
  1. Create a data frame with the observations and redo the plotting using ggplot2
df = data.frame(x=x,y=y)

ggplot(df,aes(x=x,y=y))+
  geom_point()

There will be time to explore ggplot later on in the course, but let us add a line fitted to the data.

ggplot(df,aes(x=x,y=y))+
  geom_point()+
  geom_smooth(method=lm,formula = y ~ x)

Now you can make a plot using R! More things will be introduced during exercises.

There are lot of resources for self studies on R. We recommend you to have a look at the W3schools’ tutorial on R Statistics after the exercise.

Quarto report

Now you will create a report using Quarto in which you combine text and outputs from code running in R.

  1. Create a new Quarto Document with the title “My Report”, you as the author and save it as “testreport.qmd”

  2. In the configuration section (also known as the YAML), add the text “date: today” as shown in the figure below.

  1. Press Render and view the generated html-file that appears in the browser.

  2. Go back and look at the testreport.qmd file. R-code is added as gray chunks. One can use to show or hide the code.

Let us now add our own information into the report.

  1. Remove everything from line 9 and downwards.

  2. Add the following text

## Summary

### Lessons learnt

Probability can be interpreted as a

- theoretical probability

- frequency

- subjective probability

### Skills gained

(@) Generate presentations and reports using Quarto

(@) Create and plot data from R, such as this
  1. Press Render to see what the report looks like

Let us now add the figure by adding the following code

library("ggplot2")
x = runif(10)
y = 2*x + rnorm(10)
df = data.frame(x=x,y=y)
ggplot(df,aes(x=x,y=y))+
  geom_point()+
  geom_smooth(method=lm,formula = y ~ x)

  1. Press Render and look at the report

Let us now hide the code by adding #| echo: false in the beginning of the r-chunk

  1. Press Render

Let us improve the accessibility of the report by adding a table of content.

  1. Change the YAML as shown below. The toc is the table of contents.

  1. Render

Isn’t this great!

Let us now end by adding an image to the report

  1. Download this image and upload it to your project in R Studio cloud
  1. Add the following text at the end of your testreport.qmd file
## Risk

*No matter what, it is difficult to save the world without acknowledging that risk involves our values and our uncertainties about the world.*

![](twobuttons.jpg){width=40%}
  1. Render and view your report.
Tip

You can integrate code and create figures in the same way in a Quarto presentation. The only difference is that there is a limitation on every slide.

Begin a new slide by ## [header of the slide]

To simplify sharing code and organise files during the course we here recommend to use folders

data for storing data

ex for storing .rmd files

img for storing images