DasBetterR

bachmeier no at spam.net
Fri Jun 30 18:47:06 UTC 2023


On Friday, 30 June 2023 at 16:14:48 UTC, jmh530 wrote:
> On Thursday, 29 June 2023 at 23:51:44 UTC, bachmeier wrote:
>> [snip]
>
> Glad you're continuing to do work on this front. There's a lot 
> of great material explaining things, which is always good.
>
> It would be cool to have another version of the link below for 
> using a mir Slice with R.
> https://bachmeil.github.io/betterr/setvar.html

I assume you mean that you've allocated memory on the D side, 
like this:

```
auto a = new double[24];
a[] = 1.6;
Slice!(double*, 1) s = a.sliced();
```

and you want to pass s to R for further analysis. Unfortunately, 
that will not work. R functions only work with memory R has 
allocated. It has a single struct type, so there's no way to pass 
s in this example to R.

The best you can do right now is something like this:

```
auto a = Vector(24);
Slice!(double*,1) s = a.ptr[0..24].sliced();
// Manipulate s
// Send a as an argument to R functions
```

In other words, you let R allocate a, and then you work with the 
underlying data array as a slice.

A way around this limitation would be to implement the same 
struct (SEXPREC) in D, while avoiding issues with R's garbage 
collector. That's a more involved problem than I've been willing 
to take on. If someone has the interest, the SEXPREC struct is 
defined here: 
https://github.com/wch/r-source/blob/060f8b64a3a8e489d8684c18b269eea63f182e73/src/include/Defn.h#L184 and the internals are documented here: https://cran.r-project.org/doc/manuals/r-release/R-ints.html#SEXPs

As much fun as it is to figure these things out, I have never had 
sufficient time or motivation to do so.


More information about the Digitalmars-d-announce mailing list