예제 #1
0
        /// <summary>
        /// Creates list of objects in R from list of .NET objects
        /// </summary>
        /// <typeparam name="T">.NET object type</typeparam>
        /// <param name="session">R session</param>
        /// <param name="name">Name of the variable to assign the R list to</param>
        /// <param name="list">List of .NET objects</param>
        /// <param name="cancellationToken">Cancellation token</param>
        /// <exception cref="ArgumentNullException" />
        /// <exception cref="ArgumentException" />
        /// <exception cref="REvaluationException" />
        /// <exception cref="OperationCanceledException" />
        /// <exception cref="RHostDisconnectedException" />
        public static Task CreateListAsync <T>(this IRHostSession session, string name, IEnumerable <T> list, CancellationToken cancellationToken = default(CancellationToken))
        {
            Check.ArgumentNull(nameof(session), session);
            Check.ArgumentNull(nameof(name), name);
            Check.ArgumentNull(nameof(list), list);

            var rlist = list.ToRListConstructor();

            return(session.ExecuteAsync(Invariant($"{name.ToRStringLiteral()} <- {rlist}")));
        }
예제 #2
0
        /// <summary>
        /// Used to save image, produced by R
        /// </summary>
        /// <param name="fileName">Specify image name</param>
        /// <param name="width">Image width</param>
        /// <param name="height">Image height</param>
        /// <param name="dpi">Dots per inch</param>
        public static async Task <bool> SavePlotImage(IRHostSession session, string fileName, int width, int height, int dpi)
        {
            byte[] dataImgArray = await session.PlotAsync("forec", width, height, dpi);

            Bitmap dataImgFile;

            using (MemoryStream memoryStream = new MemoryStream(dataImgArray))
            {
                dataImgFile = new Bitmap(memoryStream);
            }
            dataImgFile.Save(fileName);
            return(true);
        }
예제 #3
0
        static void Main(string[] args)
        {
            // Init R session
            IRHostSession session          = RHostSession.Create("Test");
            Task          sessionStartTask = session.StartHostAsync(new RHostSessionCallback());

            sessionStartTask.Wait();

            // Simple output from console
            Console.WriteLine("Arbitrary R code:");
            var result = session.ExecuteAndOutputAsync("Sys.info()");

            result.Wait();
            Console.WriteLine(result.Result.Output);

            // Create R DataFrame
            List <string> colNames = new List <string>(new string[] { "c1", "c2" });
            List <string> rowNames = new List <string>(new string[] { "1", "2", "3", "10" });

            var xx   = new object[] { new object[] { 1, 3, 43, 54 }, new object[] { "a", "c", "a", "d" } };
            var list = new List <IReadOnlyCollection <object> >();

            foreach (object o in xx)
            {
                list.Add(o as object[]);
            }

            DataFrame df  = new DataFrame(rowNames, colNames, list.AsReadOnly());
            var       dtc = session.CreateDataFrameAsync("data", df);

            dtc.Wait();

            // Print data frame in R session
            result = session.ExecuteAndOutputAsync("print(data)");
            result.Wait();
            Console.WriteLine("\nR data frame:");
            Console.WriteLine(result.Result.Output);

            var resultList = session.GetListAsync("list(mean(data$c1), 111)");

            Console.WriteLine("\nList elements returned from R to C#:");
            Console.WriteLine(Convert.ToDouble(resultList.Result[0]) * 100);
            Console.WriteLine(resultList.Result[1]);

            session.StopHostAsync();
            Console.ReadLine();
        }
예제 #4
0
        /// <summary>
        /// Creates R data frame from .NET <see cref="DataFrame"/>
        /// </summary>
        /// <param name="session">R session</param>
        /// <param name="name">Name of the variable to assign the R list to</param>
        /// <param name="df">.NET data frame</param>
        /// <param name="cancellationToken">Cancellation token</param>
        /// <exception cref="ArgumentNullException" />
        /// <exception cref="ArgumentException" />
        /// <exception cref="REvaluationException" />
        /// <exception cref="OperationCanceledException" />
        /// <exception cref="RHostDisconnectedException" />
        public static async Task CreateDataFrameAsync(this IRHostSession session, string name, DataFrame df, CancellationToken cancellationToken = default(CancellationToken))
        {
            Check.ArgumentNull(nameof(session), session);
            Check.ArgumentNull(nameof(name), name);
            Check.ArgumentNull(nameof(df), df);

            await session.ExecuteAsync(Invariant($"{name.ToRStringLiteral()} <- {df.ToRDataFrameConstructor()}"));

            if (df.RowNames != null && df.RowNames.Count > 0)
            {
                await session.ExecuteAsync(Invariant($"rownames({name}) <- {df.RowNames.ToRListConstructor()}"));
            }
            if (df.ColumnNames != null && df.ColumnNames.Count > 0)
            {
                await session.ExecuteAsync(Invariant($"colnames({name}) <- {df.ColumnNames.ToRListConstructor()}"));
            }
        }
예제 #5
0
        //private ILoggerFacade logger;

        public ROperations(IRHostSession session)
        {
            rHostSession = session;
            //logger = loggerFacade;
        }
예제 #6
0
 public RSessionApiTest()
 {
     _callback = Substitute.For <IRHostSessionCallback>();
     _session  = RHostSession.Create(nameof(RSessionApiTest));
 }
예제 #7
0
 private void CreateHostSession()
 {
     _session          = RHostSession.Create("Test");
     _sessionStartTask = _session.StartHostAsync(new RHostSessionCallback());
 }
예제 #8
0
 public RHostClientCaller(string scriptPath)
 {
     this.scriptPath = scriptPath;
     session         = RHostSession.Create("Test");
     task            = session.StartHostAsync(new RHostSessionCallback());
 }
예제 #9
0
 private RSession(string sessionName)
 {
     Session = RHostSession.Create(sessionName);
 }
예제 #10
0
        /// <summary>
        /// Retrieves R type name for an object
        /// </summary>
        /// <param name="session">R session</param>
        /// <param name="expression">Expression to evaluate</param>
        /// <param name="cancellationToken">Cancellation token</param>
        /// <returns>R type name</returns>
        public static async Task <string> GetTypeNameAsync(this IRHostSession session, string expression, CancellationToken cancellationToken = default(CancellationToken))
        {
            var info = await session.GetInformationAsync(expression, cancellationToken);

            return(info.TypeName);
        }
예제 #11
0
        /// <summary>
        /// Retrieves length of R object
        /// </summary>
        /// <param name="session">R session</param>
        /// <param name="expression">Expression to evaluate</param>
        /// <param name="cancellationToken">Cancellation token</param>
        /// <returns>Object kength</returns>
        public static async Task <int> GetLengthAsync(this IRHostSession session, string expression, CancellationToken cancellationToken = default(CancellationToken))
        {
            var info = await session.GetInformationAsync(expression, cancellationToken);

            return(info.Length);
        }