Calling R From D

One way to use embedr is to write a D program that has an R interpreter embedded inside it. This makes available to a D program any of the functionality available in R. That includes thousands of packages, plotting functions, and tools for data manipulation.

The problem with doing this is that it can be a mess to find all of the underlying dependencies needed for compilation. There is an R function dmd that will handle all of the work for you.1

Hello World

Let's start with an example that has R print "Hello, World!" to the screen. Put the following code in a file named hello.d:

import embedr.r;

void main() {
    evalRQ(`print("Hello, World!")`);
}

In the directory containing hello.d, run the following in R:

library(embedr)
dmd("hello")

This will tell dmd to compile your file, handling includes and linking for you, and then run it for you. You should see "Hello, World!" printed somewhere. The other examples are the same: save the code in a .d file, then call the dmd function to compile and run it.


  1. At this point it is customary for someone to tell me to use Dub. The truth is that Dub is useless for this type of project. It will not find the underlying libraries for the user, and it offer no tools to do help with the task. If you believe I am wrong, please provide a working example. That said, you should be able to add the information provided by the dmd function to your dub.sdl or dub.json file.