#' get_personnel
#'
#' get_personnel returns a data frame of accountability personnel data from the
#' Nevada Report Card (NRC) API given a numeric vector of NRC organization ids and
#' spring school years. The function gets teacher ada, sub teacher counts and staffing counts.
#'
#' All of the data that can be pulled by this function is already available in a data frame
#' `nrc_personnel` that comes with the nrc package; View(nrc_personnel). This function was
#' used to create that data frame.
#'
#' @param org_ids A numeric vector of NRC organization ids. You can look them up for a school or
#' district by using get_org_id(name). These can be viewed by looking at the
#' nrc_orgs data frame that comes with the nrc package.
#' @param spring_years a numeric vector of spring school years.
#' For example, 2016 would be submitted for the 2015-2016 school year and
#' 2015:2016 would be provided to get both the 2014-2015 and 2015-2016 school years.
#'
#' @return returns a data frame of accountability personnel data.
#'
#' @export
#' @importFrom dplyr "%>%"
#'
#' @examples
#' # Get personnel data for CCSD schools for 2015-2016
#' ccsd_ids <- nrc_orgs$id[nrc_orgs$parent_id == 64827 | nrc_orgs$id == 64827]
#' ccsd_personnel_2016 <- get_personnel(ccsd_ids, 2016)
#'
#' # Get personnel data for the state for all years.
#' state_personnel <- get_personnel(64825, 2004:2016)
get_personnel <- function(org_ids, spring_years = 2018) {
# Check that valid org_ids were provided
valid_ids <- nrc_orgs$id
if (is.null(org_ids)) return(stop("Invalid org_ids provided. Must be an id or ids from nrc_orgs."))
for (id in org_ids) {
if (!id %in% valid_ids) {
return(stop("Invalid org_ids provided. Must be an id or ids from nrc_orgs."))
}
}
# Check that valid years were provided
valid_years <- 2004:2018
if (is.null(spring_years)) return(stop("Invalid spring_years provided. Must be 2004 through 2018."))
for (year in spring_years) {
if (!year %in% valid_years) {
return(stop("Invalid spring_years provided. Must be 2004 through 2018."))
}
}
# Add the nrc year codes to the scope (e12 is the code for the personnel report).
nrc_years <- nrc_scopes$code[nrc_scopes$value %in% spring_years]
scope <- paste(c('e12', nrc_years), collapse = '.')
# Get the collection id for the group of schools and districts provided.
col_id <- get_collection_id(org_ids)
# Scores parameter (Will include teacher ada, substitute teachers, and staffing)
scores <- '797,856,763,765,767,769,771,764,766,768,770,772,775,773,777,776,774,778'
api_url <- 'http://www.nevadareportcard.com/DIWAPI-NVReportCard/api/rosterCSV?report=reportcard_1&organization='
target <- paste0(api_url, col_id, '&scope=', scope, '&scores=', scores, '&fields=309,310,311,313,318,320')
# Download the csv data from the target url.
resultsText <- RCurl::getURL(target)
# Populate a data frame from the csv results and format the column headers.
results_df <- dplyr::as_data_frame(readr::read_csv(resultsText)) %>%
dplyr::select(name = Name,
accountability_year = `Accountability Year`,
school_levels = `School Levels`,
organization_id = `Organization ID`,
organization_level = `Organization Level`,
state_id = identifier,
grade_levels = `Substitute Teachers - Grade Level`,
teacher_ada_rate = `Teacher Average Daily Attendance`,
long_term_subs_elementary = `Substitute Teachers - Elementary Long Term`,
short_term_subs_elementary = `Substitute Teachers - Elementary Short Term`,
long_term_subs_math = `Substitute Teachers - Mathematics Long Term`,
short_term_subs_math = `Substitute Teachers - Mathematics Short Term`,
long_term_subs_science = `Substitute Teachers - Science Long Term`,
short_term_subs_science = `Substitute Teachers - Science Short Term`,
long_term_subs_social_studies = `Substitute Teachers - Social Studies Long Term`,
short_term_subs_social_studies = `Substitute Teachers - Social Studies Short Term`,
long_term_subs_english = `Substitute Teachers - English Long Term`,
short_term_subs_english = `Substitute Teachers - English Short Term`,
admin_count = `Staff - Administrators #`,
admin_rate = `Staff - Administrators %`,
teacher_count = `Staff - Teachers #`,
teacher_rate = `Staff - Teachers %`,
other_staff_count = `Staff - Other #`,
other_staff_rate = `Staff - Other %`) %>%
dplyr::rowwise() %>%
# Remove the dash and id number from the name.
dplyr::mutate(name = ifelse(stringr::str_detect(name, ' - [0-9]'),
stringr::str_sub(name, 1, stringr::str_locate(name, ' - [0-9]')[1] - 1), name))
# Convert the pertinent columns to numeric.
results_df[,8:ncol(results_df)] <- sapply(results_df[, 8:ncol(results_df)], as.numeric)
# Transform the rate columns from a whole number to a decimal.
results_df[,grepl('rate', names(results_df))] <- results_df[,grepl('rate', names(results_df))] / 100
return(results_df)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.