```{r}
#| include: false
#| label: setup

library(emoji)
library(ggplot2)
library(knitr)
library(kableExtra)
library(DT)
```



## Code chunk

Open a new chunk code: `shift + ctrl + i` or <img width="10%" height="10%" src="img/new-chunk.png">

. . . 

```{r}
knitr::include_graphics("img/code.png")
```


# Chunk options 

## `echo`: Control the code display {.smaller .scrollable}


::::{.columns}

::: {.column width=50%}

Code executed & Shown

```
#| echo: true
head(rock)
```


```{r}
#| echo: true
head(rock)
```


:::

::: {.column width=50%}

Code executed & Not Shown


```
#| echo: false
head(rock)
```


```{r}
#| echo: false
head(rock)
```


:::
::::

. . . 

Code executed & entire chunk shown 


```
#| echo: fenced
head(rock)
```


```{r}
#| echo: false
head(rock)
```


## `eval` & `include`: Control the execution of the code


The code is not executed (but it is shown)


```
#| echo: true
#| eval: false
head(rock)
```


```{r}
#| eval: false
#| echo: true
head(rock)
```

. . . 

The code is executed, neither the code nor the results are show


```
#| include: false
head(rock)
```

::: {.callout-note}

## `include: false`

Super convient as first code chunk to load all the packages, data, preprocessing

:::





## Other options {footer="false"}

* `warning: true` 

    (default) the warnings obtained from the code executed in the chunk are displayed 
* `message: true` 
    
    (default) the messages associated to the code (e.g., when you upload a package with `library(package)`)
* `error` 
  + `error: false`
  
    (default) If there is an error in the code,  knitting stops and the document will not be generated until the error is fixed
  + `error: true` 

    If there is an error in the code,  the error message is shown in the document and the knitting goes on 


## YAML: Overall chunk options

Defined in the YAML, they are applied to all code chunks unless otherwise specified: 


```{markdown}
#| echo: true
[...]
execute: 
  echo: fenced
  message: true
  error: true
```


. . . 

Overruled by specific code-chunk options:


```
#| echo: false
head(rock)
```


```{r}
#| echo: false
head(rock)
```



## YAML: Show all code

```{markdown}
#| echo: true
#| code-line-numbers: "|4-7|"
[...]
format: 
  html:
    code-tools: 
     source: true
     toggle: false
     caption: This is my code
```

::: {.r-stack}

::: {.fragment .fade-in-then-out}
```{r}
include_graphics("img/show1.png")
```
:::

::: {.fragment .fade-in-then-out}
```{r}
include_graphics("img/show2.png")
```
:::
:::

## YAML: Code Fold 

```{markdown}
#| echo: true
#| code-line-numbers: "|6-7|6|7|"
[...]
execute: 
  echo: fenced
  message: true
  error: true
code-fold: true
code-summary: See this specific code
```


::: {.r-stack}

::: {.fragment .fade-in-then-out}
```{r}
#| out-width: 110%
include_graphics("img/fold1.png")
```
:::

::: {.fragment .fade-in-then-out}
```{r}
#| out-width: 110%
include_graphics("img/fold2.png")
```
:::
:::

# Images & Graphs

## External images: `.png` and `.jpg` {.smaller footer="false"}

:::{.panel-tabset}

## Code

`@fig-peacock1 illustrates a peacock`

```
#| out-width: 50%  # <1>
#| fig-align: center # <2>
#| fig-cap: A peacock living the life # <3>
#| fig-cap-location: bottom  # <4>
#| label: fig-peacock1       # <5>
knitr::include_graphics("img/peacock.png")
```

1. The width of the picture
2. The alignment 
3. The caption 
4. The caption location 
5. The label to use for cross-referencing

## Output

@fig-peacock illustrates a peacock

```{r}
#| echo: fenced
#| out-width: 50%
#| fig-align: center
#| fig-cap: A peacock living the life
#| fig-cap-location: bottom
#| label: fig-peacock
knitr::include_graphics("img/peacock.png")
```

:::

## Graphics {.smaller footer="false"}

::: {.panel-tabset}

## Code

`@fig-mtcars1 illustrates a plot`

```
#| out-width: 50%                                
#| fig-align: center                             
#| fig-cap: A graph from `mtcars`
#| fig-cap-location: margin
#| label: fig-mtcars1
ggplot(mtcars, aes(hp, mpg, color = factor(am))) +
  geom_point() +
  geom_smooth(formula = y ~ x, method = "loess") +
  theme(legend.position = 'bottom')
```



## Output


@fig-mtcars illustrates a plot

```{r}
#| echo: fenced
#| out-width: 50%
#| fig-align: center
#| fig-cap: A graph from `mtcars`
#| fig-cap-location: margin
#| label: fig-mtcars
#| code-line-numbers: "|2|3|4|5|6"
ggplot(mtcars,                                   
       aes(hp, mpg, color = factor(am))) +       
  geom_point() +
  geom_smooth(formula = y ~ x, method = "loess") +
  theme(legend.position = 'bottom')
```

:::


## Your turn

From the previous document

* Add an external image with `knitr::include_graphics()`, with a caption and a cross reference

* From the dataset of the course, the following plot, with a cross-reference and a caption: 

```{r}
#| echo: true
#| eval: false

ggplot(dataset, 
       aes(x = flexibility, y = anxiety, color = mindfulness)) +
  geom_point()
```

* The code used for the plot should be visible only if the person click on "Show code". All other code-chunk should not be visible

# Tables

## `kable` {footer="false"}


```{r}
#| echo: true

# knitr default
kable(head(mtcars))  # <1>
```

1. Basic table, the `kable()` function is in the `knitr` package

## 


```{r}
#| echo: true
#| eval: true
#| output: fragment

# kableExtra

# install.packages("kableExtra") # <1>
library(kableExtra)            # <2>
kable(mtcars) %>%              # <3>
  scroll_box(height = "450px", width = "100%") # <4>
```


1. Install package (Uncomment if you need to install the package)
2. Upload package
3. Produce the table
4. Add fancy stuff (height and with of the scroll box)



## Another option: DT


```{r}
#| echo: true
#| eval: false

# DT
# install.packages("DT")       # <1>
library(DT)                    # <2>
datatable(mtcars)              # <3>
```


1. Install package (Uncomment if you need to install the package)
2. Upload package
3. Print the table

```{r}
datatable(mtcars)
```


# Cross referencing 

## In general {footer="false"}

At this page [https://quarto.org/docs/authoring/cross-references.html](https://quarto.org/docs/authoring/cross-references.html) you can find all the details and help for a correct use of cross-referencing.

. . . 

    @obj-label
    
. . . 

YAML: 

```{markdown}
#| echo: true
[...]
lang: en
```


:::: [.columns]

::: {.column width="50%"}
| Language    | Code |
|-------------|------|
| English     | `en`   |
| Chinese     | `zh`   |
| Spanish     | `es`   |
| French      | `fr`   |
| Japanese    | `ja`   |
| German      | `de`   |
:::

::: {.column width="50%"}
| Language    | Code |
|-------------|------|
| Portuguese  | `pt`   |
| Russian     | `ru`   |
| Czech       | `cs`   |
| Finnish     | `fi`   |
| Dutch       | `nl`   |
| Italian     | `it`   |
| Polish      | `pl`   |
| Korean      | `ko`   |
:::

::::



## Images 

:::{.panel-tabset}

## Code

`@fig-pea illustrates a peacock`

```
#| out-width: 50%
#| fig-align: center
#| fig-cap: A peacock living the life
#| fig-cap-location: bottom
#| label: fig-pea
#| code-line-numbers: "|4-6|"
knitr::include_graphics("img/peacock.png")
```

## Output

@fig-mypea illustrates a peacock

```{r}
#| out-width: 50%
#| fig-align: center
#| fig-cap: A peacock living the life
#| fig-cap-location: bottom
#| label: fig-mypea
#| code-line-numbers: "|4-6|"
knitr::include_graphics("img/peacock.png")
```
:::


## Tables 


:::{.panel-tabset}

## Code

`@tbl-iris illustrates a table`

```
#| echo: fenced
#| label: tbl-iris
#| tbl-cap: "Iris Data"
#| code-line-numbers: "|2|3|"

kable(head(iris))
```

## Output

@tbl-iris1 illustrates a table

```{r}
#| label: tbl-iris1
#| tbl-cap: "Iris Data"

kable(head(iris))
```
:::

## Code

:::{.panel-tabset}

## Code `lst`

```
#| lst-label: lst-basicPlot
#| lst-cap: Basic use of the plot() function

plot(cars)
```


`@lst-basicPlot illustrates a basic use of the function plot()`

## Output

```{r}
#| eval: false
#| echo: true
#| lst-label: lst-basicPlot1
#| lst-cap: Basic use of the plot() function

plot(cars)
```

@lst-basicPlot1 illustrates a basic use of the function `plot()`


:::

## Equations

:::{.panel-tabset}

## Code

`@eq-standardize illustrates how to standardize a value`

```{markdown}
#| echo: true

$$
  z = \frac{x_i - \bar{x}}{s}
$$ {#eq-standardize}
```


## Output

@eq-standardize illustrates how to standardize a value

$$
  z = \frac{x_i - \bar{x}}{s}
$$ {#eq-standardize}


:::

## Your turn 

<br>

<br>

* Add a table based on the dataset, with a caption and a cross-reference
* The code used for the previous `ggplot` graph: 
  + Cross reference to the obtained graph 
  + Cross reference to the code used to obtain it 
* Add a collapsing callout block

# Margins 

## Content 

:::{.panel-tabset}

## Code

```{markdown}
#| echo: true

::: {.column-margin}
I want this picture displayed in the margin 


![](img/peacock.png)

:::
```


## Output 

```{r}
#| out-width: 100%
#| fig-align: center
include_graphics("img/margin-content.png")
```


:::

## Code 

:::{.panel-tabset}

## Code

```
#| column: margin

lm(hp ~ mpg*am, data = mtcars) # <1>

```

1. Fit a Linear Model with an interaction effect

## Ouput 

```{r}
#| out-width: 100%
#| fig-align: center
include_graphics("img/margin-code.png")
```
:::

## Graphs 

:::{.panel-tabset}

## Code

```
#| column: margin

ggplot(mtcars, 
       aes(hp, mpg, color = factor(am))) +
  geom_point() +
  geom_smooth(formula = y ~ x, method = "loess") +
  theme(legend.position = 'bottom')

```


## Ouput 

```{r}
#| out-width: 100%
#| fig-align: center
include_graphics("img/margin-graph.png")
```
:::


## Tables 

:::{.panel-tabset}

## Code

```{r}
#| echo: fenced
#| column: margin

datatable(mtcars,
  options = list(pageLength = 5))

```


## Ouput 

```{r}
#| out-width: 100%
#| fig-align: center
include_graphics("img/margin-table.png")
```
:::


## Multiple mixed outputs

:::{.panel-tabset}

## Code: fig-margin

```{r}
#| echo: fenced
#| eval: false
#| fig-column: margin



ggplot(mtcars, 
       aes(hp, mpg, color = factor(am))) +
  geom_point() +
  geom_smooth(formula = y ~ x, method = "loess") +
  theme(legend.position = 'bottom')

datatable(mtcars,
  options = list(pageLength = 5))

```


## Ouput: fig-margin

```{r}
#| out-width: 100%
#| fig-align: center
include_graphics("img/margin-mutliple-fig.png")
```
:::

## Multiple plots {.smaller footer="false"}


:::{.panel-tabset}

## Code 

`@fig-plotsModels illustrates different things. @fig-plotsModels-1 illustrates this, @fig-plotsModels-2 illustrates that and so on`


```
#| label: fig-plotsModels
#| column: screen-inset-shaded
#| layout-nrow: 2
#| layout-ncol: 2
#| fig-cap: "One dataset, different models" 
#| fig-subcap:
#|   - "The data"
#|   - "Polynomial"
#|   - "Linear Model"
#|   - "GLM with Poisson" 


ggplot(mtcars,
       aes(mpg, hp, size = gear)) +
  geom_point()

ggplot(mtcars,
       aes(mpg, hp, size = gear)) +
  geom_point() + 
  geom_smooth()

ggplot(mtcars,
       aes(mpg, hp, size = gear)) +
  geom_point() + 
  geom_smooth(method = "lm")
ggplot(mtcars,
       aes(mpg, hp, size = gear)) +
  geom_point() + 
  geom_smooth(method = "glm", method.args = list(family = "poisson"))


```


## Output

@fig-plotsModels1 illustrates different things. @fig-plotsModels1-1 illustrates this, @fig-plotsModels1-2 illustrates that and so on

```{r}
#| label: fig-plotsModels1
#| echo: false
#| warning: false
#| out-width: "60%"
#| column: screen-inset-shaded
#| layout-nrow: 2
#| layout-ncol: 2
#| fig-cap: "One dataset, different models" 
#| fig-subcap:
#|   - "The data"
#|   - "Polynomial"
#|   - "Linear Model"
#|   - "GLM with Poisson" 


ggplot(mtcars,
       aes(mpg, hp, size = gear)) +
  geom_point()

ggplot(mtcars,
       aes(mpg, hp, size = gear)) +
  geom_point() + 
  geom_smooth()

ggplot(mtcars,
       aes(mpg, hp, size = gear)) +
  geom_point() + 
  geom_smooth(method = "lm")
ggplot(mtcars,
       aes(mpg, hp, size = gear)) +
  geom_point() + 
  geom_smooth(method = "glm", method.args = list(family = "poisson"))


```

:::


## Multiple Tables  {.smaller footer="false"}


::: {.panel-tabset}

## Code 

`@tbl-tables presents two datasets: @tbl-tables-1 is cars and @tbl-tables-2 is pressure`

```
#| label: tbl-tables
#| tbl-cap: "Datasets"
#| tbl-subcap: 
#|   - "Cars"
#|   - "Pressure"
#| layout-ncol: 2

kable(head(cars))

kable(head(pressure))
```


## Output 


@tbl-tables1 presents two datasets: @tbl-tables1-1 is `cars` and @tbl-tables1-2 is `pressure`

```{r}
#| label: tbl-tables1
#| tbl-cap: "Datasets"
#| tbl-subcap: 
#|   - "Cars"
#|   - "Pressure"
#| layout-ncol: 2

kable(head(cars))
kable(head(pressure))
```


:::


## Your turn 

* Side by side: 
  + graph & table 
  + graph & graph (add `+ geom_smooth()` to the previous code)
  + Table & table

# Code annotation 

## In general

YAML: 

```{markdown}
#| echo: true
[...]
code-annotations: hover
```


. . . 

Options: 

* `below`: The annotation appears below the code

* `hover`: The annotation appears when the mouse hovers over the annotation marker

* `select`: The annotation appears when the annotation marker is clicked 

## An example


:::{.panel-tabset}

## Code

```{r}
#| echo: true
#| eval: false
#| results: hold
#| warning: false

## GGplot

mtcars %>%                               # .<1>
  ggplot( aes(mpg, hp, size = gear)) +   # .<2>
  geom_point() +                         # .<3>    
  geom_smooth(method = "lm")             # .<4>

```



`1. This does that`

`2. This other thing is this`

`3. And look at this!`

`4. Please have mercy` 

::: aside
Remove the dot between this symbol `#` and this symbol `<`
:::



## Output

```{r}
#| echo: true
#| eval: true
#| warning: false

## GGplot

mtcars %>%                               # <1>
  ggplot( aes(mpg, hp, size = gear)) +   # <2>
  geom_point() +                         # <3>    
  geom_smooth(method = "lm")             # <4>

```

1. This does that 
2. This other thing is this
3. And look at this!
4. Please have mercy 


:::



# Inline executable code

## In general

This allows for showing and executing code chunks directly in the text: 
```{markdown}
#| echo: true
The mean of the gear variable in mtcars is `r mean(mtcars$gear)`
```

. . . 

The mean of the gear variable in mtcars is `r mean(mtcars$gear)`

. . . 

If you want the display of the code but not its execution: 

```{markdown}
#| echo: true
This is the code to compute the mean `mean(mtcars$gear)`
```

. . . 

This is the code to compute the mean `mean(mtcars$gear)`



