knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

This vignette demonstrates the dotnet knitr engine for use in R Markdown chunks that have C# and F# code. First, register the engine with knitr:

dotnet::register_engine()

The example used comes from the two coins tutorial. In this example, which uses the "Microsoft.ML.Probabilistic.Compiler" NuGet package, dotnet add package Microsoft.ML.Probabilistic.Compiler can be accomplished by specifying it in add_packages inside engine.opts chunk option:

```{dotnet, engine.opts = list(add_packages = c('Microsoft.ML.Probabilistic.Compiler'))}
using System;
using Microsoft.ML.Probabilistic.Models;
```

And here is the probabilistic program written in C#:

```{dotnet csharp, engine.opts = list(add_packages = c('Microsoft.ML.Probabilistic.Compiler')), cache = TRUE} using System; using Microsoft.ML.Probabilistic.Models;

class Program { static void Main(string[] args) { Variable firstCoin = Variable.Bernoulli(0.5); Variable secondCoin = Variable.Bernoulli(0.5); Variable bothHeads = firstCoin & secondCoin;

// Inferring distributions:
InferenceEngine engine = new InferenceEngine();
Console.WriteLine("Probability both coins are heads: " + engine.Infer(bothHeads));

// Backwards reasoning:
bothHeads.ObservedValue = false;
Console.WriteLine("Probability distribution over firstCoin: " + engine.Infer(firstCoin));

} } ```



bearloga/dotnet documentation built on April 5, 2022, 5:33 p.m.