How to read data from Microsoft Access .accdb database files into R?
The RODBC documentation suggests it is possible, b开发者_Python百科ut I am not sure how to read data from a Microsoft Access (the new .accdb
format) file with this package into R (on Debian GNU/Linux). The vignette talks about drivers, but I do not quite understand how I can see which drivers are installed, and in particular, if I have a driver installed for me to access those .accdb
files.
What code do you use to read data from .accdb
files? And please indicate what platform you are on and if you had to install a special driver.
To import a post-2007 Microsoft Access file (.accdb) into R, you can use the RODBC
package.
For an .accdb file called "foo.accdb" with the following tables, "bar" and "bin", stored on the desktop of John Doe's computer:
library(RODBC) #loads the RODBC package
dta <- odbcConnectAccess2007("C:/Users/JohnDoe/Desktop/foo.accdb") #specifies the file path
df1 <- sqlFetch(dta, "bar") #loads the table called 'bar' in the original Access file
df2 <- sqlFetch(dta, "bin") #loads the table called 'bin' in the original Access file
The title of the page you linked, RODBC: ODBC Database Access, may be misleading. Access doesn't mean MS Access; in that title access means connectivity. RODBC is an ODBC manager for R. It serves as the mediator to provide communication between R and the ODBC driver for your target database. So for GNU/Linux, you would still need an ODBC driver for MS Access database files ... RODBC doesn't provide one.
However, I don't know of any free (as in freedom and/or beer) MS Access ODBC drivers for Linux. Easysoft sells one, but it's not cheap. There may be offerings from other vendors, too; I haven't looked.
It might be easier to use a Windows machine to export your ACCDB to a format R can use. Or run R on Windows instead of Linux.
ODBC is a bit of 'plug and pray' system connecting different bricks.
RODBC allow you to get something from an ODBC provider into R. What you still need is the (for lack of a better word) ODBC-exporting driver of the database system in question. Which you need on your OS --- so I think with the Access-into-Linux combination you are without luck. Windows-only.
People have managed to access SQL Server using FreeTDS drivers (for the TDS protocol underlying Sybase and via an early license also MS-SQL) but it is usualluy a fight to get it going.
You'll need the drivers to connect Access to the ODBC interface. These should be on your system if you have Access installed. If not, download the Access Database Engine from Microsoft. Then create your data connection in ODBC (You may need to run the 32-bit c:\windows\sysWOW64\odbcad32.exe
if running 64-bit Windows). Note that this method doesn't work on GNU/Linux. The runtimes are Windows only, as mentioned by @HansUp below.
As for code, you'll probably start with odbcConnect(dsn, uid = "", pwd = "", ...)
, and the documentation can help with the details.
The best method that worked for me
#Package
library(RODBC)
#Defining the path
datab<-file.path("Main_File.accdb")
channel<-odbcConnectAccess2007(datab)
#reading the individual files inside the Main
table<-sqlFetch(Channel,"File_1")
This will fetch data from the "File_1" inside the Main_File.
But the above code did not support the UTF encoding.
library(RODBC)
db<-file.path("student.accdb")
channel<-odbcConnectAccess2007(db)
data<-sqlFetch(channel,"stud")
data
ID Name M1 M2 M3 M4 M5 Result
1 7 Radha 85 65 92 50 62 Pass
2 8 Reka 75 85 96 75 85 Pass
An alternative to directly accessing it might be to facilitate the data export from MS Access. At least the most recent MS Access allows to save the various export steps. One can then simply run the export of various queries / tables fairly quickly.
I know this does not answer the question, but might be a workaround if you do not get RODBC to run.
My solution (the most simple that I found):
- install "Access Database Engine" from Micosoft
- configure the connection to the Access data base (mdb or accdb) in Windows Administrative Tools, using the ODBC 32b tool. I's also possible to use c:\windows\sysWOW64\odbcad32.exe
- run RStudio in 32b mode ; it can be fixed in RStudio settings (relaunch RStudio after any change)
- finally, the RODBC functions work successfully.
CAUTION: it works only in Windows, not in linux. Personnally I use Windows as a Virtual Box guest within Xubuntu.
精彩评论