This presents a few data conversions between R and the CLR (.NET or Mono). You will find the C# and R samples for this tutorial under the doc folder in the source code of https://r2clr.codeplex.com/

TODO: package tutorials for a separate download
R script: r2clr\doc\TutorialSimpleData.r

#### https://r2clr.codeplex.com/
rclrRoot = 'f:/codeplex/r2clr' # set this to your own location

library(rClr)
clrInit()

# Use the couple of following lines if you want to run basic unit tests prior to undertaking the tutorial.
# library(testthat)
# test_package('rClr')

# clrLoadAssembly(dllFilename=file.path( rclrRoot, 'doc/DataConversionSample/bin/Release/DataConversionSample.dll'))
# or
clrLoadAssembly(dllFilename=file.path( rclrRoot, 'doc/DataConversionSample/bin/Debug/DataConversionSample.dll'))



The package includes some facilities to discover what CLR assemblies are loaded, and what types (classes and value types) are exported by these assemblies.

clrGetLoadedAssemblies()
# [1] "mscorlib"              "ClrFacade"             "System.Core"           "System"               
# [5] "RDotNet"               "RDotNet.NativeLibrary" "DataConversionSample" 

typename = clrGetTypesInAssembly('DataConversionSample')
typename = "rClr.Samples.DataConversionSample,DataConversionSample"
obj = clrNew(typename)
obj
clrReflect(obj)

# $Methods
# [1] "DateTimeToString"     "Equals"               "GetDate"              "GetHashCode"          "GetOneDimArray"      
# [6] "GetOneDimStringArray" "GetRectArray"         "GetType"              "ToString"            
# $Fields
# character(0)
# $Properties
# character(0)



The conversion of some important basic types is supported, numeric vectors are translated naturally between R and the CLR. Dates are a particular numeric type for R, and are converted to date time objects in .NET, as expected. Matrices are currently supported from the CLR to R, but not yet the other way around.

clrGetMemberSignature(obj, "GetDate")
# [1] "Static, Method: DateTime GetDate, String"

str(clrCallStatic(typename, "GetDate", '2001-02-03'))
# Date[1:1], format: "2001-02-03"

str(clrCallStatic(typename, "DateTimeToString", as.Date('2001-02-03')))
#  chr "3/02/2001 12:00:00 AM"

str(clrCallStatic(typename, "GetOneDimStringArray", as.integer(5)))
#  chr [1:5] "0" "1" "2" "3" "4"

str(clrCallStatic(typename, "GetOneDimArray", as.integer(5)))
#  num [1:5] 0 1.1 2.2 3.3 4.4

str(clrCallStatic(typename, "GetRectArray", as.integer(3), as.integer(5)))
# num [1:3, 1:5] 0 1.1 2.2 0.22 1.32 2.42 0.44 1.54 2.64 0.66 ...

# What a .NET object looks like
clrCallStatic('System.Environment', 'get_Version')
# Dictionaries are not yet converted to R lists, by default.
clrCallStatic('System.Environment', 'GetEnvironmentVariables')

Last edited Feb 25, 2013 at 11:46 PM by jperraud, version 4

Comments

screig Jun 30, 2015 at 5:05 PM 
> library(rClr)
Loading the dynamic library for Microsoft .NET runtime...
Loaded Common Language Runtime version 4.0.30319.34209

> clrInit()
Error: could not find function "clrInit"

BerndMayer Apr 11, 2014 at 4:07 PM 
Sorry for asking a maybe obvious question, but I simply can't figure out where to download "DataConversionSample.dll".
On this Website I clicked on "Source Code" and navigated to "doc/DataConversionSample". But there is no subfolder "/bin/Release/" with the dll.