Search code examples
rpoolr-dbi

Why use of pool::dbGetQuery with lapply isn't working?


I'm trying to use R's "pool" package to execute a set of queries against a set of databases.

I have a list of queries, queryList (I confirmed that each element is a character vector, e.g. "SELECT...FROM...").

library(pool)
library(DBI)

# queryList defined earlier

myPool <- dbPool (...)

Results <- lapply(queryList, pool::dbGetQuery, myPool)  # fails here!

The error I get says this: "unable to find an inherited method for function 'dbGetQuery' for signature '"character", "Pool"'.

One SO thread says this is related to S4 incompatibility. pool::dbGetQuery is an S4 method.

Is there a workaround ?


Solution

  • The use of an anonymous function (e.g. function(x)..., as suggested by @neilfws) worked. However, I'm not sure why, since I didn't need to use anonymous functions when I was dealing directly with dbiConnection objects. So this works

    lapply(queryList, DBI::dbGetQuery, conn) # conn is dbiConnection
    

    but this doesn't work

    lapply(queryList, pool::dbGetQuery, pool) # pool is a pool of dbiConnections
    

    Maybe I'm misreading the official documentation?