rdatabasems-accessodbcdata-science

Error in odbcConnectAccess2007("BD-Embalses.mdb"). R could not find the function "odbcConnectAccess2007" after installing and loading RODBC


I'm a student of Data Science, and I'm working in a project to visualize a database of Spanish reservoirs; but I'm getting this error:

# package installations if required:
if(!require("tidyverse")) install.packages("tidyverse")
if(!require("janitor")) install.packages("janitor")
if(!require("patchwork")) install.packages("patchwork")
if(!require("RODBC")) install.packages("RODBC")

# load the packages
library(tidyverse)
library(readxl)
library(janitor)
library(patchwork)
library(RODBC)

# URL of the database
url <- "https://www.miteco.gob.es/content/dam/miteco/es/agua/temas/evaluacion-de-los-recursos-hidricos/boletin-hidrologico/Historico-de-embalses/BD-Embalses.zip"

# download
tempf <- tempfile() # temp file
download.file(url, tempf) 
unzip(tempf)

# open the connection with mdb
conn <- odbcConnectAccess2007("BD-Embalses.mdb")

I installed everything on my system (Arch linux), but I can't make the connection to the database for some reason (it says that the function could not be found). Everything is in Spanish, I'm just translating it into English.


Solution

  • As can be found in the win.R file of the RODBC package, odbcConnectAccess2007 is a Windows-exclusive function. It depends on an ODBC driver for MS Access being available, and these are only available for Windows.

    This means you need to find an alternative way, e.g. using mdbtools with the ODBC driver and either DBI + odbc (recommended) or RODBC, mdbtools + Hmisc::mdb.get, or RJDBC + UCanAccess.

    Side note, always download.file(url, tempf, mode = "wb") if you're downloading a non-text file. This would lead to errors further down the line.