Newbie question on data conversion (R to Clr)

Sep 15, 2015 at 5:54 PM

First off, thank you for the 'rClr' package, it seems to be the solution I need to integrate some C# functionality into R. I am not familiar at all with C#, but I need to use a C# .dll within R and I don't have access to the source code. I was able to load the dll into R and create an object with the 'clrNew' command. The object has a field I am trying to assign values to, but I can't quite figure out how to do so (it's probably really simple).

If I use the 'clrGet' command on that field, it looks like a pointer (to a list of doubles) is returned:
Formal class 'cobjRef' [package "rClr"] with 2 slots
..@ clrobj :<externalptr>
..@ clrtype: chr "System.Collections.Generic.List`1[[System.Double[], mscorlib, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089"| truncated

My question is how to set this field in the original object to a vector of double values from R? I hope the question is clear. Any help would be greatly appreciated.
Sep 16, 2015 at 3:07 PM
Ok so after playing around with this a bit, I was able to assign the clrobj values in the fields, but for only simple data types using the 'clrSet' function. However, I think some of the objects require a multi-dimensional array stored in the field with different data types in the columns (like the example where it wants a list). Unfortunately, I can't verify this at the time since the dll is not documented that well and I don't have the source code right now. Is it possible to assign a multi-dimensional array to a clrobj field using a straight clrSet call from R? Do I need to write some intermediate code in C# to handle this (maybe to piece together the array and assign it in C#)? Thanks again for any help and apologies if my question is not clear.
Sep 19, 2015 at 5:12 AM
You may try to pass something like
list(a=1:3, b=4:6)
from R, and see whether the converter gets it right. Cannot remember top of my head.

Usually given a C# data type, there is a natural candidate to convert to in R (because .NET is strongly typed); the other way around is occurring less often, and context dependent. While it is now possible to automate conversion via rClr of more complex types, I am wary of introducing ambiguities or confusions. I myself have met with cases where a conversion of C# dictionaries, convenient in one context, prevented the desired behavior in another.

If you need to pass more complex types, you may want indeed to have a thin wrapper in C# around the library you access, with simpler data types passed into it, and access your wrapper from R.

See also this post
Oct 8, 2015 at 6:39 PM
Thank you for your response! I was away for a bit on this and have come back. I have the source code now so I was able to determine that what I was trying to set was a multidimensional array [,] either of strings or doubles. I wasn't able to pass directly a matrix through R using the clrSet function to set the double[,] or string[,]. I have written a wrapper in C# to help do this.

One odd thing I have seen is that when the return type is string[,], I get an R object that cannot be printed to the console or viewed in the RStudio viewer. I can't cast it to a data frame either. Is this a pointer object of some type (I can't do a str() on it)? Or is it just what happens for a return type string[,]? I
Error: 'getCharCE' must be called on a CHARSXP
Error in is.factor(x) :
Value of SET_STRING_ELT() must be a 'CHARSXP' not a 'NULL'
I can get the dimensions of the result, and I can get what is inside (unless a NULL was returned by C#):
[1] 13 2
[1] "assetname"
Any help or insight on this would be appreciated as well. Thank you.