Database Reference
In-Depth Information
Using R with Greenplum
In Chapter 3 , Advanced Analytics - Paradigms, Tools, and Techniques , we were intro-
duced to R programming. R is a very powerful programming language that has many
built-in libraries for running statistical and analytical calculation or modeling. In this
section, we will learn how R functions can be integrated to work with data in Green-
plum. There are many ways in which we can run R on the data from Greenplum and
we will now discuss on the following two important approaches:
• R with standard DBI connector to Greenplum; in this case, R program con-
nects to Postgres/Greenplum database, loads data into R client
• PL/R—procedural language for SQL calls to R functions
DBI Connector for R
From R program, we can access Postgres or Greenplum in the following way:
//Database connector code
require(TSP)
require(fields)
require(RPostgreSQL)
drv <- dbDriver("PostgreSQL")
conn <- dbConnect(drv, user="postgres",
dbname="pgissc")
sql.str <- "select id, st_x(location) as x,
st_y(location) as y, location from stands;"
waypts <- dbGetQuery(conn, sql.str)
dist.matrix <- rdist.earth(waypts[,2:3],
R=3949.0)
rtsp <- TSP(dist.matrix)
soln <- solve_TSP(rtsp)
tour <- as.vector(soln)
dbDisconnect(conn)
dbUnloadDriver(drv)
print(paste("tour.dist=",
attributes(soln)$tour_length))
Search WWH ::




Custom Search