# Integrating with R

R is a data analysis environment created through the GNU Project. This page provides a place for Analytica users to share tricks and techniques for integrating R with Analytica models.

## Calling R from an Analytica model

### Running a batch job

#### Scenario

You need to run a particular analysis using R code and libraries, possibly running on data from generated by your Analytica model, and then you'll need to read in the results of that R analysis into Analytica.

#### Requirements

You'll need the Analytica Enterprise or better for this.

#### Basic steps

1. Install the R Software for Windows (if you haven't already)
2. Figure out how to perform the analysis of interest in R. Note down the list of commands you would issue in R.
3. From your Analytica model, use WriteTextFile to write out the R commands that will need to be executed to a file.
4. Use RunConsoleProcess to launch R.exe and run the analysis
6. Use FindInText, SplitText and ParseNumber to parse the results from R.

#### An Example

I will use an example to help illustrate each step. In the example, we have the physical dimensions of a collection of 12 books, and the weight of each book. We will use R's linear regression command to obtain the regression coefficients and related statistics for the regression. This example would be very easy to do within Analytica using the Regression function -- much easier than implementing this integration -- but the purpose here is to illustrate the steps with a very simple example.

It is assumed that you know R (which would explain why you want to use it). This is the Analytica Wiki -- we're not trying to teach R here.

The Analytica model contains the physical book dimensions in a variable called physical_dimensions, and a variable named Weight with the weight of each book:

The R example is lifted from [http://cran.r-project.org/doc/contrib/usingR.pdf Using R for Data Analysis and Graphics Introduction, Code and Commentary] by J H Maindonald.

##### Writing out the data and R commands

First, we will construct an R command to create a data frame in R named bookdata that contains both the physical dimensions and weights. The following Analytica expression does this:

 'bookdata <- structure(list(' &
JoinText(chr(13) & Term & ' = c(' & JoinText(physical_dimensions, Data_Index, ', ') & ')', Term, ', ') & ',
weight = c(' & JoinText(weight, Data_Index, ', ') & ')), ' & '
.Names=c(' & JoinText('"' & Term & '"', Term, ', ') & ', "weight"), ' & '
row.names = c(' & JoinText('"' & Data_Index & '"', Data_Index,' ,') & '),
class = "data.frame")'


This becomes the definition of R_cmd_data, and when it is evaluated, the following R command is produced (we've introduced some additional new-lines here):

 bookdata <- structure(list(
thick = c(14, 15, 18, 23, 24, 25, 28, 28, 29, 30, 36, 44),
height = c(30.5, 29.1, 27.5, 23.2, 21.6, 23.5, 19.7, 19.8, 17.3, 22.8, 17.8, 13.5),
width = c(23, 20.5, 18.5, 15.2, 14, 15.5, 12.6, 12.6, 10.5, 15.4, 11, 9.2),
weight = c(1075, 940, 625, 400, 550, 600, 450, 450, 300, 690, 400, 250)),
.Names=c("thick", "height", "width", "weight"),
row.names = c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"),
class = "data.frame")


Next, to perform the analysis, we need to perform this command in R:

summary(lm(weight~.,data=log(bookdata)))

The Analytica code that writes the data and analysis command to a script file is thus:

 WriteTextFile(script_file, R_cmd_data, append: false, warn: false);
WriteTextFile(script_file, "

summary(lm(weight~.,data=log(bookdata)))", append: true, warn: false)


where Constant script_file contains the file name for the R commands, e.g., "C:\Temp\books.R"

## Using an Analytica model from R

TBD: Would be done using ADE