stackoverflow1453

Programming&&Games&&Life

Archive for R

Calling R functions from C# program using RDotNet Library

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using RDotNet;

namespace WindowsFormsApplication18
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            REngine.SetEnvironmentVariables();            
            REngine engine = REngine.GetInstance();

            engine.Evaluate("require(stats)");            
            engine.Evaluate("plot(sin, -pi, 2*pi)");
            
        }
    }
}

How to turn .csv file to .xts object?

data <- read.table("C:/Users/USERNAME/Desktop/saatlikRSI.csv", header=TRUE, sep = ",")
values<-data$KZ
dates <- data$TAR
datess <- as.POSIXct(dates, format = "%d.%m.%Y %H:%M")
library(xts)
myxts <- as.xts(values, order.by = datess)
plot(myxts)

Calling R functions from C# program using RDotNet Library

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RDotNet;

namespace ConsoleApplication32
{
    class Program
    {
        static void Main(string[] args)
        {
            REngine.SetEnvironmentVariables();
            // There are several options to initialize the engine, but by default the following suffice:
            REngine engine = REngine.GetInstance();
          
            engine.Evaluate("dt<-c(3,3,4,4,5,5,6,6)");
            engine.Evaluate("h<-hist(dt)");
            engine.Evaluate("h");
           
            Console.ReadLine();
        }
    }
}