---
title: "Creating a PDF report"
subtitle: "How to turn your data analysis work into a quick, publishable report"
format:
  typst:
    toc: true
    number-sections: true
execute:
  echo: false
---

# About this notebook

This notebook is a quick **how-to on producing a PDF report** out of a data analysis workflow, introducing key concepts on how to specify the report format, and report section elements.

::: markdown-text
**Before getting started**

If you have just opened this file inside RStudio, you can click on the *Visual* button in the top left corner of the notebook window (next to *Source*). This will allow you to work in a notebook-style environment with clearly visible text, code, and output sections.
:::

## 1. What is Quarto?

**Quarto** lets you combine text, code, figures and tables in a single document (with the `.qmd` file extension/format), that we refer to as a *notebook.*

[You can read more about Quarto here.](https://quarto.org/)

The basic steps are:

1.  Create a `.qmd` file.
2.  Write your text and code.
3.  At the top of your file, in the YAML section, set `format: typst`.
4.  Render the document.
5.  Quarto produces a PDF.

## 2. Save your `.qmd` file

::: callout-warning
Remember, best practice is to **set up a project folder** where you store your `.qmd` notebook, data, and outputs, before starting your analysis.

That way, you can easily find all of our project documents and data in one place, and we can set our R session working directory to this folder (**Session \> Set Working Directory \> Choose Directory**). See the example below.
:::

::: .text
my_restoration_project/ \
├── data/ \
└── outputs/
:::

##  3. Edit YAML

At the top of the file, Quarto automatically adds a basic YAML header like this:

``` yaml
---
title: "Untitled"
format: html
---
```

We want to add some specifications so Quarto knows exactly what kind of report we want to create. We can specify the format using `format: typst`, to tell Quarto to create a PDF using *Typst.*

**Typst** is a typesetting system that Quarto can use to produce PDF documents without requiring installations of other software or distributions.

Typst lets you customise your PDF report styling and layout in lots of flexible and visually appealing ways. [Read more about how to customise your report with YAML specifications here.](https://quarto.org/docs/output-formats/typst.html) See below for a quick example.

We can also tell it to include a table of contents (`toc: true`), number our report sections (`number-sections: true`) and how many columns we want (`columns: 2`).

---
title: "Untitled"
author: "Untitled"
format:
  typst:
    toc: true
    number-sections: true
    columns: 2
---

We can also render our Quarto report to other format types, like HTML. Simply replace `format: typst` with `format: html`.

## 4. Add text

Write normal text underneath the YAML header. For example:

This report explores vegetation change across the study area. The analysis uses satellite-derived vegetation indices and simple summary statistics.

Use `#`, `##`, and `###` to create headings.

## 5. Add R code

Code goes inside a code cell. Code cells are always 'fenced' by three backticks like the below example.

A quick short-cut to adding a code-cell is `ctrl+alt+i`.

```` markdown
```{r}
x <- c(10, 15, 12, 18, 20)

mean(x)
```
````

When the document is rendered, Quarto runs the code and includes the output. You can hide the code and show only the output using: `#| echo: false` at the top of the code cell.

```` markdown
```{r}
#| echo: false

mean(x)
```
````

## 6. Render the PDF

Save your `.qmd` file and render it.

**From RStudio:**

Click Render - you will see a button 'Render' with a small blue arrow next to it, at the top left of the page ribbon-bar.

Quarto will create your pdf report and save it to your project folder.

## TEMPLATE

Once you understand the basic structure, you can start future reports from this template:

```` markdown
---
title: "My Report"
author: "Your Name"
format:
  typst:
    toc: true
    number-sections: true
---

## Introduction

Write your intro here.

## Data

Describe your data here.

```{r}
# Load packages
library(tidyverse)
```

```{r}
# Read data
data <- read_csv("data/my_data.csv")

head(data)
```

## Results

Describe your results here.

```{r}
# Example analysis
summary(data)
```

## Conclusion

Summarise the main findings here.
````
