Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Converting Arrays inside of arrays #44

Open
dguidara opened this issue Jun 16, 2019 · 1 comment
Open

Converting Arrays inside of arrays #44

dguidara opened this issue Jun 16, 2019 · 1 comment

Comments

@dguidara
Copy link

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;

    }
}

}

@dguidara
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant