class: center, middle, inverse, title-slide # Essentials 05: File Paths & kable() Tables --- class: inverse # Session Objectives ### File Paths & kable() Tables - Recap file paths - Creating tables - knitr::kable() arguments - kableExtra functions --- class: inverse # Setup ### .orange[Tasks]: 1. Open the slides for today 1. Open/create your seminRs project 1. Check you have/create an r_docs folder 1. Check you have/create a data folder 1. Download the [Rmd document](https://and.netlify.app/seminr/05/essentials/essentials_05.Rmd) 1. Move this Rmd to the r_docs folder of your seminRs project & open it <br> .italic[Make sure to follow these exact steps & check your folder structure is correct!] --- class: inverse # File Paths ### Absolute file paths "C:/Users/danie/Documents/seminRs_21/data/data.csv" ### Relative file paths "../data/data.csv" When working in a R Markdown doc, you need to navigate from where that doc is saved <br> <br> <br> .italic[Reminder: to jump up a level we use ../] --- class: inverse # The handy here::here() function here::here() navigates to files starting where the R project is (usually minimises the need to jump up a level) ### .orange[Tasks]: 1. Install packages: here, knitr, & kableExtra in the console if you don't have them e.g. .orange[install.packages("here")] 1. Load tidyverse, knitr, kableExtra, & here in the pckgs chunk e.g. .orange[library(knitr)] 1. Take a look at the message that appears in the console after loading here ### Using here::here() & readr::read_csv() readr::read_csv(here::here("data/data.csv")) here::here("data/data.csv") %>% readr::read_csv(.) --- class: inverse # Path Practice ### .orange[Tasks]: 1. Download [data 1](https://and.netlify.app/seminr/05/essentials/data/data_1.csv) & [data 2](https://and.netlify.app/seminr/05/essentials/data/data_2.csv), save them/move them both to the data folder of your seminRs project 1. Following the examples below, load the first dataset using a relative path from your Rmd document & load the second dataset using the here::here() function <br> data_1 <- readr::read_csv(".orange[filepath.csv]") <br> data_2 <- here::here(".orange[filepath.csv]") %>% readr::read_csv(.) <br> .italic[Remember, if you need to jump up a level use ../] --- class: inverse # knitr::kable() Tables - knitr::kable() is a function for generating tables - Within all functions we can specify additional arguments/inputs, they take the general structure of: ```r function(argument_1 = something, argument_2 = something) ``` - Within kable(), we have a few different arguments that we can specify/change to alter our tables - Most useful are: col.names, caption, & digits --- class: inverse # knitr::kable() To create a table with no additional formatting: ```r knitr::kable(data) ``` <br> To create a table with modified column names, a caption, and specific decimal places: ```r knitr::kable(data, col.names = c("New Column Name 1", "New Column Name 2", "New Column Name 3"), caption = "This is the caption for my pretty table made with knitr::kable().", digits = 3) ``` --- class: inverse # kableExtra::kable_styling() - kableExtra extends the functionality of knitr::kable() to make our tables pretty 😍 - You can pipe the output of kable() into the styling functions of kableExtra - Most common function we're going to use is kableExtra::kable_styling() - The most useful arguments of the kable_styling() function are font_size, full_width, position, & bootstrap_options <br> Within kableExtra::kable_styling() we can specify these arguments with the following options: ```r font_size = 12 # any number to denote font size full_width = TRUE or FALSE position = "left", "right" or "center" bootstrap_options = "basic", "striped", "bordered", "hover", or "condensed" ``` --- class: inverse # knitr::kable() %>% kableExtra::kable_styling() ### Putting it all together ```r knitr::kable(data, col.names = c("New Column Name 1", "New Column Name 2", "New Column Name 3"), caption = "This is the caption for my pretty table made with knitr::kable().", digits = 3) %>% kableExtra::kable_styling( font_size = 10, full_width = FALSE, position = "right", bootstrap_options = "bordered") ``` ### .orange[PaRty Time!] Work through the tasks in the Rmd, & ask any questions as you go, essentially just play around, keep knitting to see your changes, & see what options are the most appropriate or the prettiest --- class: center, middle <div class="padlet-embed" style="border:1px solid rgba(0,0,0,0.1);border-radius:2px;box-sizing:border-box;overflow:hidden;position:relative;width:100%;background:#F4F4F4"><p style="padding:0;margin:0"><iframe src="https://uofsussex.padlet.org/embed/nrud4gk8x63gbfdc" frameborder="0" allow="camera;microphone;geolocation" style="width:100%;height:608px;display:block;padding:0;margin:0"></iframe></p><div style="padding:8px;text-align:right;margin:0;"><a href="https://padlet.com?ref=embed" style="padding:0;margin:0;border:none;display:block;line-height:1;height:16px" target="_blank"><img src="https://padlet.net/embeds/made_with_padlet.png" width="86" height="16" style="padding:0;margin:0;background:none;border:none;display:inline;box-shadow:none" alt="Made with Padlet"></a></div></div>