FRAMverse / framrsquared

R Package interfacing with the FRAM databases
Other
2 stars 1 forks source link

Connection issue #27

Open Ty-WDFW opened 1 month ago

Ty-WDFW commented 1 month ago

@sthurner11, you can describe your connection issue here and we'll try to resolve it.

sthurner11 commented 1 month ago

If I run this code NOT in a project, it works just fine. When in a project (project 'Database'), this code causes R to abort. So, it either has to do with using a project, or something about how my files are set up.... but it's weird it works totally fine when not in a project.

library(framrsquared)
library(tidyverse)
# Connect to Database -----------------------------------------------------
fram_db <- connect_fram_db('C:/Users/sthurner/Documents/FRAM/Coho/2024/Preseason Modeling/Database/2024NOFCohoFRAMdatabaseCODE.mdb') 

When in a different project (Project: 'Hoko BP Change'), this code works fine:

# From Ty Garber via email 9/12/24
library(framrsquared)
library(tidyverse)
# checks for differences in ER between two base periods
fram_db <- connect_fram_db('C:/Users/sthurner/Documents/FRAM/Coho/Hoko BP Change/Hoko/HokoCorrected.mdb')

Included photos of file structures for reference. Please ignore my messy and not up to standards file structures on these, it is on my to do list. Screenshot 2024-10-02 115109 Screenshot 2024-10-02 115139

Ty-WDFW commented 1 month ago

So framrsquared::connect_fram_db() is just a wrapper around DBI::dbConnect(), you can see that here. I'd imagine that it's not framrsquared but conditions of your local environment or DBI or the Access database itself causing the error.

You can take out framrsquared variable by just using the DBI::dbConnect() to connect to the database directly through DBI.

See if this will throw and error in your Database project:

db_path <- 'C:/Users/sthurner/Documents/FRAM/Coho/2024/Preseason Modeling/Database/2024NOFCohoFRAMdatabaseCODE.mdb'

con <- DBI::dbConnect(
        drv = odbc::odbc(),
        .connection_string = paste0("Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=", db_path, ";"))

If it does, it's not going to be framrsquared that's causing R to crash - you probably would want to update DBI then hit 'Compact and Repair' button in Access if updating DBI doesn't fix it.

Since you're in the project you can also try to use here::here() to construct the full path from relative path. Try running this and see if it works - maybe there's some weird white space in your path.

fram_db <- connect_fram_db(here::here('2024NOFCohoFRAMdatabaseCODE.mdb'))