Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using sqldf and RPostgreSQL together

When using RPostgreSQL I find that I cannot use sqldf in the same way. For example if I load the library and read in data into a data frame using the following code:

library(RPostgreSQL)
drv <- dbDriver("PostgreSQL")
con <- dbConnect(drv, host="localhost", user="postgres", password="xxx", dbname="yyy", port="5436")
rs <- dbSendQuery(con, "select * from table");                           
df<- fetch(rs, n = -1); dbClearResult(rs) 
dbDisconnect(con) 

I know have the contents of this table in the dataframe df. However if I want to run a SQL command using sqldf I would previously do something like this:

sqldf("SELECT * FROM df WHERE X > 10")

This no longer works as I get the error message:

Error in postgresqlNewConnection(drv, ...) : 
  RS-DBI driver: (could not connect postgres@localhost on dbname "test"
)
Error in !dbPreExists : invalid argument type

I assume this is operator error on my part, but I can't figure how what arguments to supply to sqldf so that it just focuses on the data frame and does not try to connect to anything else.

like image 848
djq Avatar asked Apr 19 '12 21:04

djq


People also ask

How do I join two Postgres databases?

Install the postgres_fdw extension using CREATE EXTENSION . Create a foreign server object, using CREATE SERVER , to represent each remote database you want to connect to. Specify connection information, except user, and password, as options of the server object.

Can you use PostgreSQL as a data warehouse?

If you've heard of PostgreSQL, there's a reason. It's a useful and common data warehouse tool maintained by an active community. It can also handle more than just one kind of data processing, which makes it a pretty compelling option.

Can SQLite connect to PostgreSQL?

Using CData Sync, you can replicate SQLite data to PostgreSQL. To add a replication destination, navigate to the Connections tab. Click Add Connection. Select PostgreSQL as a destination.


2 Answers

Using sqldf with RPostgreSQL

sqldf will automatically work with the test database in PostgreSQL if it sees that RPostgreSQL is loaded. So you can create a test database in PostgreSQL and then use sqldf with that

or, you can specify the name of a different database.

See: sqldf FAQ 12

Using sqldf with RSQLite

If you want to use sqldf with RSQLite rather than with RPostgreSQL you can use sqldf's drv argument to force it use a non-default driver. e.g.

sqldf("select foo from bar...",drv="SQLite")

or, you can set the driver globally using the "sqldf.driver" option. From within R:

options(sqldf.driver = "SQLite")

or, another possibility if you wish to use RSQLite is to detach RPostgreSQL before you use sqldf and load it again afterwards.

See ?sqldf for details.

like image 97
G. Grothendieck Avatar answered Nov 03 '22 17:11

G. Grothendieck


I had the same error and I detached the RPostgeSQL package, rerun my sqldf code and it worked fine r detach("package:RPostgreSQL", unload=TRUE)

like image 7
Kevin Ogoro Avatar answered Nov 03 '22 16:11

Kevin Ogoro