Search code examples
c#r.net

How can I see the output of command line in cs file


I am working in .cs file and I need to see the output of some object/variable executed by the c# program. I will use that for debbuging purposes each time.

For example, in the code below, I need to see the output of the variable DataNaiss (type,number of rows,all elements included...).

var DataNaiss = engine.Evaluate("DataNaiss=DataIns[which(cond1 & DataIns$Id %in% ID_Final),'Date.naissance']").AsCharacter();

I tried to Add this code :

Console.WriteLine(DataNaiss.ToString());
Console.ReadLine();

The output is in this picture :

enter image description here

Just see the end line please R.net CharacterVector . It seems that it talk about the type of DataNaiss But where are all elements of it?

In other way, until now I don't know why I have also in the same console window the data of the table DataAch imported knowing that I just call console window for showing DataNaiss and not for other thing !! you can see in the same picture elements of the first table DataAch. my code of importing data is:

 REngine engine = REngine.GetInstance();

 DataFrame DataAch = engine.Evaluate("DataAch=read.table('C:/Users/isalah/Desktop/Fichiers_CRM/Fichier_csv/Achats1.csv',header=TRUE,sep =';',fill =TRUE)").AsDataFrame();
 DataFrame DataDia = engine.Evaluate("DataDia=read.table('C:/Users/isalah/Desktop/Fichiers_CRM/Fichier_csv/Diagnostic.csv',header=TRUE,sep =';',fill=TRUE)").AsDataFrame();
 DataFrame DataCad = engine.Evaluate("DataCad=read.table('C:/Users/isalah/Desktop/Fichiers_CRM/Fichier_csv/CADEAUX.csv',header=TRUE,sep =';',fill=TRUE)").AsDataFrame();
 DataFrame DataIns = engine.Evaluate("DataIns=read.table('C:/Users/isalah/Desktop/Fichiers_CRM/Fichier_csv/Inscrits.csv',header=TRUE,sep =';',fill =TRUE)").AsDataFrame();

How do you explain this please?


Solution

  • Just see the end line please R.net CharacterVector . It seems that it talk about the type of DataNaiss But where are all elements of it?

    Calling .ToString() on a variable won't magically show all the data you need.

    According to Microsoft Documentation you can see that:

    The default implementation of the ToString method returns the fully qualified name of the type of the Object

    Therefor, you can understand that the type of that variable called DataNaiss in your case is actually of type CharacterVector.

    Looking at the source code of R.Net you can obviously see that the class CharacterVector doesn't override ToString method.

    In order to get the elements of the variable DataNaiss you have to access them yourself. The only way I can see from the source code is to access them like DataNaiss[0] since in their source code you have:

    public override string this[int index]
    {
        get
        {
            if (index < 0 || Length <= index)
            {
                throw new ArgumentOutOfRangeException();
            }
            using (new ProtectedPointer(this))
            {
                return GetValue(index);
            }
        }
        set
        {
            if (index < 0 || Length <= index)
            {
                throw new ArgumentOutOfRangeException();
            }
            using (new ProtectedPointer(this))
            {
                SetValue(index, value);
            }
        }
    }
    

    I suggest you can loop over the DataNaiss object according to it's length, and grab the data you need, for example if you have access to Length property:

    for (int i=0; i<DataNaiss.Length; i++)
    {
        Console.WriteLine(DataNaiss[i]);
    }
    

    This is not tested. I am answering all of this according to what I read in their code.