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.