~hrbrmstr/epidata

ref: dc17d09a82427dfd2aadefdc7701fe872c6b320a epidata/R/union.r -rw-r--r-- 1.2 KiB
dc17d09ahrbrmstr fix CRAN errors 3 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#' Retreive Union Coverage
#'
#' The union coverage rate shows the percentage of the workforce covered by a collective
#' bargaining agreement.
#'
#' @return \code{tbl_df}
#' @references \href{https://www.epi.org/data/}{Economic Policy Institute Data Library}
#' @note Data source: CPS ORG | Hirsch and Macpherson (2003)
#' @return data frame
#' @export
#' @examples
#' if (interactive()) get_union_coverage()
get_union_coverage <- function() {

  params <- list(subject="unioncov")

  res <- epi_query(params)
  if (is.null(res)) return(data.frame())

  cols <- stringi::stri_trans_tolower(res$columns$name)
  cols <- stringi::stri_replace_all_regex(cols, "[\\('\\)]", "")
  cols <- stringi::stri_replace_all_regex(cols, "[[:space:]" %s+%
                                            rawToChar(as.raw(c(0xe2, 0x80, 0x93))) %s+% "-]+",
                                          "_")
  out <- setNames(as_data_frame(res$data), cols)
  out <- dplyr::mutate_all(out, "clean_cols")
  out <- suppressMessages(readr::type_convert(out))

  cite <- html_text(read_html(res$meta$source %||% "<p>Economic Policy Institute</p>"))
  message(sprintf('Note: %s\nCitation: "%s"', res$meta$notes %||% "None", cite))

  out

}