Recent

Author Topic: How to connect to R languase  (Read 7381 times)

Almir.Bispo

  • Jr. Member
  • **
  • Posts: 91
  • CSV Comp DB is the Best NoSQL
    • CSV Comp DB (NoSQL)
Re: How to connect to R languase
« Reply #15 on: April 15, 2018, 05:16:54 pm »
This is not a good idea because if you can work directly with Object Pascal in Bigdata why use R? This is like use a Ferrari with firebird engine.
You can work with  object Pascal,CSV Comp DB and bigdata directly (just use ILDE-Pascal)
look this:
https://www.youtube.com/watch?v=qNaY8GknLWM&t=0s
https://www.youtube.com/watch?v=2XytLSkgyQw
download ILDE-Pascal here:
http://adltecnologia.blogspot.com.br
CSV Comp DB has a CQL languagem embedded on ILDE-Pascal
https://www.youtube.com/watch?v=O2aml1vBvq4&t=0s
CSV Comp DB Developer {Pascal Lover}

Phil

  • Hero Member
  • *****
  • Posts: 2750
Re: How to connect to R languase
« Reply #16 on: April 15, 2018, 05:19:12 pm »
This is not a good idea because if you can work directly with Object Pascal in Bigdata why use R? This is like use a Ferrari with firebird engine.

R is what statisticians and scientists know and use.

He _is_ using Pascal, for the frontend, where it might make more sense.

Almir.Bispo

  • Jr. Member
  • **
  • Posts: 91
  • CSV Comp DB is the Best NoSQL
    • CSV Comp DB (NoSQL)
Re: How to connect to R languase
« Reply #17 on: April 15, 2018, 05:27:49 pm »
Everything that you can do with R,so you can do with object Pascal (and much more,not onl frontend but statistical functions,graphs too)
CSV Comp DB Developer {Pascal Lover}

jamie

  • Hero Member
  • *****
  • Posts: 4447
Re: How to connect to R languase
« Reply #18 on: April 15, 2018, 05:59:39 pm »
I agree, I can use the TChart for most of what R does and some generalized Pascal code for the rest.

The only true wisdom is knowing you know nothing

engkin

  • Hero Member
  • *****
  • Posts: 2699
Re: How to connect to R languase
« Reply #19 on: April 15, 2018, 06:21:44 pm »
I am sad that the Rserve only support java/C++/Python/.NET/CLI/C#/Ruby.
Rserve does not support a specific language. It is a TCP/IP server. It comes with example clients for C++, Java, and PHP. Any person interested enough could develop a client for FPC.


On the other hand, under Windows, I would be more interested in calling R.Dll directly.

I don't know how helpful for you, but there used to be a Delphi interface on http://treetron.googlepages.com. You can still find a copy on Web Archives. It is old from 2009:
Quote
Most LGPL'ed R headers have been translated to PASCAL and you can interact with R on an equal (almost) level than the commonly used C code. It is about building packages! To embed R in a Delphi application you would have to translate some more headers.

I don't know if there is any recent/complete one.

wp

  • Hero Member
  • *****
  • Posts: 8401
Re: How to connect to R languase
« Reply #20 on: April 15, 2018, 10:10:05 pm »
My application uses the unit 'ShellCommandRunner', and click the button1 can get the correct result,but click the button2 i cant get the correct result.How can i do?

This is because TShellCommandRunnerThread sets FreeOnTerminate in its constructor. This means that the thread is destroyed automatically when it is finished. When you click Button2 afterwards the thread does not exist any more because you create it only once. You can fix this by creating the threads whenver the corresponding button is clicked:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   th: TShellCommandRunnerThread;
  4. begin
  5.   th := TShellCommandRunnerThread.Create;
  6.   th.OnOutputAvailable := @OnOutputAvailable;
  7.   th.CommandLine := 'cmd /c dir c:\';
  8.   th.Start;
  9. end;
  10.  
  11. procedure TForm1.Button2Click(Sender: TObject);
  12. var
  13.   th: TShellCommandRunnerThread;
  14. begin
  15.   th := TShellCommandRunnerThread.Create;
  16.   th.OnOutputAvailable := @OnOutputAvailable;
  17.   th.CommandLine :=  'cmd /c dir d:\';
  18.   th.Start;
  19. end;
  20.  
  21. // Capture the output
  22. procedure TForm1.OnOutputAvailable(const pBuffer: PByteArray; const pCount: integer);
  23. var
  24.   s: string;
  25. begin
  26.   s := TShellCommandRunner.BufferToString(pBuffer, pCount);
  27.   Memo1.Append(ConsoleToUTF8(s));
  28. end;
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

bills

  • New Member
  • *
  • Posts: 38
Re: How to connect to R languase
« Reply #21 on: April 16, 2018, 04:55:33 pm »
Thank you wp. I think i cant Frequently create and kill the R thread.

bills

  • New Member
  • *
  • Posts: 38
Re: How to connect to R languase
« Reply #22 on: April 16, 2018, 05:01:28 pm »
I am sad that the Rserve only support java/C++/Python/.NET/CLI/C#/Ruby.
Rserve does not support a specific language. It is a TCP/IP server. It comes with example clients for C++, Java, and PHP. Any person interested enough could develop a client for FPC.


On the other hand, under Windows, I would be more interested in calling R.Dll directly.

I don't know how helpful for you, but there used to be a Delphi interface on http://treetron.googlepages.com. You can still find a copy on Web Archives. It is old from 2009:
Quote
Most LGPL'ed R headers have been translated to PASCAL and you can interact with R on an equal (almost) level than the commonly used C code. It is about building packages! To embed R in a Delphi application you would have to translate some more headers.

I don't know if there is any recent/complete one.

Thank you engkin.
This is a good solution. But my ability is poor. If i have time, I'll study and try to write a interface.

jwdietrich

  • Hero Member
  • *****
  • Posts: 1174
    • formatio reticularis
Re: How to connect to R languase
« Reply #23 on: April 18, 2021, 07:40:00 pm »
A cross-platform way to invoke R from Free Pascal code is implemented in the following two functions:

Code: Pascal  [Select][+][-]
  1. function RunRCommand(const script: AnsiString; out RResult: AnsiString): boolean;
  2. var
  3.   commandHeader: array of TProcessString;
  4. begin
  5.   SetLength(commandHeader, 2);
  6.   commandHeader := ['-e', script];
  7.   Result := RunCommand(RCommand, commandHeader, RResult, [poNoConsole], swoHIDE);
  8. end;
  9.  
  10. function RunRCommands(const script: array of AnsiString; out RResult: AnsiString): boolean;
  11. var
  12.   i: integer;
  13.   commandHeader: array of TProcessString;
  14. begin
  15.   SetLength(commandHeader, 2 * length(script));
  16.   for i := 0 to length(script) - 1 do
  17.   begin
  18.     commandHeader[2*i] := '-e';
  19.     commandHeader[2*i + 1] := script[i];
  20.   end;
  21.   Result := RunCommand(RCommand, commandHeader, RResult, [poNoConsole], swoHIDE);
  22. end;
  23.  

The difference between the two functions is that RunRCommand executes a single R command and then directly returns its result, whereas RunRCommands can execute a sequence of commands, which is useful if you want to want to run a script or assign variables so that you want to preserve some memory in R. The functions expect the R command(s) in the variable script and return the result in RResult. The boolean result of the functions indicates if the operation was successful or if it raised an error. The global variable RCommand is to be provided by the program and contains the path to Rscript or Rscript.exe, resp.

The following function is able to semi-automatically provide the correct path:

Code: Pascal  [Select][+][-]
  1. procedure SetPathToR(const FallBackPath: String);
  2. const
  3.   WinFallBack = 'C:\Program Files\R\R-3.2.3\bin\x64\Rscript.exe';
  4.   UnixFallBack = '/usr/local/bin/Rscript';
  5. begin
  6.   {$IFDEF Windows}
  7.     SetLength(CmdHeader, 1);
  8.     CmdHeader := ['Rscript.exe'];
  9.     if not RunCommand('where', CmdHeader, RCommand, [poNoConsole], swoHIDE) then
  10.     begin
  11.       if FallBackPath = '' then
  12.         RCommand := WinFallBack
  13.       else
  14.         RCommand := FallBackPath;
  15.     end;
  16.   {$ELSE}
  17.   {$IFDEF UNIX}
  18.     SetLength(CmdHeader, 1);
  19.     CmdHeader := ['Rscript'];
  20.     if not RunCommand('which', CmdHeader, RCommand, [poNoConsole], swoHIDE) then
  21.     begin
  22.        if FallBackPath = '' then
  23.         RCommand := UnixFallBack
  24.       else
  25.         RCommand := FallBackPath;
  26.    end;
  27.   {$ENDIF}
  28.   {$ENDIF}
  29. end;

It can be invoked, e.g., in the initialization section of a unit with

Code: Pascal  [Select][+][-]
  1. initialization
  2.   SetPathToR('');

Providing a non-empty string in FallBackPath can be used to address custom installations with non-standard paths.
« Last Edit: April 18, 2021, 07:41:34 pm by jwdietrich »
function GetRandomNumber: integer; // xkcd.com
begin
  GetRandomNumber := 4; // chosen by fair dice roll. Guaranteed to be random.
end;

http://www.formatio-reticularis.de

Lazarus 2.0.12 | FPC 3.2.0 | PPC, Intel, ARM | macOS, Windows, Linux

 

TinyPortal © 2005-2018