Calling R from .NET: a case-study using Rapid NCA, the noncompartmental analysis workflow tool Chris Campbell Agenda • Using .NET • Identify Opportunity • Design Solution • Develop Solution • Connections with R.NET • Complete & Deploy Tool Using .NET What is .NET? • Object-oriented environment to develop applications • Safe execution environment • Choice of programming languages • Framework consisting of: • runtime • class library • Developed with Visual Studio Using .NET Visual Studio • A graphical programming tool (IDE) • Also, Visual Studio Express is a free version Using .NET Choice of languages Tried and tested languages • C# is the main one • F# is a functional language (not a steep learning curve if you know OCaml) • Knowledge of XAML (a Microsoft declarative XML language) is required for state of the art graphics • C++/CLI useful for legacy and bespoke parallel processing (including GPGPU) Other possibilities.. • Vb.Net is very like C# (no advantage over it) • Third parties have added languages to the CLI platform Using .NET An “ajar source” platform Not exactly open source, but… • Most CLI third party languages are open • C# and VB.Net are not – but many open source projects based on them • Microsoft have made F# open source • There are free (Express) versions of Visual Studio for the languages • Compiler is free and other editors / IDEs are available (but not usually preferred by developers) Using .NET Performance and Computation Performance is very good • On graphics (millions of data points will plot with ease and zoom smoothly) • Computation is fast enough in C#, calling R adds little overhead • Standard Maths library is limited; third parties and MS maths for “drawing” are better • Data parallel computation is possible on the desktop (GPGPU) • F# provides further “big data” capabilities A Project Case Study Identify Opportunity • Where can repetitive tasks be automated? • How can regular outputs be generated? • How can we make methods and techniques more accessible to colleagues? Identify Opportunity • Customer needed to send monthly reports to dozens of trial centres • Small team, so time limited • Normally simple noncompartmental analysis, standard report Design Solution • What is the simplest way for the task to be performed? • Which steps can be eliminated? • What are barriers to understanding for users? Design Solution Streamline Workflow What is Needed? • Import data to analysis software • Map variables • Select units • Select non-compartmental • Select model • Select interpolation • Select rules for BLQ • Select terminal phase calculation method • Perform analysis • Choose partial area • Export results • Export plots • Write report • Format report Design Solution Analysis Engine using R Let’s get .Net technical.. Connections with R.NET • What will be provided to R? • What will be returned from R? • What happens if something goes wrong? Connections with R.NET Using the R Service • R.NET allows R calls to be submitted to an R service • R.NET is not the only option • R.NET connects to R down to Expression level • So objects can effectively be passed back into .NET • Recommend a robust .Net framework to do this Connections with R.NET _pluginsManager = new RPluginManager(PluginLocation, RLocation); _pluginsManager.SetActivePlugin(); _session = _pluginsManager.GetSession(); bool sessionOk = _pluginsManager.TryMakeSession(out _session); R is efficiently accessed, via R.Net (as pictured in Visual Studio) via a Plugin (as above) Connections with R.NET “Engineer’s” framework Connections with R.NET Robustness • Function may be passed data outside it’s anticipated structure > checkOrderedVector(c(0, 1, 3, 2, 4), + description = "Time") Error in checkOrderedVector(c(0, 1, 3, 2, 4), description = "Time") : Error: Time is not ordered. Actual value is 0 1 3 2 4 > Connections with R.NET Robustness • The tool expects a certain return object • An error in an R call should be trapped by the communicating function > check01 <- try(checkOrderedVector(Time, + description = "Time"), silent = TRUE) > • Return object passed as normal • An error checking element of the return object can report information about the error Connections with R.NET Using the framework _pluginsManager = new RPluginManager(PluginLocation, RLocation); _pluginsManager.SetActivePlugin(); _session = _pluginsManager.GetSession(); bool sessionOk = _pluginsManager.TryMakeSession(out _session); _session.SetNumericSymbol("TimePtVector", CheckTimePointData(toAnalyse)); _session.SetNumericSymbol("ConcVector", CheckConcentrationPointData(toAnalyse)); var evalString = string.Format("ncaAnalysis(TimePtVector, ConcVector, … MathEngineDataRowDto<double> ncaGetBack = _session.PerformNumericEvaluation(evalString, "ROutput_Error"); _lastErrors = ncaGetBack.ErrorStrings; _session.FlushConsole(); _pluginsManager.RelinquishSession(); A Rapid Solution Complete & Deploy Tool • Can users understand how to use tool? • How confident are we in tool output? Deploy Tool Data Import Map Variables Review Analysis Review Grouping Generate Report Select Report Type Add Group Comments View Report Conclusions • Great graphical interfaces can be built using .NET • Intuitive interactive features are available • R.NET allows R analysis to be accessed as a service • Good coding practice will ensure application is robust • Work on a well engineered framework will be rewarded with desktop solutions created at high speed