-->

Using RMySQL interferes with RPostgreSQL

2019-07-21 20:02发布

问题:

I have an R script where I want to pull some data from a MySQL database, then from a PostgreSQL database. However, loading the MySQL driver from RMySQL prevents me from loading the PostgreSQL driver from PostgreSQL.

I can load the Postgres driver fine on its own:

> RPostgreSQL::PostgreSQL()
<PostgreSQLDriver:(58810)> 

Then I can load the RMySQL driver:

> RMySQL::MySQL()
<MySQLDriver>

However, if I load the MySQL driver first, the PostgreSQL driver fails to load:

> RMySQL::MySQL()
<MySQLDriver>
> RPostgreSQL::PostgreSQL()
Error in as.integer(from) : 
  cannot coerce type 'S4' to vector of type 'integer'

This seems a little magical to me: RMySQL shouldn't be altering functions in RPostgreSQL's namespace.

Does anyone have any idea what might be causing this problem and how I can solve it? I've replicated the problem on 2 different machines, with the exact same results.

Machine 1: Mac OS 10.9.5:

Machine R version 3.1.2 (2014-10-31)
Platform: x86_64-apple-darwin13.4.0 (64-bit)

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
[1] DBI_0.3.1       RMySQL_0.10     RPostgreSQL_0.41

Machine 2: Fedora release 20 (Heisenbug):

R version 3.1.1 (2014-07-10)
Platform: x86_64-redhat-linux-gnu (64-bit)

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
[1] DBI_0.3.1       RMySQL_0.10     RPostgreSQL_0.4

/edit: Furthermore, even if I load the Postgres driver first, merely loading the MySQL driver completely borks the Postgres driver:

library(DBI)
dr1 <- RPostgreSQL::PostgreSQL()
dr2 <- RMySQL::MySQL()

> dbConnect(dr1, host = "me", port = "1234", dbname = "me", user="me", password="me")
Error in as.integer(from) : 
  cannot coerce type 'S4' to vector of type 'integer'

/edit 2: This is a bug in RMySQL 0.10; the above code works fine in RMySQL 0.9.3. Judging from this 20-month old question, a similar bug crops up from time to time in RMySQL.