Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run Racket code in R

Tags:

r

racket

Is there a way to run Racket code from R (R script calling Racket code) ?

I looked in Rseek and further sources without any pointers.

like image 625
user2030503 Avatar asked Jan 08 '23 17:01

user2030503


1 Answers

A similar question came up recently on the Racket mailing list, and, someone posted a link to an older thread where Matthew Flatt suggested starting a subprocess and piping.

It looks like Julia doesn't provide a foreign interface (e.g., to call Julia from C). So, I think that you'll have to run Julia in a separate process via process or subprocess and communicate with the process through stdin and stdout.

At Wed, 29 Jan 2014 16:58:29 -0800 (PST), E Comer wrote:

Hi Racket developers, programmers and users:

Is there a way to call a Julia function from DrRacket? [I'm trying to integrate the number crunching capabilities of Julia with the nice graphics of the Plot module in Racket, to study some properties of particular dynamical systems]

Thank you very much for your support.

Enrique

So for example, write a little Racket program that loops: read from stdin, write the result to stdout. The format of what it reads and writes is up to you. It could read s-expressions (convenient in Racket) and output a line-oriented result (maybe more convenient in R, I don't know).

Then have R start the Racket program as a subprocess, and talk to its stdin and stdout.

As one example of the reverse (have Racket pipe to a subprocess) see this Racket "client" talking to this Python "server" subprocess. I don't know if R has something like Racket's process, which gives you the stdin and stout of the subprocess.

Although an FFI seems like the "correct" approach, in reality it can be challenging to marshal things between high-level languages. Often it's overkill when you have some specific interop in mind. My advice is to start with the approach of piping to a subprocess. It's generally simple and reliable. It may turn out to be fast enough for your needs. Sometimes it's even faster.[1]


[1]: As some anecdata, look at GitHub's history of using Pygments, written in Python, in their Ruby code. At one point they had some elaborate approach of embedding Python in Ruby. To make it faster, they switched to running Pygments in a subprocess and piping. (That's my understanding from the commit history, anyway.)

like image 90
Greg Hendershott Avatar answered Jan 14 '23 13:01

Greg Hendershott