Reproducible HTML reports

Ambra Perugini

Getting started (YAML)

---
title: "Report analisi"
author: "Nome Cognome"
date: "`r Sys.Date()`"
bibliography: bibliography.bib
output: html_document
embed-resources: true
---

YAML Parameters

  • toc: true (table of content)

  • toc-title: Contents

  • toc-location: left

  • number-sections: true

  • embed-resources: true (to have an automomous file outside of the folder)

  • execute: you can specify defoult cunck settings

  • cache: ture (saves chunks output to compile faster)

  • results: hide

  • echo: false (it does not show the chunk code)

  • code-fold: true (nice pop up window with code)

i.e.:

---
title: "my html"
  format:
    html:
      toc: true
      toc-title: Contents
      toc-location: left
      number-sections: true
      embed-resources: true
execute:
  cache: true
  results: hide
  echo: false
---

Chunks

Example of an r chunk:

```{r}

#| results: hide

#| echo: false

#| label: fig-name_figure

#| fig-width: 8

#| fig-height: 4

#| fig-cap: Caption of you figure.

```

Columns

Content 1:

  • Item 1

  • Item 2

  • Item 3

Content 2:

  • Item 1

  • Item 2

  • Item 3

:::: {.columns}

::: {.column width="50%"}


:::

::: {.column width="50%"}


:::

::::

Colored text and images

I love Quarto!

I <span style="color:red;">love</span> Quarto!

Hoe to insert images:

![](figures/magic.png){fig-align="center"}

Adding citations (pt.1)

I liked this article a lot and it was written by Scheel et al. (2021), (Scheel et al., 2021).

@scheel2021hypothesis for in-text citation
[@scheel2021hypothesis] for between brakets citation

References are then automatically listed in full at the end of the document.

To create a .bib file, open a new text file and save it with the .bib extension.

Remember to include the following entry in the YAML header:

bibliography: "file name"

csl: "apa.csl"

Adding citations (pt.2)

The CSL (Citation Style Language) file provided in the folder allows you to format citations correctly according to APA style.

Valori nel testo presi da oggetti r

```{r}
library(readxl)
DATA <- read_xls(path = "data/Sara_dataset.xls")

cor_pre <- cor(DATA$anx_pre, DATA$mathquiz, use = "complete.obs")


cor_pre
```
[1] -0.05491077

Correlations:

  • Correlation between math and anxiety -> \(r =\) -0.0549108
- Correlation between math and anxiety -> $r =$ `\r cor_pre\`
  • -0.0549108
- `\r cor(DATA$anx_pre, DATA$mathquiz, use = "complete.obs")`

Panels (visual)

library(criticalESvalue)
library(psych)
library(psychTools)
data("holzinger.swineford")
Holz <- holzinger.swineford
tt <- t.test(Holz$t01_visperc [ Holz$female == 1], Holz$t01_visperc [ Holz$female == 2])
critical(tt)

    Welch Two Sample t-test

data:  Holz$t01_visperc[Holz$female == 1] and Holz$t01_visperc[Holz$female == 2]
t = 1.4095, df = 298.9, p-value = 0.1597
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -0.06419229  0.38823118
sample estimates:
mean of x mean of y 
 4.314090  4.152071 

|== Effect Size and Critical Value ==| 
d = 0.1623665 dc = ± 0.2269609 bc = ± 0.2262117 
g = 0.1619587 gc = ± 0.2263909 
m1 <- 10
m2 <- 15
sd1 <- 5
sd2 <- 4.25
n <- 30
critical_t2sp(m1 = m1, m2 = m2, sd1 = sd1, sd2 = sd2, n = n)
$dz
[1] -0.7619393

$dzc
[1] 0.3734061

$d
[1] -1.077545

$dc
[1] 0.528076

$bc
[1] 2.450367

$se
[1] 1.198089

$df
[1] 29

$g
[1] -1.049394

$gc
[1] 0.51428

$gz
[1] -0.7420336

$gzc
[1] 0.3636509

Panels (source)

::: panel-tabset
### From data (code)
#| echo: true
#| results: hide

library(criticalESvalue)
library(psych)
library(psychTools)
data("holzinger.swineford")
Holz <- holzinger.swineford
tt <- t.test(Holz$t01_visperc [ Holz$female == 1], Holz$t01_visperc [ Holz$female == 2])
critical(tt)
### From data (output)
#| echo: false
#| results: markup

library(criticalESvalue)
library(psych)
library(psychTools)
data("holzinger.swineford")
Holz <- holzinger.swineford
tt <- t.test(Holz$t01_visperc [ Holz$female == 1], Holz$t01_visperc [ Holz$female == 2])
critical(tt)
### From sample size (code)
#| echo: true
#| results: hide

m1 <- 10
m2 <- 15
sd1 <- 5
sd2 <- 4.25
n <- 30
critical_t2sp(m1 = m1, m2 = m2, sd1 = sd1, sd2 = sd2, n = n)
### From sample size (output)
#| echo: false
#| results: markup

m1 <- 10
m2 <- 15
sd1 <- 5
sd2 <- 4.25
n <- 30
critical_t2sp(m1 = m1, m2 = m2, sd1 = sd1, sd2 = sd2, n = n)
:::

Exercise

  • Download the “Sara_dataset.xlsx” dataset.
  • Describe the variables using descriptive statistics.
  • Analyse two of the variables in any way you see fit.
  • Insert a two-column section.
  • Include a word in a different color.
  • Insert an image.
  • Add citations using the .bib file.
  • Write a short concluding paragraph summarizing the work.

Extra:

  • Reference objects from the R environment in the text.
  • Create a figure using the data.
  • Add fig-cap.
  • Add label.
  • Optional: adjust the image size in the code chunk.

Sara_dataset Variables

  • Participants’ gender (gender: 1 = female, 2 = male)
  • Type of undergraduate degree obtained (major: 1 = psychology, 2 = medicine, 3 = biology, 4 = sociology, 5 = economics)
  • Experimental condition (cond: 1 = easy, …, 4 = impossible)
  • Self-reported fear of mathematics (phobia), measured on a 0–10 scale
  • Number of mathematics courses previously attended (prevmath)
  • Score on a mathematics pre-test (mathquiz)
  • Score on a statistics test (statquiz)
  • Self-measured heart rate under normal conditions (hr base), before the test (hr pre), and after the test (hr post)
  • Scores on an anxiety test under normal conditions (anx base), before the test (anx pre), and after the test (anx post)

CSS (extra)

What is a CSS file?

  • CSS (Cascading Style Sheets) is a language used to format documents written in HTML (also applicable to Reveal.js presentations).

  • CSS files have the .css extension and contain style rules that can be applied to HTML elements.

  • It separates content from presentation and formatting, improving code maintainability.

How to include it in a Quarto file:

format:
  html:
    css: stile.css

Illustrative example

A .css file is like a text document where you define the formatting settings for your HTML file. Here’s a very brief example:

body {
  font-family: Arial, sans-serif;
  background-color: #f4f4f4;
}
h1 {
  color: darkred;
  text-align: center;
}

CSS inside Quarto

References

Scheel, A. M., Tiokhin, L., Isager, P. M., & Lakens, D. (2021). Why hypothesis testers should spend less time testing hypotheses. Perspectives on Psychological Science, 16(4), 744–755.