3.3  :  Interfacing R with the Millennium Database

R "... is a free software environment for statistical computing and graphics. It is the open source version of the commercial statistical software package S. See the Comprehensive R Archive Networka> for documentation and downloads for R. We mention R here because it allows very easy access to the Millennium database via the GAVO web application.

The following R function uses wget to execute an SQL query and load the result directly into the environment, i.e. one does not need to write the result to file to get hold of it.

#
# This function executes the specified SQL query on a GAVO website
#
# Warning, no sophisticated error handling, caller must check result
#
gavoWebQuery <- function(url="http://www.g-vo.org/Millennium?", user="", password="", sql)
{
  url<-paste(url,"SQL=",sep="")
  cmd<-paste("wget --quiet --http-user=",user," --http-passwd=",password," -O - \"",url,sql,"\"",sep="")
  res<-system(cmd,intern=TRUE)
  if(length(res) == 0)
    result = "ERROR check your web address, user or password"
  else { 
    if(res[1] == "#OK")
      result<-read.csv(textConnection(res), colClasses="character")
    else
      result<-res
  }
  result
}
The following is an example R session using the above function with a query returning the merging history of a galaxy identified by its ID. For this public URL there is no user and password required.
sql<-"
select p.* 
  from millimil..mmgalaxy d
  ,    millimil..mmgalaxy p
 where d.galaxyid = 0
   and p.galaxyid between d.galaxyid and d.lastprogenitorid
 order by p.galaxyid
"

result<-gavoWebQuery(url=webAppUrl, sql=sql)
plot(result$x, result$snapnum)
The result of this session is visualised in the following figure: