[R] Calling R functions in Delphi

Earl F. Glynn efg at stowers-institute.org
Wed Dec 6 18:00:17 CET 2006


Thanks for the great example, Tom.

"Tom Backer Johnsen" <backer at psych.uib.no> wrote in message 
news:4575AE08.8010304 at psych.uib.no...
> Anna Belova wrote:

>> We would like to call quantile() function from the R-package STATS in a
>> Delphi program. If this is possible, could anyone provide us with an
>> example?
>
> It is possible, and in principle simple.  The essentials:  (1) Write a
> file containing the something like a script in R with whatever
> commands. (2) Start a process involving the execution of R with a
> command line containing two arguments, the name of the command file
> and the file where you want the output (results) to be. (3) wait for
> the process to stop.  So, here is a function (returns true if
> everyhing worked OK) that does that:

. . .

>    CreateOK := CreateProcess(Nil, PChar('R.exe ' + CommandLine), nil,
>       nil,False, CREATE_NEW_PROCESS_GROUP+NORMAL_PRIORITY_CLASS, nil,
>       nil, StartInfo, proc_info);

I had to give the full path to the R executable to get this to work:

   CreateOK := CreateProcess(Nil, PChar('C:\Program 
Files\R\R-2.4.0\bin\R.exe ' + CommandLine), nil,
       nil,False, CREATE_NEW_PROCESS_GROUP+NORMAL_PRIORITY_CLASS, nil,
       nil, StartInfo, proc_info);

I used Delphi 7 to test StartRAndWait with this button press event:

procedure TForm1.Button1Click(Sender: TObject);
  VAR
    Command: STRING;
begin
  Screen.Cursor := crHourGlass;
  TRY
    Command := 'CMD  BATCH  Sample.R  SampleOutput.txt';
    StartRAndWait(Command);
  FINALLY
    Screen.Cursor := crDefault
  END
end;


Sample.R file
=====================
sink('quantile.txt')
quantile(0:100)
sink()
=====================

I used sink in the R script to isolate the output of the R quantile command 
to help any parsing of the output:

SampleOutput.txt
=====================
R version 2.4.0 (2006-10-03)
Copyright (C) 2006 The R Foundation for Statistical Computing
ISBN 3-900051-07-0

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> sink('quantile.txt')
> quantile(0:100)
> sink()

=====================


quantile.txt
=====================
  0%  25%  50%  75% 100%
   0   25   50   75  100
=====================


Tinn-R is written in Delphi, so its source code should be a great example of 
a Delphi/R interface.  I've never studied the source code -- that's been on 
my "to do" list for months --, but I'm guessing it uses RCOm, like 
Hans-Peter suggested.  Nevertheless, Tom's example above may also be quite 
useful.

efg

Earl F. Glynn
Scientific Programmer
Stowers Institute for Medical Research




More information about the R-help mailing list