Creating a list of S4 objects

I wrote a function that allows me to connect to multiple databases at once and then store the connections in a list so that I can call them as needed for each individual project. The function is working great and I've been able to return data from all the databases that I've attempted to connect to (so far up to 3 at once), but it looks like storing this type of connection in a list is deprecated and I got the following error/warning messages.

Error in (function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ‘dbGetQuery’ for signature ‘"list", "character"’
In addition: Warning messages:
1: In `[<-`(`*tmp*`, i, value = new("JDBCConnection", jc = new("jobjRef",  :
  implicit list embedding of S4 objects is deprecated
2: In `[<-`(`*tmp*`, i, value = new("JDBCConnection", jc = new("jobjRef",  :
  implicit list embedding of S4 objects is deprecated
3: In `[<-`(`*tmp*`, i, value = new("JDBCConnection", jc = new("jobjRef",  :
  implicit list embedding of S4 objects is deprecated

What would be the non-deprecated or best practice way to accomplish this?

1 Like

I'm not sure what your issue is.
A reprex would likely help.
In general you can put s4 objects into a list. not sure what implicit list embedding means though.

setClass("Person", representation(name = "character", age = "numeric"))
  hadley <- new("Person", name = "Hadley", age = 31)
  nirgrahamuk <- new("Person",name="Nir",age=38)
  
  mylist_of_people <- list(hadley,
                           nirgrahamuk)
  
  mylist_of_people[[1]]
  mylist_of_people[[2]]

That's where I'm getting stuck. I don't have a problem with your example but only when I try to list JDBC Connections.

I'm not really sure how to create a reprex of this issue because it's only when I try to list database connections.

Fixed it!

I was double checking over my code and realized that the for loop I was using to populate my list was assigning the connections to:

list[i] <- connection

when I should have used double brackets, like this:

list[[i]] <- connection
2 Likes

good spot, thanks for the update

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.