[R-sig-Geo] [R-sig-DB] [R] Connecting to PostgreSQL/PostGIS from R (rgdal?)

Tim Keitt tkeitt at gmail.com
Sat Jun 2 21:02:10 CEST 2007

On 6/2/07, Prof Brian Ripley <ripley at stats.ox.ac.uk> wrote:
> On Sat, 2 Jun 2007, Tim Keitt wrote:
> > On 6/2/07, Prof Brian Ripley <ripley at stats.ox.ac.uk> wrote:
> >> On Sat, 2 Jun 2007, Tim Keitt wrote:
> [...]
> >> > Edzer's approach is one I've suggested on many occasions -- that we
> >> > subclass the sp classes to act as a proxy to a PostGIS table. I first
> >> > developed that approach in rpgsql -- you could create proxy data
> >> > frames that simply forwarded access requests to PostgreSQL (I think
> >> > the idea has now been applied to SQLite tables). The problem of course
> >> > is that R does not support polymorphism "below the surface" in the C
> >> > level implementation, so once a proxy object is passed to R's
> >> > internals, it is nothing more than a pointer with no call interface.
> >>
> >> I really don't think that is true, and indeed polymorphism is almost
> >> entirely implemented at C level.  A visible R object and a SEXP are one
> >> and the same thing.
> >
> > I guess what I mean is that -- unless I'm mistaken -- I cannot
> > override the data access routines. Lets say I have a C function that
> > takes an SEXP as an argument and the function expects that SEXP to
> > point to a data frame. I cannot call the same C function on an SEXP
> > that points to a PostgreSQL proxy object because the C code will try
> > to dereference memory offset from the base of the data portion of the
> > SEXP. In the case of the db handle, there is no data in memory! Or is
> > there now a full C-level function call interface for manipulating data
> > frames (that can handle type dependent dispatch)? Cool if so -- its
> > been awhile since I looked.
> >
> > (Just as an example think about what happens when the data in a matrix
> > is passed to BLAS routines and what would happen if you tried to pass
> > the db handle to BLAS.)
> But that is several layers deeper. In general one should check that your
> code points to the type of object you want, and dispatch if you want to be
> polymorphic.  Remember that in S/R it is functions (including operators)
> that are polymorphic, and for example the C-level versions of [, $, $<-
> ... all are (and long have been).

Right. Point well taken. However, in general R functions like "lm" (or
more precisely, the low level R functions called by "lm") do not use
those C-level functions but instead pass the SEXP through and assume
that the SEXP points to a block of allocated memory, not eg a database
handle. I had hoped at one point to make proxy data frames fully
substitutable for real data frames so they could be passed directly to
functions like "lm". It is possible as you point out, but would
require extensive rewriting of R's low level code to always call the
correct getter/setter functions for each object type.


> --
> Brian D. Ripley,                  ripley at stats.ox.ac.uk
> Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
> University of Oxford,             Tel:  +44 1865 272861 (self)
> 1 South Parks Road,                     +44 1865 272866 (PA)
> Oxford OX1 3TG, UK                Fax:  +44 1865 272595

Timothy H. Keitt, University of Texas at Austin
Contact info and schedule at http://www.keittlab.org/tkeitt/
Reprints at http://www.keittlab.org/tkeitt/papers/
ODF attachment? See http://www.openoffice.org/

More information about the R-sig-Geo mailing list