Reading of various types of extensions

I did this to read CSV files and change column names.

I wanted to add the following statement; If the file I try to read is not CSV, it changes the reading for XLSX / DBF or DBC extension types.

How can I do this? Thanks

FormataArquivoEvento <- function(nome.arquivo,nomes.colunas) {
  data.frame <- read.csv2(nome.arquivo)
  colnames(data.frame) <- nomes.colunas
  data.frame %>% mutate_if(is.numeric,as.character) -> data.frame
  data.frame %>% mutate_if(is.factor,as.character) -> data.frame
  
  return(data.frame)
}

You can use the switch function to read the file differently depending on the extension.

library(tools)
library(readxl)

FormataArquivoEvento <- function(nome.arquivo, nomes.colunas) {
  extension <- tools::file_ext(nome.arquivo)
  switch(
    extension,
    "csv" = read.csv2(
      nome.arquivo, 
      colClasses = "character",
      col.names = nomes.colunas
    ),
    "xlsx" = readxl::read_xlsx(
      nome.arquivo,
      col_types = "text",
      col_names = nomes.colunas
    ),
    # Add other reading statements as needed
    stop("No reader function for file extension ", extension)
  )
}

A few unrelated comments about your code:

  • It's dangerous to assign variables which share names with existing objects, especially commonly used ones like data.frame.
  • Most reading functions let you specify what class the data should be. That way, you won't need to convert it afterward.
  • Similarly, most reading functions let you supply the column names up front.
  • Many people frown on rightward assignment (->). If you're the only person who'll look at the code, then it's no problem. But if others will read it, they may not notice a variable's being assigned.
3 Likes