library(pacman)
p_load(tidyverse, pdftools, ggpage, inspectdf)
p_load(tidyverse, pdftools, ggpage, inspectdf)
library(pacman)
p_load(tidyverse, pdftools, ggpage, inspectdf)
download.file("https://viewfromll2.files.wordpress.com/2019/04/mueller-report.pdf", "mueller-report.pdf", mode = "wb")
# Table of contents
toc <- pdf_toc("mueller-report.pdf")
# Table of contents
toc <- pdf_toc("mueller-report.pdf")
toc
jsonlite::toJSON(toc, auto_unbox = TRUE, pretty = TRUE)
# Author, version, etc
info <- pdf_info("mueller-report.pdf")
info
# Table with fonts
fonts <- pdf_fonts("mueller-report.pdf")
# Table with fonts
fonts <- pdf_fonts("mueller-report.pdf")
fonts
report <- pdf_text("mueller-report.pdf")
# first page text
cat(report[1])
# second page text
cat(report[9])
fileConn <- file("mueller-report.txt")
writeLines(report, fileConn)
close(fileConn)
# renders pdf to bitmap array
bitmap <- pdf_render_page("mueller-report.pdf", page = 10)
# save bitmap image
png::writePNG(bitmap, "page.png")
jpeg::writeJPEG(bitmap, "page.jpeg")
mueller_report_csv <- "https://raw.githubusercontent.com/gadenbuie/mueller-report/ab74012b0532ffa34f3a45196d2b28004e11b9c2/mueller_report.csv"
mueller_report <- read_csv(mueller_report_csv)
mueller_report
mueller_pages <-
mueller_report %>%
# pad pages with fewer lines than expected
complete(
page,
line = 1:max(mueller_report$line),
fill = list(text = "")
) %>%
# Pre-process for {ggpage}
ggpage_build(
ncol = 30,
bycol = FALSE,
page.col = "page",
wtl = FALSE,
x_space_pages = 10,
y_space_pages = 100
) %>%
mutate(
color = case_when(
str_detect(word, "trump|president") ~ "Trump",
str_detect(word, "russia")     ~ "Russia",
str_detect(word, "cohen")      ~ "Cohen",
str_detect(word, "co(m|rn)ey") ~ "Comey",
str_detect(word, "flynn")      ~ "Flynn",
str_detect(word, "manafort")   ~ "Manafort",
str_detect(word, "sessions")   ~ "Sessions",
str_detect(word, "mcgahn")     ~ "McGahn",
TRUE ~ "normal"
),
color = factor(color, c(
"Trump", "Russia", "Cohen", "Comey",
"Flynn", "Manafort", "Sessions", "McGahn", "normal"
))
)
mueller_pages
# manually assigned colors from ggthemes::pal_calc()
colors <- rep("", length(levels(mueller_pages$color)))
names(colors) <- levels(mueller_pages$color)
colors["Trump"]    <- "#FF4023"
colors["Russia"]   <- "#004983"
colors["Cohen"]    <- "#FF922E"
colors["Comey"]    <- "#559B30"
colors["Flynn"]    <- "#4D276D"
colors["Manafort"] <- "#7BCAFD"
colors["Sessions"] <- "#7F1327"
colors["McGahn"]   <- "#FFD040"
colors["normal"]   <- "#d0d0d0"
ggpage_plot(mueller_pages) +
aes(fill = color) +
scale_fill_manual(
values = colors,
breaks = setdiff(names(colors), "normal")
) +
labs(fill = NULL, caption = "@grrrck") +
guides(fill = guide_legend(nrow = 1)) +
theme(legend.position = "bottom")
