How would I convert a character vector into a message? C# R.NET

200 views Asked by At

Here is my current code:

private string result;
        private string result1;

        public async Task calCasync(string ar)
        {
            REngine engine;
            REngine.SetEnvironmentVariables();
            engine = REngine.GetInstance();
            engine.Initialize();
            CharacterVector vector = engine.Evaluate(ar).AsCharacter();
            result = vector[0];

            ReplyAsync("> " + result);
            try
            {
                result1 = String.Join(" ", vector.Cast<int>());
                ReplyAsync("> ");
                ReplyAsync("ad> " + result1);
            }
            catch(Exception e)
            {
                Console.WriteLine(e);
            }
        }`

I have tried to modify some parts of it to make it work, but it only responds with 'result' and not 'result1'. In this I would like to convert a matrix or a vector into a string which I can use for further things.

result = vector[1]; 

or higher gives a error saying that it is too high. Every time executed result1 = String.Join(" ", vector.Cast<int>()); gives a error. I have not found any information online on this topic. If it does help I am using visual studio 2017.

Thanks -Yan

3

There are 3 answers

0
Saragada Ramesh On

Here is my Code.

Open the R Editor and type the below code:

characters <- c("H","E","L","L","O")
stringdata <- paste(characters,collapse="")

Type characters in R console window Then

Output : "H" "E" "L" "L" "O"

Type stringdata in R console window Then

Output : "HELLO"

for paste function in R refer the below documentation

https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/paste

0
Yan Ivanov On

The way I solved it was by using

CharacterVector vector1 = engine.Evaluate(ar).AsCharacter();
    string[] resp1 = engine.Evaluate(ar).AsCharacter().ToArray();

then using

result1 = ConvertStringArrayToString(resp1);
0
de boer shapes On

You can turn it into a string with a loop. The indices start at i=0. so you only go up to the (Length-1), thus the condition i<Length.

        //evalutate R code
        CharacterVector vector = engine.Evaluate(_r_code).AsCharacter();
        //string to hold r result
        string result="";
        for (int i=0; i<vector.Length; i++){
            if (i==0){
                result=vector[i];
            }else{
                result=result+" "+vector[i];
            }
        }