Welcome to our newest blog segment, Featured Blog Friday, with blogs written by AGR Dynamics employees about their interests and current projects! We are kicking off this segment with our SQL consultant, Helga Ingimundardóttir, and her “shrimp project” as it has been affectionately referred to around the office. Feel free to comment and/or ask questions below or tweet to Helga (@tungufoss).

Recently I had the opportunity of updating software from the early years of AGR Dynamics‘ history, from the time when the company was called Bestun & Ráðgjöf (e. Optimisation and Consultancy). The project is called ShrimpMeasure and is an application specially tailored to the needs of Icelandic Export Center Ltd. The application serves as a user interface connected to an A&D EK-2000i Digital Scale. A user weighs individual shrimps from a catch in order to inspect if its weights are within range of predefined output groups.

The figure below shows a screenshot of the collection form, where the emphasis is to easily see:

  • last weight received;
  • total sample size,
  • and gross weight of all samples.


Integrating R


This is done to facilitate the user when collecting data and knowing when to feed the next shrimp to the collected samples as the scale needs to stabilise before introducing the next sample. Afterwards, the report is saved and sent to a SQL server. The interface, which is coded in Visual C#, uses the Entity Framework and with LINQ to Entities it provides Language-Integrated Query (LINQ) support that enables developers to write queries against the Entity Framework conceptual model using Visual C#. With this set-up data is collected and accessed in a systematic way using relational database management system.

Previous version of the ShrimpMeasure software used the native System.Chart class to create histograms of the weight samples. I’m a firm believer that life’s too short for ugly plots, so something needed to be done. As a result, only one thing came to mind, namely integrating R to the project. For those who are unfamiliar with the wonders that is R, it is a free software environment for statistical computing and graphics supported by the R Foundation for Statistical Computing.

A personal hero of mine from the Rstats community is Hadley Wickham, who is the Chief Scientist at RStudio and has a god-like status among data nerds. Hadley has developed several notable and widely used packages including ggplot2, plyr, dplyr, and reshape2. All of which are time-saving (read: life-saving) packages that make coding in R worth the shift in software environment.

In order to use R in your otherwise C# project, you can load the RDotNet library from CodePlex. It is distributed via R.NET.Community on NuGet package manager. RDotNet is an in-process interoperability bridge to R from the .NET Framework. RDotNet requires .NET Framework 4 and the native R DLLs installed with the R

The implementation is relatively easy and straightforward. After loading the package to the project, you update the environment path to let it know where the binary path to R is located on the computer. Then you can introduce variables to the R engine by first declaring the variable (using engine.SetSymbol) and afterwards you can evaluate it with regular R syntax (using engine.Evaluate). The following code demonstrates how easily R can be implemented within C#:


using System;
using System.Linq;
using RDotNet; // load with NuGet

namespace Demo
    class Program
        static void Main(string[] args)
            // set up basics and create RDotNet instance
            String environmentPath = Environment.GetEnvironmentVariable("PATH");
            String binaryPath = @"C:\Program Files\R\R-3.2.2\bin\i386";

            Environment.SetEnvironmentVariable("PATH", environmentPath + System.IO.Path.PathSeparator.ToString() + binaryPath);
            REngine engine = REngine.CreateInstance("RDotNet");
            engine.Initialize(); // required since v1.5                            

            // some random weight samples 
            double[] samples = new double []{ 3.2, 3.6, 3.2, 1.7, 0.8, 2.9, 2, 1.4, 1.2, 2.1, 2.5, 3.9, 3.7, 2.4, 1.5, 0.9, 2.5, 1.7, 2.8, 2.1, 1.2 };

            // introduce the samples into R
            engine.SetSymbol("samples", engine.CreateNumericVector(samples));

            // set the weights as a data frame (regular R syntax in string)
            engine.Evaluate("df <- data.frame(id=c(1:length(samples)), weight = samples)");

            // evaluate and retrieve mean 
            double avg = engine.Evaluate("mean(df$weight)").AsNumeric().ToArray()[0];
            // same for standard deviation
            double std = engine.Evaluate("sd(df$weight)").AsNumeric().ToArray()[0];
            // print output in console
            System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("en-gb");

            Console.WriteLine(string.Format("Weights: ({0})", string.Join(",", 
                samples.Select(f => f.ToString(ci)) // LINQ expression
            Console.WriteLine(string.Format("Sample size: {0}", samples.Length));
            Console.WriteLine(string.Format(ci, "Average: {0:0.00}", avg));
            Console.WriteLine(string.Format(ci, "Standard deviation: {0:0.00}", std));

            // Keep the console window open in debug mode.
            Console.WriteLine("Press any key to exit.");



Now, using the RDotNet engine to run R-commands natively from the C# code, it’s possible to render beautiful ggplot2 figures instead of the previous heinous charts, depicted in the figure below. Moreover, with the simple syntax of ggplot2 it’s possible to offer a variety of plots that would have been too time consuming to implement within the limits of the System.Chart class. Not to mention, less aesthetically pleasing.


Integrating R