R/download_hansard.R

Defines functions download_hansard quiet

Documented in download_hansard

#' @noRd
quiet <- function(x) {
  sink(tempfile())
  on.exit(sink())
  invisible(force(x)) }

#' @importFrom utils download.file
#' @importFrom utils unzip
#' @importFrom ff file.move
#' @export
download_hansard <- function() {
  
  print("Replace the sample data with the full c19 Hansard corpus?")
  print("Press 1 to download.")
  print("Press 2 to exit.")
  
  input <- readline(prompt="Select Option: ")
  
  if(input == 1) { 
    hansardr_path <- paste0(.libPaths(), "/hansardr/")[1]
    
    url <- "https://smu.box.com/shared/static/34sxstotmquq2ud81r449nomlm0fcplp"
    fname <- paste0(hansardr_path, "hansardr_data.zip")
    
    if(file.exists(paste0(hansardr_path, "debate_metadata_1800.RData"))) { 
      if(file.info(paste0(hansardr_path, "debate_metadata_1800.RData"))$size > 900) {
        remove_samples(hansardr_path) } }
    
    tryCatch(
      expr = {
        download.file(url, fname, mode = "wb")
        zip_file <- paste0(hansardr_path, "hansardr_data.zip")
        unzip(zip_file, exdir = paste0(hansardr_path, "uncompressed_data"))
        
        hansard_subset <- list.files(paste0(hansardr_path, "uncompressed_data/data/"))
        to <- paste0(hansardr_path, "data/")
        
        for(file in hansard_subset) {
          file.move(paste0(hansardr_path, "uncompressed_data/data/", file), to) }
        
        unlink(paste0(hansardr_path, "hansardr_data.zip"))
        unlink(paste0(hansardr_path, "uncompressed_data"), recursive = TRUE) },
      
      error = function(e) {
        print("The c19 Hansard data cannot be downloaded. Please open a GitHub Issue if this problem persists.")
        print("https://github.com/stephbuon/hansardr/issues") } ) }
  
  else if (input == 2 ) { 
    invisible() } 
  else {
      print("Not a valid option. Exiting.") } } 
stephbuon/hansardr documentation built on March 1, 2023, 6:42 p.m.