[R] Running an R script from Delphi 7

Tom Backer Johnsen backer at psych.uib.no
Tue Apr 7 17:12:22 CEST 2009


Gentlepersons:

A long time ago I used to be able to start R (version 2.6.1) from a 
Delphi 7 program and run a script by using a procedure like the following:

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, nil, nil, nil, nil,
    CreateOK := CreateProcess(Nil, PChar('C:\Program 
Files\R\R-2.8.1\bin\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;

Now it seems to hang on the call on WaitForSingleObject toward the end. 
  My current version of R is 2.8.1 if that is relevant.  Does anybody 
have any suggestions?

Tom




More information about the R-help mailing list