---
title: "IV\\. R Markdown (presentation)"
author: "Data Science Lab, University of Copenhagen"
date: today
format: 
  html:
    toc: true
    toc-depth: 3
    toc-location: left    
    embed-resources: true
    standalone: true
execute:
  warnings: true
  messages: true
  echo: true
---

This document does not include much, because we will simply illustrate some features along the way. You are encouraged to work with the exercises which include explanations. 

The presentation (but not this document, as just explained) will cover the following:

* Open new Quarto document, look at its structure. Render!

* Change some text, headers, etc.

* R chunks: run code without rendering, good practice

* Options in R chunks

* Example: `table1`

* Output formats: html, docx (Word), pdf

* Good practice: See the list in the end of the document with exercises

---

## Some simple commands

```{r}
11 * 5
sqrt(25)
plot(trees)
```

---

## Run code without knitting: Results shown inline or in Console

We recommend that you run and adopt your R code without knitting all the time. You can do that as usual (e.g. with Ctrl-Enter) or you can click the "Play" bottom to the far left in the R chunk. You can choose whether the results are shown in the Console/Plot window (numerical results/graphs) or inline in the Rmd file: _Tools_ &#8594; _Global options_ &#8594; _R Markdown_ &#8594; _Show output inline for all R Markdown documents_ (tick if off or not)

---

## Options in R chunks

It is possible to control if code and/or output is shown in the knitted document. 

First, some code with the default settings (both code and output shown): 


```{r}
reg <- lm(Volume ~ Girth, data=trees)
summary(reg)$coefficients
```

Then exactly the same code, but now with the code suppressed. This done with the option `echo: FALSE` (not visible in the output). The easiest thing is to insert such options via the small wheel in the upper right corner of the R chunk. 

```{r}
#| echo: false
reg <- lm(Volume ~ Girth, data=trees)
summary(reg)$coefficients
```

---

## Example 1: `table1`

The html output format plays well together with certain facilities for table generation. As an example, there is a function called `table1` which easily generates a table of statistics for variables of a dataset, possibly stratified after other variables in the dataset. The `table1` function is in a package with the same name. 

We first (install and) load the package and import the **downloads** data once again. (I inserted an option such that we don't get messages about loading of packages).

```{r}
#| message: false
# install.packages("table1")
library(table1)
library(readxl)
downloads <- read_excel("downloads.xlsx")
```

We then make an unstratified table:

```{r}
table1(~ size + time, data=downloads)
```

And finally a table stratified after machine name:

```{r}
table1(~ size + time | machineName, data=downloads)
```

Notice that the `table1` function generates nice html-output, but less nice pdf output unless the package `kableExtra` is also installed. The Word output is even worse if the package `flexTable` is not installed. You get a message about that if you have not installed the relevant package.

---

**End of presentation.**
