2.16 Running R in Origin

There are a couple of ways to interact with R from Origin.

  • Using the R Console and the Rserve Console. These tools allow Origin users to issue R commands within the Origin environment and transfer data between the two applications either using a dialog interface, or by using commands. For examples, see Data Analysis in Origin with R Console.
  • Using the LabTalk objects R and RS (RServe) . The R and RS LabTalk objects can execute R commands and pass variables and datasets between Origin and R.


To make use of the R object and R Console, you need to have R installed locally. To use the RS object and the Rserve Console, you will need to have the R packages installed on the server side, and communicate to the server computer with Origin on the client side.

  • For information on obtaining and installing R locally, see this topic.
  • For information on setting up an R server, see this topic.


Minimum Origin Version Required: 2016 SR0

Execute R Command in Script Window

We can use LabTalk objects R and RS to execute R commands and pass variables between LabTalk and R. The examples below work for both R and RServe (replace "R" with "RS").

Initialize R or RS

Before running R command in Labtalk, you can initialize the R application by running:

if( R.Init()<0 )
	{
		type -b "Please install R software first.";
		return;
	}

in the Script Window.

To initialize the RS object, please refer to Script below, and view this page for parameters setting.

if(RS.Init(***.***.**.**, 12306, user, password)<0)  // Address is Rserve PC IP, e.g: 192.168.18.75
	{
		type -b "Initialize failed.";
		return;
	}

Execute R and RS command

Execute R command

R.Exec(rand<-sample(x = 1:6, size = 50, replace = TRUE));
R.Exec(rand);

and perform analysis:

R.Exec(sum<-summary(rand));
R.Exec(sum);

The summary could be shown in this way:

Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
1.00    2.00    3.50    3.56    5.00    6.00

Execute Multiple R Command Lines

R.Exec("array<-1:25;dim(array)<-c(5,5);array") // Run multiple R command lines separated by semicolon

Execute RS command

string fname$ = system.path.program$ + "Samples\Curve Fitting\Sensor01.dat";
impasc;
range r1 = 1:2;

//send data from worksheet to R vector
RS.Send(%(r1), RR, 2);  //send the range to R data.frame
 
RS.Exec(fit<-lm(RR$Sensor.Output~RR$Displacement));  //perform linear fit
RS.Exec(fit);

Execute R/RS command example

In addition, a practical example is introduced in this page Perform Logistic regression in R by using LabTalk.

Execute .R file

If you want to execute a *.R file in your computer, you can use the syntax below in Script Window:

R.Exec(source(".R file path"));

For example:

R.Exec(source("D:\\RData\\test.R"));

Send Origin Dataset to R Console

//Active a workbook
R.Send("1!1", arr1, 0); // Send Column 1 in Sheet 1 current workbook to R as variable ''array'' (type vector)
range rx=1; // Use range notation rx to refer to column 1 in active worksheet
R.Send(%(rx), rlabel, 0); // Send column 1 to R as variable rlabel (type vector)
// Suppose the values of first columns are: 1, 2, 3, --
range rr = 1; // Refer to first column in active worksheet
R.Send(%(rr), temp, 0); // Send first column as R variable temp

Receive R Variable as Origin Dataset

R.Receive("1!1",RMat,1) // Receive R Matrix Variable RMat to first matrix object in current matrixbook and first matrix sheet
R.Receive("1!1",df$vec1,0); //Receive data member 'vec1' in R data frame 'df' to column as a vector
//Suppose there is a logical vector temp with value [1] FALSE FALSE TRUE
range rl=1;
R.Receive(%(rl),temp,0); // Column 1 in active sheet would be 0,0,1 instead

Pass Variables between Labtalk and R

R and RS objects have four member functions: GetReal, SetReal, GetStr, and SetStr which are used to pass numeric and string variables between Labtalk and R, for example:

You define numeric or string variable in R Serve Console,

Rvar=16
Rstr="height"

You can pass value to LabTalk variable in this way:

R.GetReal(LTVar, RVar)
R.GetStr(LTStr$, RStr)
//pass a real number in a R list 'e1' to a Labtalk variable
R.GetReal(LTVar,e1$a);

Or you define a numeric or string variable in LabTalk, you can pass the value to R variable in this way:

R.SetReal(LTVar, RVar)
R.SetStr(LTStr$, RStr)

Utilizing R Console or Rserve Console

We can use the R Console (or Rserve Console ) to run R commands directly in the dialog input box, then, pass the resulting values into an Origin worksheet using buttons << and >>. Data can be exchanged in multiple formats (vector, matrix, data.frame) between R and Origin worksheet (or matrix sheet) by using R console (or Rserve Console).
The following graph shows how to generate random data with a binomial distribution and pass it to an Origin worksheet.

R Send Data.png