Sharper is a R package which allows you to execute and interact with dotnet
assemblies from your R environment.
The goal of this package was driven by the idea to use production and industrial code implemented in dotnet
into a R environment, gets results in R to process statistics exploration, analysis or research.
The package provides you tools to interact with your dotnet
assemblies. A large basic data conversions between R and dotnet
are included and the package allows you to enrich or override data converters. A part of those conversions are based on R.Net project. Thanks to the creator Jean-Michel Perraud and all contributors on this repository.
R and dotnet
have different language paradigms. R is a functional language and dotnet
is an OOP (Oriented Object Programming). You will probably need to manipulate objects in your R environment. This is why the package also provides a R6
class generator from dotnet
classes.
You can install the latest version of sharper from CRAN:
install.packages("sharper") # Not available yet
or the development version from GitHub using devtools
:
If you are on linux be sure to have curl installed first.
devtools::install_github("fdieulle/sharper")
then load the package
library(sharper)
By default the package download and install a minimal dotnet core
environment during the installation.
But you can update or install other dotnet core
version as follow:
install_dotnetCore() # Install the latest dotnet core app version
For more details about dotnet
version please see the Microsoft .NET Core. and also the ?install_dotnetCore
to see how to proceed.
Once the installation is proceed the settings will be saved and your chosen dotnet
environment will be automatically loaded when the package will with library(sharper)
The function netLoadAssembly(filePath)
loads a .Net assembly in the CLR (Common Language Runtime).
If your custom library contains other assemblies as dependencies, I advise you to use this following code to load all of them.
library(sharper)
path <- "Your path where dlls are stored"
assembiles <- list.files(path = path, pattern = "*.dll$|*.exe$", full.names = TRUE)
lapply(assemblies, netLoadAssembly)
When the sharper
package is loaded it contains by default all the .Net framework and access on your GAC stored assemblies. So you don't need to load them manually.
Once your assemblies are loaded in your process you can start to interact with them. A simple entry point is to use static call. The package provides you 3 functions for that
netCallStatic(typeName, methodName, ...)
: Call a static method for a given .Net type namenetGetStatic(typeName, propertyName)
: Gets a static property value from a .Net type namenetSetStatic(typeName, propertyName, value)
: Sets a static property value from a .Net type name
For more details about the static interactions see
The package allows you to create and manage .Net objects from R. A .Net object is stored in an R externalptr
which represents the address in the CLR.
To create and manage .Net objects from R you can use the following R functions:
netNew
: Create an object from its .Net type namenetCall
: Call a member method for a given external pointer of a .Net object.netGet
: Gets property value from a given external pointer of .Net object.netSet
: Gets property value from a given external pointer of .Net object.
For more details about the static interactions see
To easily manipulate this .Net objects you can wrap dotnet
objects into a R6 base class named NetObject
. This class provides you some function as follow:
get
: Gets a property valueset
: Sets a property valuecall
: Call a member methodas
: Cast the currentR6Class
to anotherR6Class
which has to inherit fromNetObject
by keeping the sameexternalptr
(dotnet
object pointer).
For more details about the static interactions see
Because .Net is an OOP language you can have a lot of classes to use in your R scripts. The package provides you a function which automatically generates for you R6 classes from .Net types.
netGenerateR6(typeNames, file)
: Generate R6 classes from .Net types
This generator respects the class hierarchy and also generate an roxygen2
syntax for your custom package documentations. roxygen2
supports R6 class documentation since the version 7.
During the development step of your projects it's always helpful to debug your code. the .Net code can be easily debugged with your Visual Studio or another IDE. For R I like to use the restorepoint
.