質問

Background

I am using RDotNet to run an R script that performs a voronoi tessellation using the deldir package. After R:tiles = tile.list(voro) I wish to extract R:tiles[[i]][c("x","y")] for the each tile i into a C#:List<Tuple<double,double>>.

Issue 1

I can extract the R:tiles object into C#-world using var tiles = engine.Evaluate("tiles").AsVector().ToList(); but I am struggling to understand how to use RDotNet to extract the x, y values for each tile from this point:

enter image description here

I don't know how to iterate over this object to extract the x, y values that I desire.

Issue 2

Alternatively, I attempted to create a new simpler object in R, i.e. values and attempt to extract a string and parse values from that. So far I have only created this object for one of the points:

R: e.g.

values <- tiles[[1]][c("x","y")]

C#: e.g.

var xvalues = engine.Evaluate("values[\"x\"]").AsCharacter();
var yvalues = engine.Evaluate("values[\"y\"]").AsCharacter();
// Some boring code that parses the strings, casts to double and populates the Tuple

However I can only extract one string at a time and have to split the string to obtain the values I'm after. This does not seem like the way I should be doing things. enter image description here

Question

How can extract the x,y coordinates for every tile from R:tiles[[i]][c("x","y")] into a C#:List<Tuple<double,double>>?

役に立ちましたか?

解決

I think you are after something like the following if I got what you seek correctly. The full code I tested is committed to a git repo I've just set up for SO queries. I've tested against the NuGet package for 1.5.5; note to later readers that subsequent versions of R.NET may let you use other idioms.

var res = new List<List<Tuple<double, double>>>();
// w is the result of tile.list as per the sample in ?tile.list
var n = engine.Evaluate("length(w)").AsInteger()[0];
for (int i = 1; i <= n; i++)
{
    var x = engine.Evaluate("w[[" + i + "]]$x").AsNumeric().ToArray();
    var y = engine.Evaluate("w[[" + i + "]]$y").AsNumeric().ToArray();
    var t = x.Zip(y, (first, second) => Tuple.Create(first, second)).ToList();
    res.Add(t);
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top