Skip to content
This repository was archived by the owner on May 22, 2024. It is now read-only.

Development Guide

Hai Qian edited this page Sep 13, 2013 · 52 revisions

Here is a simple guide for developing wrapper functions of MADlib.

  1. Useful Utility Functions in PivotalR

    schema.madlib cbind db.array db.data.frame as.db.data.frame conn.id names delete content conn.eql as.factor eql

  2. Useful Internal Utility Functions in PivotalR

    Call a hidden function from command line for testing

    .db.getQuery .load.func .suppress.warnings .restore.warnings .check.madlib.version .get.params .get.res .is.conn.id.valid .unique.string .strip .get.dbms.str .madlib.version.number

  3. Useful R Code Snippets

    Raise an error Output a string Join multiple strings Exception handling Set the result class Check whether an object belongs to a class Get the command call as a string Check whether an argument is missing Regular expression and gsub The for loop Check NULL value

  4. Examples

    S3 example S4 example

Useful Utility Functions in PivotalR

schema.madlib

cbind

db.array

db.data.frame

as.db.data.frame

conn.id

names

delete

content

conn.eql

as.factor

eql

Useful Internal Utility Functions in PivotalR

Call an internal function from command line for testing

PivotalR:::.unique.string()

.db.getQuery (query, con.id)

Execute the query string in connection conn.id, returns a data.frame, which is the result of the SQL query. The function .get.params is preferred than this one. One should use .get.res function instead.

.load.func (funcname, conn.id)

Load a SQL function definition from inst/sql/

.suppress.warnings (conn.id)

Suppress all warnings, returns the original warning levels

.restore.warnings (pre.warn)

Restore the original levels

.check.madlib.version (data, allowed.version=0.6)

When MADlib version is smaller than allowed.version, raise an error

.get.params (formula, data)

Analyze a formula and get the dependent, independent and grouping variables. Do pivoting if factor column is specified. Create intermediate table for db.Rquery and db.view objects.

.get.res (sql, tbl.output = NULL, conn.id)

.is.conn.id.valid (conn.id)

.unique.string ()

.strip (str, rm = "\\s")

.get.dbms.str (conn.id)

.madlib.version.number(conn.id)

Useful R Code Snippets

Raise an error

stop("We have ",
     "an error at line ", 365, "!")

Output a string

cat("We have ",
    "a string here ", 365, sep = "")

Join multiple strings

paste("We have", "something at", 365)
paste("We have ", "something at ", 365, sep="")
paste0("We have ", "something at ", 365)

a <- c(1,2,3)
paste(a, "is a", collapse=" + ", sep="")

Exception handling

res <- try(.db.getQuery(sql, conn.id(x)), silent = TRUE)
if (is(res, .err.class))
    stop("Could not do the summary!")

.get.res already has exception handling built in.

Set the result class

class(rst) <- "arima.css.madlib"

Check whether an object belongs to a class

is(x, "db.Rquery")
is(res, .err.class)
is(res, "data.frame")

Get the command call as a string

call <- deparse(math.call())

Check whether an argument is missing

if (missing(j)) {
    stop("Error")
}

Regular expression and gsub

gsub(regular-expression-to-replace, new-regular-expression, your-string)

gsub("\\d+", "digits", "1233535 is the number") # returns "digits is the number", note the double slashes

R's regular expressions use \\ instead of \.

The for loop

for (i in seq_len(n)) print(i) # seq_len(0) is integer(0), loop is not executed

Check NULL value

if (is.null(x))
    stop("Error: cannot be NULL!")

Examples

S3 example

Linear Regression

S4 example

ARIMA

Clone this wiki locally