Search code examples
rrodbc

Import data when tables have accented characters with RODBC


I am using R to import data with the RODBC package. The issue that I'm using an Excel file that has an accent in the table name and I need to figure out how to import the data without having to manually change the spreadsheet's name.

Below is a reproducible example of the problem:

download.file("http://www.secretariadoejecutivo.gob.mx/docs/datos_abiertos/Datos_abiertos_Victimas_Fuero_comun.xls", 
          destfile="./Victimas_Fuero_comun.xls",
          mode = "wb")

library(RODBC)

filePath <- "./Victimas_Fuero_comun.xls"
channel <- odbcConnectExcel2007(filePath)
sqlTables(channel)
DatosAbiertosVictimasFuero  <- sqlFetch(channel,
                            "Datos_abiertos_Víctimas_Fuero_c") # accent in 'Víctimas'
odbcClose(channel)
str(DatosAbiertosVictimasFuero )


sessionInfo()

R version 3.3.0 (2016-05-03)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252 LC_NUMERIC=C                               LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] RODBC_1.3-13

Solution

  • this worked

    library(readxl)
    
    filePath <- "./Victimas_Fuero_comun.xls"
    
    
    data <- read_excel(filePath, sheet = 1)