Example 1: Simple Calculation - Aster R

Teradata Aster® R User GuideUpdate 3

Product
Aster R
Release Number
7.00.02.01
Published
December 2017
Language
English (United States)
Last Update
2018-04-13
dita:mapPath
fop1497542774450.ditamap
dita:ditavalPath
Generic_no_ie_no_tempfilter.ditaval
dita:id
fbp1477004286096
lifecycle
previous
Product Category
Software

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