Tables
This lesson is called Tables, part of the Going Deeper with R course. This lesson is called Tables, part of the Going Deeper with R course.
Transcript
Click on the transcript to go to that point in the video. Please note that transcripts are auto generated and may contain minor inaccuracies.
Your Turn
Copy the starter code below into RStudio and use it to make a table using
flextable
orgt
.Work on adjusting your column names, column width, number formatting, and anything else you might want to try out.
---
title: "Oregon Department of Education Diversity Report"
format: html
---
```{r}
library(tidyverse)
```
![](ode-logo.jpg)
This is a report for the [Oregon Department of Education](https://www.oregon.gov/ode/pages/default.aspx) on diversity in Oregon school districts.
> The Oregon Department of Education fosters equity and excellence for every learner through collaboration with educators, partners, and communities.^[https://www.oregon.gov/ode/about-us/Pages/default.aspx]
```{r}
# The code below will bring in the enrollment by race/ethnicity data
# It will then filter to only include one school district (Baker SD 5J)
# It then use select() to drop a few columns that we don't need
baker_enrollment_by_race_ethnicity <-
read_rds("https://github.com/rfortherestofus/going-deeper-v2/raw/main/data/enrollment_by_race_ethnicity.rds") |>
select(-district_institution_id) |>
select(year, district, everything()) |>
mutate(year = case_when(
year == "School 2021-22" ~ "2021-2022",
year == "School 2022-23" ~ "2022-2023",
)) |>
filter(district == "Baker SD 5J") |>
select(-c(district, number_of_students))
```
```{r}
# Use pivot_wider() to make the race/ethnicity groups into the columns (keep year in rows)
# Then create a table with the flextable or gt package
# Work on adjusting your column names, column width, number formatting,
# and anything else you might want to try out.
# YOUR CODE HERE
```
Learn More
If you want to learn more about pivot_wider()
, see Chapter 6 of R for Data Science.
If you really want to go deep on making good tables, check out the course Making Beautiful Tables with R or read Chapter 5 of my book R Without Statistics.
To learn more about the flextable
package, visit its documentation page. There is also a book that David Gohel, the package author, has created to show how flextable
works. And finally, there is a flextable
gallery, where you can see examples of nice tables made with the package (and the code used to make them).
To learn more about the gt
package, check out its documentation page.
Have any questions? Put them below and we will help you out!
Course Content
44 Lessons
You need to be signed-in to comment on this post. Login.
Karina Wheeler • November 29, 2023
Is there anyway to keep the caption for gt when making it interactive? The caption disappears when the gt table is made interactive.
Gracielle Higino Coach • December 1, 2023
Hi Karina! Apparently this is a bug that's yet to be solved by Quarto developers. I suspect it has something to do with how Quarto interprets tables. The gt documentation recommends adding headers and subheaders: https://posit.co/blog/new-in-gt-0-9-0-interactive-tables/ This would be a workaround to captions, and you could refer to the table object by the header index. I hope this helps!
Brian Slattery • November 30, 2023
Not sure if this is covered in the individual Making Your Reports Shine lessons, but if flextable is better for Word/Powerpoint and gt is better for web/interactive, is there a way to set a conditional to choose how to render the table depending on what the report is targeting? Or would you typically have to create two separate qmd files if you wanted to have the same report available in Word format but also on the web?
Gracielle Higino Coach • December 1, 2023
Hey! That's a great question - I'm glad we talked about it in the live session! Here's the direct link to the answer: https://muse.ai/v/CYHNT7A?t=420