Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql Delphi:如何在Delphi中调用R函数(或集成R)?_Mysql_R_Function_Delphi - Fatal编程技术网

Mysql Delphi:如何在Delphi中调用R函数(或集成R)?

Mysql Delphi:如何在Delphi中调用R函数(或集成R)?,mysql,r,function,delphi,Mysql,R,Function,Delphi,有没有人有关于如何在Delphi中使用R函数的提示或示例?我通过MySQL以集成的方式使用了R和Delphi,我将Delphi的输入发送到MySQL,运行连接到MySQL(包RMySQL)的函数/on R脚本,它将输出返回到MySQL,然后使用Delphi。但是这个过程很慢,这完全取决于脚本R的大小。有人举个例子或提示来加速这个过程吗 这有一个例子,但所有链接都已关闭。下面的代码显示了如何使用present R和Delphi的一个小示例 unit Unit1; interface uses

有没有人有关于如何在Delphi中使用R函数的提示或示例?我通过MySQL以集成的方式使用了R和Delphi,我将Delphi的输入发送到MySQL,运行连接到MySQL(包RMySQL)的函数/on R脚本,它将输出返回到MySQL,然后使用Delphi。但是这个过程很慢,这完全取决于脚本R的大小。有人举个例子或提示来加速这个过程吗

这有一个例子,但所有链接都已关闭。下面的代码显示了如何使用present R和Delphi的一个小示例

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Datasnap.Provider,
  Data.DB, Datasnap.DBClient;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses Unit2;

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('C:\Program Files\R\R-3.0.2\bin\x64\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;

procedure TForm1.Button1Click(Sender: TObject);
var
    Command: STRING;
begin

  DataModule.ClientDataSet.Open;
  DataModule.ClientDataSet.Insert;
  DataModule.ClientDataSeta.AsFloat:= strtofloat(Edit1.Text);
  DataModule.ClientDataSetb.AsFloat:= strtofloat(Edit2.Text);
  DataModule.ClientDataSet.Post;
  DataModule.ClientDataSet.ApplyUpdates(0);
  DataModule.ClientDataSet.Close;
  Screen.Cursor := crHourGlass;
  try
    Command := 'CMD  BATCH  script.R  outputconsole.txt';
    StartRAndWait(Command);
  finally
    Screen.Cursor := crDefault
  end;

  DataModule.ClientDataSet.Open;
  DataModule.ClientDataSet.Last;
  Edit3.Text:= DataModule.ClientDataSetresult.AsString;
  DataModule.ClientDataSet.Close;

end;
end.

在我看来,这根本不是德尔菲的问题。您已经提供了Delphi代码,但实际上R代码在这里很重要。您的Delphi程序只是启动一个新的R进程,并等待它终止。是的,旋转新进程会带来一些开销,但我猜大部分时间都花在执行R代码上

所以,如果你想加速整个计算,首先要做的是加速计算中耗时最长的部分。这似乎是在R下执行的部分


可以在其他进程中嵌入R。这可以避免每次执行R代码时都等待新的R进程启动。然而,嵌入R并不是最容易的事情。在C++中有很好的R包,可以很容易地嵌入R。我在想,特别是漂洗。你可以利用它,或者至少从中获得灵感。但是,我强烈怀疑嵌入无法解决根本问题,这仅仅是因为R代码的运行时间比您希望的要长。

我认为您可以通过“ShellExecute”来实现这一点,而不需要TProcessinformation和TStartupInfo。

我认为没有办法让R脚本运行得更快(当然,不需要更改R脚本),但是你用这个调用阻止了主线程,这是不好的。最好在线程中运行它-让它在脚本完成时引发一个事件,并在需要时返回退出代码。嗯……这是真的。我会试试。@J……请看我在David ans中的最后一条评论。可能吗?@Artur_Indio-你成功调用了R吗来自Delphi的函数?指向Meme()的链接是坏的。你有副本吗?@祭坛我不能,即使今天我还在寻找!这也太准确了,我还认为一切实际上都取决于我的Sprit R。然后在Delphi中调用R函数将是最好的方式,不是这个交互Delphi->MySQL->R->Delphi。下面的网站他说更多的RDCOM无法访问上的文件我将看看Rinside,它应该更容易集成,因为R是用C编写的。嵌入将如何加快速度?对不起,我指的是RCCP而不是Rinside。也许下面这篇使用RDcom的文章的作者可以为我提供必要的文件。请看第7页。是的,但是嵌入将如何加快速度p?你似乎在抓救命稻草。如果把时间花在执行R代码上,嵌入不会帮助你提高速度。