R/utilFinalAccountabilityCsvToDataFrame.R

utilFinalAccountabilityCsvToDataFrame <- function(year,
                                                  datafolder,
                                                  filePattern,
                                                  federativeBody,
                                                  layoutType,
                                                  csvLayouts,
                                                  toFile = T){

  message("Processing the data...")

  orig <- getwd()
  setwd(dataFolder)

  #setAs("character","br.date", function(from) as.Date(from, format="%d/%m/%Y") )

  dfData <- Sys.glob(sprintf(filePattern, year, body)) %>%
    lapply(read.table,
           colClasses = "character", # --> isso não deu certo: layouts$receitas_de_candidatos$Type
           header = T,
           sep = ";",
           na.strings = "#NULO",
           stringsAsFactors = F,
           fill = T,
           fileEncoding =  'windows-1252') %>%
    dplyr::bind_rows()


  dataLayout <- eval(parse(text = paste0("layouts$", layoutType)))

  #for(i in which(layouts$receitas_de_candidatos$Type=="numeric")) {
  for(i in which(dataLayout$Type == "numeric")) {
    dfData[,i] <- as.numeric(dfData[,i])
  }

  for(i in which(dataLayout$Type == "br.date")) {
    dfData[,i] <- as.Date(dfData[,i], format="%d/%m/%Y")
  }

  dfData$Ano.eleicao <- year

  setwd(orig)

  if (toFile){
    fileName <- paste0(paste(layoutType, year, sep = "_"), ".RDS")
    saveRDS(dfData, file = fileName)

    dfData <- data.frame(
      fileName = fileName,
      stringsAsFactors = F)
  }

  dfData
}
brunomssmelo/TseWrangler documentation built on May 13, 2019, 8:07 a.m.