# 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){
)
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
)

`ta.install.scripts("fruit_1104b.R")`
`outputs2 <- c("Date Date", "DailySales numeric", "DailyProfit numeric")`
```ta.source(data = tadf.fruit, script = "fruit_1104b.R", outputs = outputs2)