I am attempting to import all of the tables within a relational MS Access database (.accdb) into R as separate data.frames. I have previously relied upon dbplyr and RMariaDB when accessing relational SQL databases stored online and I was under initial impressions that I could appropriate some of the methods to this .accdb, but I am becoming persuaded I was mistaken.
I have accomplished what I wanted to do with this code:
#### Libraries =================================================================
library(RODBC)
library(dplyr)
library(dbplyr)
#### Import database ===========================================================
# Define connection strings
dbq_string <- paste0("DBQ=", getwd(),"my_database.accdb")
driver_string <- "Driver={Microsoft Access Driver (*.mdb, *.accdb)};"
db_connect_string <- paste0(driver_string, dbq_string)
# Create .accdb connection
con <- odbcDriverConnect(db_connect_string)
#### Extract tables to data.frames
table1 <- sqlFetch(con, "tbltable1")
table2 <- sqlFetch(con, "tbltable2")
table3 <- sqlFetch(con, "tbltable3")
I am seeking advice on two topics here: 1.) Is there a tidyverse way of going about this? I've previously used code like:
table1 <- tbl(con, "tbltable1")
Table1 <- table1 %>% select(my_col) %>% collect()
is this not possible grammar for working through Access?
2.) Can my code where I am extracting tables to data.frames be refined by an apply-family function? I previously experimented with functions to get all of the table names (table_names) and pass it as an argument to an apply function to assign each table. I was under the impression that something like lapply(table1, sqlFetch, con)
would do it, but I receive an error indicating improper table connection.