what is the difference between src_postgres
and dbConnect
function? Both can be used to connect R with postgres using the RPosgresql package. In my experiments I only could use src_postgres to read and dbConnect to write to the database.
When I tried it in different combinations I only received errors.
This seems fairly strange to me.
src_postgres
is a function for creating a connection to a PostgreSQL database from the dplyr package. The RPostgreSQL package implements a method for the genericdbConnect
from the DBI package.src_postgres
callsdbConnect
from RPostgreSQL (I assume).The generic connection object returned by
dbConnect
is meant to be an open ended interface for sending SQL queries to the data base. This means you could feed it anyselect
,update
,insert
,delete
, etc. query that you like.src_postgres
is part of the higher level interface to working with data from databases that Hadley built in dplyr. Thesrc_*
functions connect to a db and then thetbl
functions specify a more specific data source (table, view, arbitrary select query) to pull data from. There are some basic table manipulation functions in dplyr but I don't believe it is intended to be a tool for doingupdate
orinsert
type things in the db. That's just not what that tool is for. Note that the "verbs" implemented in dplyr are all focused on pulling data out and summarising (select
,filter
,mutate
, etc.).If you need to alter data in a data base on a row level, you'll need to send SQL queries to a connection created by
dbConnect
. If all you're doing is pulling data from a db and analyzing it in R, that is what dplyr is for.