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

FundRmentals 11

Dr Danielle Evans | University of Sussex

1 / 11

Setup & Q&A

  • Open RStudio

  • Open your fundRmentals R Project (click the blue cube in the top right corner of RStudio)

  • Open a new Rmd file for today

  • In a new code chunk, load tidyverse, palmerpenguins, knitr, & kableExtra, using the library() command (install them in the console if you need to)

  • In another code chunk, create a peng_data_smry table by copying & running the below code:


peng_data_smry <- palmerpenguins::penguins %>% na.omit() %>% dplyr::group_by(., species) %>%
dplyr::summarise(., avg_mass = mean(body_mass_g), sd_mass = sd(body_mass_g),
avg_flipper_l = mean(flipper_length_mm),
sd_flipper_l = sd(flipper_length_mm), n = n())


2 / 11

Overview

  • kable tables

  • Code chunk options

  • Next steps
3 / 11

knitr::kable() tables

  • knitr::kable() is just one function we can use in R to create tables

  • For a very basic table we can use kable() like this:

knitr::kable(data)


species avg_mass sd_mass avg_flipper_l sd_flipper_l n
Adelie 3706.164 458.6201 190.1027 6.521825 146
Chinstrap 3733.088 384.3351 195.8235 7.131894 68
Gentoo 5092.437 501.4762 217.2353 6.585431 119


Task: try out knitr::kable() on our peng_data_smry, knit your doc to see it!

4 / 11

knitr::kable() tables

To make our table prettier, we can set some basic options within the kable() function, including col.names, caption & digits:


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)







Task: modify our kable table so that the column names are more presentable (with capital letters & spaces) & to have 2 decimal places

5 / 11

Solution

knitr::kable(peng_data_smry,
col.names = c("Species", "Mean Body Mass", "SD Body Mass", "Mean Flipper Length",
"SD Flipper Length", "Frequencies"),
digits = 2)


Species Mean Body Mass SD Body Mass Mean Flipper Length SD Flipper Length Frequencies
Adelie 3706.16 458.62 190.10 6.52 146
Chinstrap 3733.09 384.34 195.82 7.13 68
Gentoo 5092.44 501.48 217.24 6.59 119
6 / 11

kableExtra::kable_styling()

  • The kableExtra package extends the functionality of knitr::kable() to make our tables super pretty

  • You can pipe %>% the output of knitr::kable() into the styling functions of kableExtra

  • The most common function we're going to use is kableExtra::kable_styling()

  • The most useful arguments of kableExtra::kable_styling() are font_size, full_width, position, & bootstrap_options with the following options:


font_size = 12 # any number for font size
full_width = TRUE or FALSE
position = "left", "right" or "center"
bootstrap_options = "basic", "striped", "bordered", "hover", or "condensed"
7 / 11

Piping it all together %>%

To put it all together, we could write something like this:

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 = TRUE,
position = "right",
bootstrap_options = "striped")






Task: add in some kableExtra::kable_styling() to our kable table, choose any font_size you like, set full_width to be FALSE, position it in the center, and set bootstrap_options to be bordered, then knit your doc!

8 / 11

Solution

knitr::kable(peng_data_smry,
col.names = c("Species", "Mean Body Mass", "SD Body Mass", "Mean Flipper Length",
"SD Flipper Length", "Frequencies"),
digits = 2) %>%
kableExtra::kable_styling(
font_size = 12,
full_width = FALSE,
position = "center",
bootstrap_options = "bordered")


Species Mean Body Mass SD Body Mass Mean Flipper Length SD Flipper Length Frequencies
Adelie 3706.16 458.62 190.10 6.52 146
Chinstrap 3733.09 384.34 195.82 7.13 68
Gentoo 5092.44 501.48 217.24 6.59 119
9 / 11

Code Chunk Options

  • Our knitted file doesn't look so great with all our code included! 😢

  • But not to fear! We can easily change this by setting different code chunk options such as:

    • eval = FALSE (code is not run)
    • echo = FALSE (code is not shown, but the output is)
    • message = FALSE (messages are not shown)
    • warning = FALSE (warnings are not shown)
  • We can even use a mix of these in the same code chunk to get the desired output

An example code chunk with a name & eval = TRUE set

Task: set the code chunk options so that the code creating your tables is not shown in the knitted file & see how it looks!

10 / 11

You made it to the end!!

  • Well done for getting through to the end of this whirlwind couRse!! 🥳
  • You've all done amazingly and I'm very grateful for your patience and motivation (I know deep, deep down that maybe everyone doesn't absolutely love R... so huge well done for sticking with it!)
  • For supervising projects, students will have access to an R help desk and analyses workshops with me and Jennifer Mankin, they also have access to the R-DEX which you can refer them to if you get any 'How do I?' questions

FundRmentals feedback form

  • Please feel free to answer a few questions here about the course, and also comment any things you might want or find helpful in the future
  • You can be brutally honest, I'll try not to take it to heaRt 😄
11 / 11

Setup & Q&A

  • Open RStudio

  • Open your fundRmentals R Project (click the blue cube in the top right corner of RStudio)

  • Open a new Rmd file for today

  • In a new code chunk, load tidyverse, palmerpenguins, knitr, & kableExtra, using the library() command (install them in the console if you need to)

  • In another code chunk, create a peng_data_smry table by copying & running the below code:


peng_data_smry <- palmerpenguins::penguins %>% na.omit() %>% dplyr::group_by(., species) %>%
dplyr::summarise(., avg_mass = mean(body_mass_g), sd_mass = sd(body_mass_g),
avg_flipper_l = mean(flipper_length_mm),
sd_flipper_l = sd(flipper_length_mm), n = n())


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