Calling R functions in Delphi
Earl F. Glynn wrote:
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);
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.
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;
Looks sensible to me.
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.
That is a good suggestion. I'll have to have a look at that. Thank you! Tom
efg Earl F. Glynn Scientific Programmer Stowers Institute for Medical Research
______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
+----------------------------------------------------------------+ | 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/ | +----------------------------------------------------------------+