Skip to content

Calling R functions in Delphi

9 messages · Anna Belova, Duncan Murdoch, Francisco J. Zagmutt +5 more

#
Hello All,

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?

Thanks in advance.

--Anna
-----------------------------------------
Anna Belova
Abt Associates Inc.
4800 Montgomery Ln, St 600
Bethesda, MD-20814
phone: 301-347-5304
fax: 301-652-7530
http://www.abtassociates.com/environment

-----------------------------------------
This message may contain privileged and confidential informa...{{dropped}}
#
If your only interest is the quantile function, you may consider 
implementing your own version in Pascal.  Since R is open source you can 
look at the source code here: 
https://svn.r-project.org/R/trunk/src/library/stats/R/quantile.R

And use the code as a guideline (I recommend reading the GNU license 
before you use R for your own applications)

You can alway use the COM capabilities in R, available at 
http://sunsite.univie.ac.at/rcom/

Try RSiteSearch("Delphi") for a few interesting threads on the subject.

Regards,

Francisco

Dr. Francisco J. Zagmutt
College of Veterinary Medicine and Biomedical Sciences
Colorado State University
Anna Belova wrote:
#
Anna Belova wrote:
Hi Anna

There was a translation of the header files from C to Delphi, so that 
you could used R directly without using DCom - Hans-Peter Suter 
translated / made them.

I'll mail you the version which he send me off list.

Rainer

  
    
#
2006/12/5, Rainer M Krug <RKrug at sun.ac.za>:
Thanks for mentioning. You can download these files any time from:
  - http://treetron.googlepages.com/  (select delphi interface units)

BUT notice, that they will only enable you to write *packages* which
contain Delphi code and not to link/embed R directly from Delphi. IMO
you should use RCom as mentioned above.
#
Anna Belova wrote:
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:

function StartRAndWait (CommandLine : string) : Boolean;
var
    Proc_info: TProcessInformation;
    Startinfo: TStartupInfo;
    ExitCode: longword;
    CreateOK : Boolean;
begin
    Result := False;

    { Initialize the structures }

    FillChar(proc_info, sizeof (TProcessInformation), #0);
    FillChar(startinfo, sizeof (TStartupInfo), #0);
    Startinfo.cb := sizeof (TStartupInfo);
    Startinfo.dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
    Startinfo.wShowWindow := SW_HIDE;

    { Attempt to create the process. If successful wait for it to end}

    CreateOK := CreateProcess(Nil, PChar('R.exe ' + CommandLine), nil,
       nil,False, CREATE_NEW_PROCESS_GROUP+NORMAL_PRIORITY_CLASS, nil,
       nil, StartInfo, proc_info);
    if (CreateOK) then begin
       WaitForSingleObject (proc_info.hProcess, INFINITE);
       GetExitCodeProcess(proc_info.hProcess, ExitCode);
       Result := True
       end;
    CloseHandle(proc_info.hThread);
    CloseHandle(proc_info.hProcess);
    end;

The argument for the procedure (CommandLine) is a string, created by a 
statement like:

Command := 'CMD BATCH ' + CommandFileName + ' ' + TempFileName;

where CommandFileName is the name of the file with the script, and 
TempFileName is the name of the text file containing the output. The 
procedure is fairly lowlevel, but it worked for me using Delphi 7.  I 
do not remember how I managed to put this together (probably a mix of 
help from the R and Delphi lists), so please do not ask questions 
about the finer details.

Tom


+----------------------------------------------------------------+
| Tom Backer Johnsen, Psychometrics Unit,  Faculty of Psychology |
| University of Bergen, Christies gt. 12, N-5015 Bergen,  NORWAY |
| Tel : +47-5558-9185                        Fax : +47-5558-9879 |
| Email : backer at psych.uib.no    URL : http://www.galton.uib.no/ |
+----------------------------------------------------------------+
#
R(D)COM available from CRAN (section Other) gives you R as a COM server.
If Delphi can act as a COM client (which I think is true), you can
access R directly from within your program without the need to write files.

  
    
#
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
#
Earl F. Glynn wrote:
That (I think) would depend on whether the directory to the R.exe is 
in the path for the Windows system you use (which I seem to remember I 
added in my environment).  In any case, I was looking for some kind of 
an interface to R late last spring, and this was what I arrived at. 
Which worked.
Looks sensible to me.
That is a good suggestion.  I'll have to have a look at that.  Thank you!

Tom