Skip to content
Prev 105649 / 398503 Next

Calling R functions in Delphi

Thanks for the great example, Tom.

"Tom Backer Johnsen" <backer at psych.uib.no> wrote in message 
news:4575AE08.8010304 at psych.uib.no...
. . .
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.
=====================


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