Introducing 'dotnet' knitr engine for C# & F# chunks in R Markdown

I had a thought “wouldn’t it be cool to do a blog post about Bayesian inference with Infer.NET?” and then a follow-up thought “wouldn’t it be even cooler to have the probabilistic programs as R Markdown chunks that would be actually built/compiled and then run/executed just like Python and Julia chunks would be?”

And that’s how I ended up spending an evening learning how to make custom language engines for {knitr} and making one for C# and F# languages.

Under the hood, {dotnet} features a simple R wrapper for the dotnet command line interface that is available after installing the .NET SDK.

Currently it is only available from GitHub and will stay that way indefinitely. You can install it in R with the {remotes} package:

# install.packages("remotes")
remotes::install_github("bearloga/dotnet")

Once installed, it needs to be registered with knitr in your R Markdown documents:

dotnet::register_engine()

Once registered, chunks can use the dotnet engine. Those chunks can have either C# or F# code, which will be made into .NET applications and run:

```{dotnet}
using System;
```

For example:

using System;

class Program {
  static void Main(string[] args) {
    Console.WriteLine("Hello from R Markdown!");
  }
}
## Hello from R Markdown!

For more examples see the source document for the package’s README and the two vignettes:

The package is released as open source and is licensed under the BSD 2-Clause License.

Posted on:
June 10, 2020
Length:
2 minute read, 255 words
Tags:
R
See Also:
Wikipedia Preview for R Markdown documents
Even faster matrix math in R on macOS with M1
Making Of: Session Tick visualization