Converting Arrays inside of arrays
dguidara opened this issue · 1 comments
I have an API that returns an Object[] that contains inside of it double[,]. I can get the outer Object[] but I am not sure how to call the converter to turn the double [,] into the matrix.
clrLoadAssembly("Test.dll")
testClassName <-"Test.Test"
(testObj <- clrNew(testClassName))
clrCall(testObj,"CreateData")
[[1]]
NULL
How do I unpack that data? I tried looking for a way to get a handle to the converter class and call it on the elements. I am new to R and come mainly from a .Net background. The array can have either doubles or strings inside of it.
Test Class:
using System;
namespace Test
{
public class Test
{
public Object[] CreateData()
{
var temp = new Double[2, 3];
temp[0, 0] = 1;
temp[0, 1] = 2;
temp[0, 2] = 3;
temp[1, 0] = 4;
temp[1, 1] = 5;
temp[1, 2] = 6;
var retVal = new object[] { temp };
return retVal;
}
}
}
I figured out a workaround as follows:
Create a helper method that access the elements within the object[] and returns an Object. From there the built in conversion works fine;
private readonly object[] _data;
public Test()
{
_data = CreateData();
}
public object GetItemAt(int index)
{
return _data[index];
}
public Object[] CreateData()
{
var temp = new Double[2, 3];
temp[0, 0] = 1;
temp[0, 1] = 2;
temp[0, 2] = 3;
temp[1, 0] = 4;
temp[1, 1] = 5;
temp[1, 2] = 6;
var retVal = new object[] { temp };
return retVal;
}
testData <- clrCall(testObj,"GetItemAt",as.integer(0))
testData
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
Pretty Cool! I realized that array object[] had elements there were NULL, so I came up with this approach.
Let me know if there is a better way to do it. I am not sure how to add my own custom conversion.