This project is read-only.

Returning a time series to .net using r packages xts or zoo.

Jan 13, 2015 at 3:19 AM
I am trying to call a function in R that can return a time series as tx, xts, or zoo format. In all cases, does not return the dates. It will only return the stock prices.

The following code returns null

command = "IndexValues <- Quandl(\"EOD/SPY\", authcode=\"myauthcode\", start_date=\"2014-01-01\", type = \"xts\")",
DataFrame df = rEngine.Evaluate(command).AsDataFrame(); // returns null
var t1 = rEngine.GetSymbol("IndexValues").AsDataFrame(); // returns null

If I change the type to "zoo", or "raw" or "ts", they all return null. If I cast to a list, it will return the stock prices without dates.

Any ideas?
Jan 14, 2015 at 7:39 AM
Edited Jan 14, 2015 at 8:00 AM
My modification of your sample code looks like this:
static void Main(string[] args)
    var rEngine = REngine.GetInstance();
    const string command = "library(xts); IndexValues <- xts(rnorm(10), Sys.Date()+1:10)";
    var df = rEngine.Evaluate(command).AsDataFrame();
You are correct, RDotNet is returning null. The issue is that IndexValues is an 'xts' object and is not a dataframe. I assume that you expected RDotNet to coerce the type for you in the same way '' would, but it does not.

As currently implemented, RDotNet's .AsDataFrame() exists to let the C# representation of the "IndexValues" object know that it is a C# DataFrame if and only if its already a R DataFrame.

You can see that for yourself here:

As is, I don't see a simple way to coerce a type to a dataframe from the C API, so your best bet is to call '' from the R code.
Jan 14, 2015 at 3:49 PM
Thank you for your reply. That is what I was thinking yesterday. I modified my code and determined that when converted to a dataframe, sees dates as a numericvector:
        string command = "IndexValues <- xts(rnorm(10), Sys.Date()+1:10)";

        command = "data.frame(Date = index(IndexValues), IndexValues, row.names=NULL)";
        var df1 = rEngine.Evaluate(command).AsDataFrame();
df returns two columns, both numeric vectors. It does not see the date field as a date. I understand the number is an offset from 1/1/1970, but unless I know to look for the date fields by name, I can't programmatically create a generic import into a dataset. I think I am missing something easy. Any help would be appreciated. Thanks again.
Jan 15, 2015 at 12:47 AM
Its not that easy, getting types sent from R over to C# when the C# side can't predeclare them is difficult. I know there's some dynamic stuff for DataFrames, but I know little about it. On the static side, you need to do something like this. I'm sure there's alternative ways to make this work, but here's mine:
public class RowData
    public double Date { get; set; }

    public double IndexValues { get; set; }
private static void Main(string[] args)
    var rEngine = REngine.GetInstance();
    const string command = @"library(xts); IndexValues <- xts(rnorm(10), as.POSIXlt(Sys.Date()+1:10));
                            data.frame(Date = index(IndexValues), IndexValues, row.names=NULL)";
    var df1 = rEngine.Evaluate(command).AsDataFrame();

    var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
    var dates = df1.GetRows<RowData>().Select(s => epoch.AddSeconds(s.Date)).ToList();

Jan 15, 2015 at 4:15 AM
Not much to add to what @skyguy94 has in terms of sample code. I would highly recommend that for date-time structures you use with R.NET that you:
  • Use POSIXct date/times
  • Use UTC date/times
For triangulation, code similar in spirit to the above can be found in:
 public static DateTime CreateDateFromRPOSIXct(double rDateNumericValue);
In my day job I work a lot with time series, and I like xts. I think I considered improving R.NET facilities, but refrained from it knowing what a can of worm date-time handling is. I spent a lot of time adding support for date-time and time zones (and its unit testing) in the rClr package. Basically my intent is to port only the very unambigious part to R.NET, and I think that may mean only facilities for UTC annd POSIXtc. Predictability and correctness before convenience.

If you want more details and pointers to information about date and time systems, you can skim through
Jan 15, 2015 at 10:22 AM
@jmp75 Has anyone ever thought about adding a TypeConverter parameter to the attribute? It seems like a canonical way to reduce some of the awkwardness of pushing the conversion directly on the calling code.
Feb 2, 2015 at 1:30 PM
This code evaluate a list of predicted data. But why can't I print each of them in console in numeric format?
              const string command = "library('forecast'); birth <-scan(''); fit <- nnetar(birth); fcast <- forecast(fit); fcast";
              var list = engine.GetSymbol("fcast").AsList();
              foreach (var x in list)
Feb 2, 2015 at 6:50 PM
Seem like this code returns 10 predicted values correctly
              const string command = "library('forecast'); birth <-scan(''); fit <- nnetar(birth); fcast <- forecast(fit); predict <- fcast$mean;";
              var list = engine.GetSymbol("predict").AsNumeric();
              foreach (var x in list)