7.00.02.01 - Example 1: Simple Calculation - Aster R

Teradata Aster® R User GuideUpdate 3

prodname
Aster R
vrm_release
7.00.02.01
created_date
December 2017
category
Programming Reference
User Guide
featnum
B700-1033-700K

This example takes the same input and produces the same output as in Example 3: Custom R Function.

Instead of defining an R function, users create an R script that reads data in from a file and writes the output to a table. Users then use the runner function ta.source() to execute the script.

  1. Create the R script "fruit_1104b.R" as shown here.
    # Script file is "fruit_1104b.R"
    # File descriptor pointing to standard input
    in_table = file(description="stdin",open="r") 
    
    # Read column values from stdin into a vector, nrows = -1 means read all rows
    while(1){ 
        fields <- try(read.table(in_table,header=FALSE,sep="\t",quote="",nrows=-1) silent=TRUE
        ) 
        if(inherits(fields,"try-error"))   
        break 
        #Fields are from table Fruit_sales 
        s_date <- as.character(fields[,1]) 
        orderid <- as.integer(fields[,2]) 
        itemid <- as.character(fields[,3]) 
        cost <- as.numeric(fields[,4]) 
        salesprice <- as.numeric(fields[,5]) 
         
        # do work 
        profit <- salesprice - cost 
        i <- data.frame(s_date, orderid, itemid, cost, salesprice, profit) 
        DailySales <- aggregate(salesprice~s_date, i, sum) 
        DailyProfit <- aggregate(profit~s_date, i, sum)  
    
        outrec <- data.fame(DailySales[,1], DailySales[,2], DailyProfit[,2]) 
        #write to standard output stream 
        write.table(outrec, stdout(), col.names=FALSE, row.names=FALSE, quote=FALSE, sep="\t")
    }
    # END
    
  2. Create a table in the database based on the input data, and convert it to a virtual data frame.
    ta.create(Fruit_sales, 
    table="fruit_tmp", 
    schemaName="public", 
    tableType="dimension", 
    row.names=FALSE, 
    colTypes=NULL 
    )
    
    tadf.fruit <- ta.data.frame("fruit_tmp")
  3. Install the script on the cluster.
    ta.install.scripts("fruit_1104b.R")
  4. Create the vector defining the output column names and types.
    outputs2 <- c("Date Date", "DailySales numeric", "DailyProfit numeric")
  5. Use the Aster R runner function ta.source() to run the script.
    ta.source(data = tadf.fruit, script = "fruit_1104b.R", outputs = outputs2)
    
    
          Date   Total Sales Total Profit
    1 2010-05-01       29.80         5.65
    2 2010-05-02       31.78         6.80
    3 2010-05-03       19.25         4.72
    4 2010-05-04       20.57         5.72