class: center, middle, inverse, title-slide .title[ # Statistics and Quantitative Methods (S1) ] .subtitle[ ## Week 4 — Workshop ] .author[ ### Dr Stefano Coretta ] .institute[ ### University of Edinburgh ] .date[ ### 2022/10/11 ] --- # Update the sqmf package Time to update the sqmf package (if you haven't already)! Run the following in the console: ``` r remotes::install_github("stefanocoretta/sqmf") ``` --- # Rmarkdown .pull-left[ **R scripts:** * They have the `.R` file extension. * You write code in the script. * You can add text as comments (lines starting with `#`). * Plots are shown in the `Plot` tab of the bottom-right panel. * It can look busy. ] -- .pull-right[ **Rmarkdown document:** * They have the `.Rmd` file extension. * You write text as you would in a common text file. * You write code inside "code chunks". * Plots are shown in the file, after their code chunk. * You can format text using the [markdown](https://www.markdowntutorial.com/) syntax. * You can render it to HTML or PDF to create analysis reports. ] --- layout: true # Create a new Rmarkdown (`.Rmd`) file --- .center[ ![:scale 60%](../../img/rmd-1.png) ] --- .center[ ![:scale 60%](../../img/rmd-2.png) ] --- .center[ ![:scale 60%](../../img/rmd-3.png) ] --- .center[ ![:scale 60%](../../img/rmd-4.png) ] --- .center[ ![:scale 60%](../../img/rmd-5.png) ] --- .center[ ![:scale 60%](../../img/rmd-6.png) ] --- layout: false class: center middle inverse # R MARKDOWN TUTORIAL --- # Instructions 1. Download an unzip [this archive](https://drive.google.com/drive/folders/1h5YAoabIk0vhBGpcU8cQdUmNZYNPuh3z?usp=sharing). 2. Create two folders in your RStudio project (if you haven't already): 1. `code` and `data`. 3. Move `04_lm_cat.Rmd` into the `code` folder in yout RStudio Project. 4. Move `alb_vot.csv` and `PDQ_WT_preprocessed.csv` to the `data` folder in your RStudio project. 5. Open `04_lm_cat.Rmd` in RStudio. <br> **NOTE**: Don't confuse RStudio with an RStudio project!!! RStudio is the software, a project is a folder with an `.Rproj` file. --- layout: false layout: true # Reading data --- So far we worked with **ready-made data** provided by the sqmf package. **But what about reading in your data?** -- .pull-left[ **Easy!** We can use the `read_csv()` function from the tidyverse. ] .pull-right[ .center[ ![:scale 60%](../../img/kara-kupfer-GzA2R5uvFKM-unsplash.jpg) ] ] ??? Photo by <a href="https://unsplash.com/@karakupf?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Kara Kupfer</a> on <a href="https://unsplash.com/s/photos/happy-pug?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Unsplash</a> --- layout: false class: center middle inverse background-image: url(../../img/scott-rodgerson-BwMcYuHI9OI-unsplash.jpg) background-size: cover .f1[ARE YOU IN AN<br>RSTUDIO PROJECT?] ??? Photo by <a href="https://unsplash.com/@scottrodgerson?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Scott Rodgerson</a> on <a href="https://unsplash.com/s/photos/warning?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Unsplash</a> --- layout: true # Reading data --- Fundamental concepts: - **Working directory**: - This is the directory (i.e. folder) which is set as the folder you are working from. - You can check this in R with `getwd()`. - When using RStudio projects, the working directory is your project's folder. -- - **File paths**: - A file path is the "address" of a file on your computer. For example: `"/Users/stefano/Documents/sqmf/script_1.R"` -- - There are **two ways** of specifying file paths: - **Absolute paths**: `"/Users/stefano/projects/voicing/code/analysis.R"` - **Relative paths**: `"./code/analysis.R"` - Relative paths are relative to the **working directory**. - The `"./"` part means "start from the working directory". --- layout: false class: center middle inverse # Please use RStudio projects and relative paths! --- layout: true # Reading data --- - Attach the tidyverse packages with `library(tidyverse)`. - The `read_csv()` function reads `.csv` files into R as a tibble (data frame, data table). - **NOTE** this is `read_csv()` NOT `read.csv()`. - It takes at least one argument: the `file` path (as a string). ``` r alb_vot <- read_csv("./data/alb_vot.csv") alb_vot ``` - `"./data/alb_vot.csv"` is a relative path. --- When working with file paths, you can use **RStudio auto-complete**: - Start by typing `read_csv("")`. - Then move the text cursor between the `""`. - Press `TAB` and a list of files and folders from the working directory will be displayed. - You can navigate the list with the arrow keys and ENTER. --- layout: false layout: true # Rmarkdown --- Rmarkdown files start with a **YAML preamble**. ```yaml --- title: "My first Rmd file" author: "Stefano" date: "2022-10-11" output: html_document --- ``` -- You specify some info or settings about the document in the YAML preamble using `key: value` pairs. --- Then you can start writing text using **markdown**! ```md # A title (H1) Some text, this is **bold**. ## Another title (H2) Some more *italics* text and a list: - Apples. - Pears. - Oranges. ``` --- .center[ ![:scale 50%](../../img/code-chunk.png) ] And code... in **code chunks**. - Code chunks are created by "fencing" code with **three backticks**. - Lines 26 and 28 in the picture above. - Everything between the lines with backticks must be working R code. -- - The opening backticks are followed by `{r pressure, echo=FALSE}`. -- - In the top-right corner of the code chunk there are three buttons: (1) Code chunk settings, (2) Run all code above, (3) Run the code of the current chunk. --- layout: false class: center middle inverse # NOW GO THROUGH THE RMARKDOWN TUTORIAL