Options

R function with multiple arguments

I want to develop a UDSF in R with multiple arguments. Something like below

validate <- function(x,tname,cname)
{

var <- x
tableName <- tname
colName <- cname
result <- sqlQuery(myconn, paste(" SELECT COUNT(*) FROM", tableName, "WHERE", colName,"=",val))

if (result > 0 )
{
return(x)
}
else
{
return(1000)
}
}
validateFactory <- function()
{
list(name=validate, udxtype=c("scalar"), intype=c("int","varchar","varchar"), outtype=c("int"))
}

But I am no unable to use the actual values for arguments "tname" and "cname" in this.

This works if there is a single arguments but not for more than 1.

How can I use data.frame or something else to fix this?

Comments

  • Options
    Hi Siddarth,

    UDSF values are passed in as a single data frame.  See "/opt/vertica/sdk/examples/RFunctions/RFunctions.R" for examples of multi-argument UDSF's.

    If you want each column to be mapped to a separate R variable, maybe you could write a wrapper function that splits the frame by column.  The first example in the above file does something that's not exactly this but that should get you started.

    (I would only recommend using a variable per column if you have a relatively small number of columns.  If you have many hundreds of columns, which some people do, you probably want a single array and not hundreds of variables.)

    Adam
  • Options
    A bit late but I prefer questions to have additional answers.

    You can use a PARAMETERTYPECALLBACK function as part of your UDF design. The parameters you send to the R code arrive as :

    MainRFunction <- function(dataset, parameters){
         str(dataset) ==> data frame with defined columns (See intype in factory function)

         str(parameters) ==> list with N objects defined in a parameter function

    }

    FactoryFunction <- function(){
        list(name = MainRFunction,
    udxtype = "SCALAR",
    intype = ..., 
    outtype = ...,
    parametertypecallback = ParameterFunction, 
    }

    ParameterFunction <- function(){
        paramAmount <- 2    
        init <- rep(NA, paramAmount)
        param <- data.frame(datatype = init, length = init, scale = init, 
            name = init)
        param[, 1] <- rep("VARCHAR", 2)
        param[, 4] <- c("par1", "par2")
        return(param)
    }

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file