Beispiel #1
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();
        }
Beispiel #2
0
        public async Task CreateDataFrame(string[] rowNames, string[] colNames, object[] data)
        {
            var list = new List <IReadOnlyCollection <object> >();

            foreach (object o in data)
            {
                list.Add(o as object[]);
            }
            var original = new DataFrame(rowNames, colNames, list.AsReadOnly());
            await _session.CreateDataFrameAsync("x", original);

            var rdf = await _session.GetDataFrameAsync("x");

            rdf.RowNames.Should().HaveCount(rowNames.Length).And.ContainInOrder(rowNames);
            rdf.ColumnNames.Should().HaveCount(colNames.Length).And.ContainInOrder(colNames);
            rdf.Data.Should().HaveCount(colNames.Length);
            rdf.Data.First().Should().HaveCount(rowNames.Length).And.ContainInOrder(data[0] as object[]);
        }
Beispiel #3
0
 public async Task CreateDataFrameAsync(string name, DataFrame dataFrame)
 {
     //logger.Log("Creating DataFrame: "+name, Category.Info, Priority.None);
     await rHostSession.CreateDataFrameAsync(name, dataFrame);
 }