This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Inquiry related to processing multiple db file simulataneously

Hello,

I am trying to create a dataframe from many different model file. I want to create dataframe for each tissue with it respective model file. So my first step is to input the .db file and dbgetquery to convert it into dataframe for further use. This is what I am doing

library("RSQLite")
sqlite <- dbDriver("SQLite")
data_files <- list.files("C:/Users/Desktop/Model_files")  # Identify file names
data_files   

en_adrenal.db
en_brain_Cortex.db
en_spleen.db
en_wholeblood.db
and so on
#then use a loop to be able to read in all the db file and create df for each tissue:                                               
for(i in 1:length(data_files)) {                              # Head of for-loop
dbname <-  data_files[i]
#dbname <- "en_Whole_Blood.db" 
db = dbConnect(sqlite,dbname[i])
df[i] <- dbGetQuery(db,"SELECT * FROM weights")
}

But in my case its giving an error.

Error in df[i] <- dbGetQuery(db, "SELECT * FROM weights") : 
object of type 'closure' is not subsettable
In addition: Warning message:
call dbDisconnect() when finished working with a connection 
View(df1)

In my case only one df1 is created rather that individual dataframe for all the tissues. Does anyone know how to deal with this or any advice to solve this error.

sql r

1 answer

how about calling dbDisconnect after dbGetQuery ?

I did that but it still showing error.

for(i in 1:length(data_files)) {                              # Head of for-loop
+     dbname <- data_files[i]
+     #dbname <- "en_Whole_Blood.db" 
+     db = dbConnect(sqlite,dbname[i])
+     df[i] <- dbGetQuery(db,"SELECT * FROM weights")
+     dbDisconnect(df[i])
+ }

Error in df[i] <- dbGetQuery(db, "SELECT * FROM weights") : 
  object of type 'closure' is not subsettable

Log in to answer this question.