R/cder.R

Defines functions .munge_cder .munge_cder_names .dictionary_cder_names

# https://repository.usaspending.gov/cder_library/authorized/search/keyword
#

.dictionary_cder_names <-
  function() {
    tibble(nameCDER = c("Data Element Name", "Release Date", "Definition", "Field Labels",
                        "Set", "Data Type", "Field Implementation", "List of Values",
                        "Minimum Length/Value", "Maximum Length/Value", "Instructions",
                        "Category", "Definition Source", "Form Name", "OMB Control Number",
                        "OMB Expiration Date", "Form Family", "Agency", "Form Description",
                        "Grant Common Form"),
           nameActual = c("nameElement", "dateReleased", "descriptionElement", "labelFields",
                          "nameSet", "typeData", "typeImplementation", "fieldsElement",
                          "minimumLengthElement", "maximumLengthElement", "elementInstructions",
                          "categoryElement", "sourceDefinition", "nameForm", "ombControl",
                          "dateOMBExpiration", "familyForm", "nameAgency", "descriptionForm",
                          "commonFormGrant")

    )
  }

.munge_cder_names <-
  function(data) {
    names_dict <- names(data)

    dict <- .dictionary_cder_names()
    actual_names <-
      names_dict %>%
      map_chr(function(name) {
        df_row <-
          dict %>% filter(nameCDER == name)
        if (nrow(df_row) == 0) {
          glue::glue("Missing {name}") %>% message()
          return(name)
        }

        df_row$nameActual
      })

    data %>%
      set_names(actual_names)
  }


.munge_cder <-
  function(data) {
    data <-
      data %>%
      .munge_cder_names()
    date_cols <- data %>% select(matches("date")) %>% names()

    data <-
      data %>%
      mutate_at(date_cols,
                function(x){
                  x  %>% substr(1, 10) %>% mdy()
                }) %>%
      .munge_data()

    data


  }

#' Dictionary CDER Elements
#'
#' Returns detailed information for all CDER elements.
#'
#' This contains tens of thousands of fields realting to the Standardized Data Element Repository
#'
#' @return
#' @export
#'
#' @examples
#' dictionary_cder()
dictionary_cder <-
  memoise::memoise(function(){
    data <-
      fread("https://asbcllc.com/r_packages/govtrackR/data/cdar.tsv.gz",showProgress = FALSE) %>%
      as_tibble() %>%
      .munge_data(clean_address = F)

    data <- data %>%
      mutate_if(is.character,
                list(function(x){
                  ifelse(x == "", NA_character_, x)
                }))
    data <- data %>%
      rename(isCommonFormGrant = commonFormGrant) %>%
      mutate(isCommonFormGrant = isCommonFormGrant == "YES")

    data <-
      data %>%
      mutate(idRow = 1:n())
    data
  })
abresler/govtrackR documentation built on July 11, 2020, 12:30 a.m.