简体   繁体   中英

Merging multiple ACCESS files in R

I am trying to merge different ACCESS files (located in different subfolders under the same folder). After reading through different blogs and some of the questions people have asked here, I came up with the code below.

Parent.Folder<-"D:/Documents/Rwd"
setwd(Parent.Folder)
library(RODBC)
subfolders<-list.dirs(Parent.Folder, recursive = TRUE)[-1]
list_filenames<-list.files(subfolders, recursive = FALSE)
list_filenames<-paste("D:/Documents/Rwd/", list_filenames, sep="")
list_filenames<-sub(".mdb","",list_filenames)
for (file in list_filenames){
  if(!exists("data")){
    channel<-odbcConnectAccess(file)
    data<-sqlQuery(channel,paste("select* from HRESULTS"))
    close(channel)  
    }

  if(exists("data")){
    channel<-odbcConnectAccess(file)
    temp_data<-sqlQuery(channel,paste("select* from HRESULTS"))
    data<-rbind(data,temp_data)
    rm(temp_data)
    close(channel)  
  }
}

All tables are in the same format and my aim is to get them in one data frame. The first if statement ran fine and gave me a data frame with data from first file but after that I am getting the error "Error in sqlQuery(channel, paste("select* from HRESULTS")) : first argument is not an open RODBC channel" I have never worked with ODBC or SQL before so my apologies if I am making a very obvious mistake. Any help will be greatly appreciated. Thanks!

Being a file-level database system like SQLite, MS Access files require full path references including extensions in ODBC connections.

For your needs, consider using the list.files argument full.names = TRUE and then filter the list for only viable access extensions, .mdb or .accdb. Finally, consider using lapply for a growing list of imported query data tables that you can later run a do.call(rbind...) to concatenate to final dataframe:

Parent.Folder <- "D:/Documents/Rwd"
subfolders <- list.dirs(Parent.Folder, recursive = TRUE)[-1]

list_filenames <- list.files(subfolders, full.names = TRUE, recursive = FALSE)
acc_files <- list_filenames[grep(".mdb|.accdb", list_filenames)]

output <- lapply(acc_files, 
                 function(file) {
                    channel <- odbcConnectAccess(file) 
                    # ALTERNATIVE CONNECTION
                    # channel <- odbcDriverConnect(paste0('driver=
                    #            {Microsoft Access Driver (*.mdb, *.accdb)};
                    #            DBQ=', file))                        
                    data <- sqlQuery(channel, "select * from HRESULTS")
                    close(channel)
                    return(data) 
                })

data <- do.call(rbind, output)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM