Tables
This lesson is called Tables, part of the R in 3 Months (Spring 2026) course. This lesson is called Tables, part of the R in 3 Months (Spring 2026) 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.
Loading transcript...
View code shown in video
---
title: "Portland Public Schools Math Proficiency Report"
format: html
execute:
echo: false
warning: false
message: false
---
```{r}
library(tidyverse)
library(here)
library(flextable)
library(gt)
library(scales)
```
```{r}
third_grade_math_proficiency_wide <-
read_rds(here("data/third_grade_math_proficiency_dichotomous.rds")) |>
filter(district == "Portland SD 1J") |>
filter(
school %in%
c(
"Abernethy Elementary School",
"Ainsworth Elementary School",
"Alameda Elementary School",
"Arleta Elementary School",
"Atkinson Elementary School"
)
) |>
select(year, school, percent_proficient) |>
arrange(school) |>
pivot_wider(
id_cols = school,
names_from = year,
values_from = percent_proficient
)
```
{width=300px fig-align="center" fig-alt="Portland Public Schools logo"}
# Introduction
This is a report on math proficiency results in [Portland Public Schools (PPS)](https://www.pps.net/). The PPS mission statement is as follows:
> We provide rigorous, high-quality academic learning experiences that are inclusive and joyful. We disrupt racial inequities to create vibrant environments for every student to demonstrate excellence.^[https://www.pps.net/about/portland-public-schools-information/overview]
# Don’t Use the Default Output
```{r}
third_grade_math_proficiency_wide
```
# Flextable
```{r}
#| tbl-cap: Math proficiency among third graders in five Portland schools
third_grade_math_proficiency_wide |>
mutate(`2018-2019` = percent(`2018-2019`)) |>
mutate(`2021-2022` = percent(`2021-2022`)) |>
flextable() |>
set_header_labels(school = "School") |>
# align(j = 2, align = "center") |>
# width(j = 2, 4, unit = "cm") |>
# width(j = 3, 2, unit = "cm")
autofit()
```
# gt
```{r}
#| tbl-cap: Math proficiency among third graders in five Portland schools
third_grade_math_proficiency_wide |>
gt() |>
cols_label(school = "School") |>
cols_width(
school ~ px(100)
) |>
# cols_align(
# columns = `2018-2019`,
# align = "center"
# ) |>
fmt_percent(
columns = 2:3,
decimals = 1
)
```
# gt interactive
```{r}
third_grade_math_proficiency_wide_full <-
read_rds(here("data/third_grade_math_proficiency_dichotomous.rds")) |>
filter(district == "Portland SD 1J") |>
select(year, school, percent_proficient) |>
arrange(school) |>
pivot_wider(
id_cols = school,
names_from = year,
values_from = percent_proficient
)
```
```{r}
third_grade_math_proficiency_wide_full |>
gt() |>
cols_label(school = "School") |>
fmt_percent(
columns = 2:3,
decimals = 0
) |>
opt_interactive(
use_search = TRUE,
use_highlight = TRUE
)
```
Your Turn
Copy the starter code below into RStudio and use it to make a table using
flextableorgt.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)
```

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 5 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 as well as Creating beautiful tables by Albert Rapp.
Have any questions? Put them below and we will help you out!
Course Content
144 Lessons
You need to be signed-in to comment on this post. Login.