+ - 0:00:00
Notes for current slide
Notes for next slide

Essentials 05: File Paths & kable() Tables

1 / 11

Session Objectives

File Paths & kable() Tables

  • Recap file paths
  • Creating tables
  • knitr::kable() arguments
  • kableExtra functions
2 / 11

Setup

Tasks:

  1. Open the slides for today

  2. Open/create your seminRs project

  3. Check you have/create an r_docs folder

  4. Check you have/create a data folder

  5. Download the Rmd document

  6. Move this Rmd to the r_docs folder of your seminRs project & open it


Make sure to follow these exact steps & check your folder structure is correct!

3 / 11

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




Reminder: to jump up a level we use ../

4 / 11

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)

Tasks:

  1. Install packages: here, knitr, & kableExtra in the console if you don't have them e.g. install.packages("here")

  2. Load tidyverse, knitr, kableExtra, & here in the pckgs chunk e.g. library(knitr)

  3. 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(.)

5 / 11

Path Practice

Tasks:

  1. Download data 1 & data 2, save them/move them both to the data folder of your seminRs project

  2. 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


data_1 <- readr::read_csv("filepath.csv")


data_2 <- here::here("filepath.csv") %>% readr::read_csv(.)


Remember, if you need to jump up a level use ../

6 / 11

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:

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

7 / 11

knitr::kable()

To create a table with no additional formatting:

knitr::kable(data)


To create a table with modified column names, a caption, and specific decimal places:

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)
8 / 11

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


Within kableExtra::kable_styling() we can specify these arguments with the following options:

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"
9 / 11

knitr::kable() %>% kableExtra::kable_styling()

Putting it all together

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")

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

10 / 11

Made with Padlet
11 / 11

Session Objectives

File Paths & kable() Tables

  • Recap file paths
  • Creating tables
  • knitr::kable() arguments
  • kableExtra functions
2 / 11
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
t Restart the presentation timer
?, h Toggle this help
Esc Back to slideshow