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.
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.
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.
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With