Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically read Access (.mdb) files into R for both Windows and Mac

Tags:

r

ms-access

I am trying to write an open data package that reads New York State education data into R. That data are provided as an Access database.

I want to write a function that downloads, reads, and imports those files, and I want it to be supported across platforms. The existing approach suggests installing a 32-bit version of R, which does not fit the bill for programmatic access.

A tour of #RStats suggests that this is a common pain point (1, 2, 3, 4)

On OS X/Linux, you can use mdb.get from the Hmisc package, provided that you have a third party library called mdb-tools. But this seems to be of little help for Windows.

An accepted answer will read the .mdb file linked above into R across Windows, OS X, and Linux.

like image 838
Andrew Avatar asked Jun 19 '16 22:06

Andrew


People also ask

How do I import an Access database into R?

A database table can be imported into R using either the sqlFetch() function or the sqlQuery() function, in much the same way as Excel spreadsheets are imported. The “school” table is imported using sqlFetch, and the “class” table is imported using sqlQuery.

What program reads MDB files?

An MDB file is a Microsoft Access database file. Open one with Access, MDBopener.com, or another database program. Convert to ACCDB, CSV, Excel formats, etc. with those same programs.


1 Answers

How about just with RODBC? Can you also download and use the mdb file (e.g. to make queries/views directly inside the mdb?)

I usually load data from Access dbs into R with the following code chunk:

# read in the data
library(RODBC)
db <- odbcDriverConnect("Driver={Microsoft Access Driver (*.mdb, *.accdb)};
                        DBQ=C:\\Path\\To\\Database\\my_db.accdb")

# Get data
data <- as_tibble(sqlFetch (db , "Table or Query Name", rownames=TRUE))
like image 76
DarwinsBeard Avatar answered Sep 18 '22 07:09

DarwinsBeard