### Script saves copies of tables and charts to external files
# User must create folder/subdirectory called "TablesAndCharts"
# before running these scripts
#
### A. 1. Set the working directory, load required
### libraries, and read IPEDS data
#
rm(list = ls()) ### clear the environment
home <- "/blah-blah...blah-blah/PROJECT-AFFIRMATIVE-ACTION-LEGAL-CHALLENGE/"
setwd(home)
load(file="master3.rda")
#
### Developed using R = version 3.5.1 and R Studio = version 1.456
### install.packages("tidyverse")
library(tidyverse)
###library(RColorBrewer)
###install.packages("kableExtra")
library(knitr)
library(kableExtra)
#
### B. Source the local functions
#################################
#################################
#
saveCharts <- function(dt, chartName, isSummary=FALSE){
### Function saves all charts for each institution to .jpg files
#
if(isSummary) {
# Save a summary: chart = dt
chart = dt
fileName <- paste0("TablesAndCharts/", chartName, ".jpg")
savePlot(chart, fileName)
} else {
for (inst in dt){
chart <- inst[[chartName]]
shortName <- inst[["shortName"]]
fileName <- paste0("TablesAndCharts/", shortName, "-",chartName, ".jpg")
savePlot(chart, fileName)
}
}
}
#
savePlot <- function(chart, fileName){
print(chart)
ggsave(plot=chart, filename=fileName, device="jpeg", width=8, height=4, dpi=300)
dev.off()
}
#
saveTables <- function(dt, tableName, isSummary=FALSE) {
### Function calls Kable to save all tables for each institution to .html files
### ... or to save a standalone summary table
### Remember to manually add style section to html to add desired color
### to the rows ... don't use "stripe" option in saveKable function
#
setwd(paste0(home, "/TablesAndCharts/"))
if(isSummary) {
# Save a summary: table = dt
fileName <- paste0(tableName, ".html")
title <- paste0("<b>", tableName, "</b>")
table <- dt
saveKable(table, fileName, title)
#
} else { # Save tables for all institutions
for (inst in dt){ # save a table for each instituton in dt
table <- inst[[tableName]]
title <- paste0("<b>", inst[["institution"]], " Graduates, Class of 2016</b>")
shortName <- inst[["shortName"]]
fileName <- paste0(shortName, "-", tableName, ".html")
saveKable(table, fileName, title)
}
}
#
setwd(home)
}
#
saveKable <- function(table, fileName, title = NULL) {
### Function invokes Kable to create markdown for one table,
### then saves the table to filename
#
kable(table, caption=title) %>%
kable_styling(bootstrap_options = c( "hover",
full_width = TRUE, "responsive")) %>%
save_kable(file = fileName, self_contained = T)
}
#
#############################
############################
#
### C. Execute the script commands that call the functions
# Print and save final version of objects each institution
#
# Don't rush through the saves that are not isSummary
# They take a few moments. Rushing generates spurious
# error messages
saveCharts(listInstitutions, "chartGradRace")
# Some charts remove rows that are zeros or infinite... OK
saveCharts(listInstitutions, "chartGradRaceA2W")
saveCharts(chartSummaryGrads, "chartSummaryGrads", isSummary = TRUE)
saveCharts(chartSummaryNamesTop5AFAs, "chartSummaryNamesTop5AFAs", isSummary = TRUE)
#
saveTables(listInstitutions, "tableGradRaceOthers")
saveTables(listInstitutions, "tableGradRaceRaces")
saveTables(tableHarvardTech, "tableHarvardTech", isSummary=TRUE)
saveTables(tableSummaryGrads, "tableSummaryGrads", isSummary=TRUE)
saveTables(tableSummaryEnrollRaces,"tableSummaryEnrollRaces", isSummary=TRUE)
saveTables(tableSummaryEnrollOthers,"tableSummaryEnrollOthers", isSummary=TRUE)
#
save(file="master4.rda", listInstitutions, tableSummaryGrads, tableHarvardTech,
tableSummaryEnrollRaces, tableSummaryEnrollOthers, chartSummaryGrads)
This blog was established by the Digital Learning Lab to provide information that supports Black America’s efforts to close the Digital Divide. Its original focus on HBCUs has been broadened to include other colleges, universities, and community-based groups that enhance the computational thinking skills of Black Americans and the networks of successful Black techs who support each others’ efforts to achieve even greater success.
R-code master4
Subscribe to:
Posts (Atom)
No comments:
Post a Comment
Thank you!!! Your comments and suggestions will be greatly appreciated ... :-)