Posted By: Smon
Strings from Zorro to R - 07/17/21 18:12
What is this good for?
I manage data from different assets in R and wanted to reference this data by asset name.
So when I want something from R, I must pass a numerical vector but also the information which asset I want to work with.
I could have done it like this:
1. Step: Pass the asset string with Rx()
2. Step: Pass the numerical data with Rv()
As the R-Bridge is painfully slow, I wanted to reduce this into one single call. This is my solution:
I manage data from different assets in R and wanted to reference this data by asset name.
So when I want something from R, I must pass a numerical vector but also the information which asset I want to work with.
I could have done it like this:
1. Step: Pass the asset string with Rx()
2. Step: Pass the numerical data with Rv()
As the R-Bridge is painfully slow, I wanted to reduce this into one single call. This is my solution:
Code
#ifndef <r.h> #include <r.h> #endif int maxstr2int = 0; // //works just like stridx() but pushes the strings to the R session as well: int str2int(string mystring) { mystring = strx(mystring,"/",""); //I think getting rid of "/" is always a good idea int index = stridx(mystring)+1; //+1 because the first element in a vector in R has index 1, while in C it has index 0 if(maxstr2int == 0) Rx("Zstrings <- c()"); //create Zstrings Vector in R (it doesn't exist yet if maxstr2int == 0) if(index > maxstr2int) { maxstr2int = index; string Rstring = strf("Zstrings[%d] <- '%s'", index, mystring); Rx(Rstring); } return index-1; //By subtracting 1, we get the same return value as with stridx() } function run() { if(!Rstart("", 3)) { print(TO_ANY, "\nError - could not start R session!"); quit(); } while(asset(loop("AUD/JPY","EUR/GBP","GBP/CHF","USD/CHF"))) //just some assets as strings. This will work with any sting (up to 1000 characters) { int index = str2int(Asset); //Access the Strings in R: string Rstring = strf("print(paste('String in R:', Zstrings[%d]))", str2int(Asset)+1); //Error when directly passed to Rx() printf("\nString in C: %s - Below should be the same:", strxid(str2int(Asset))); Rx(Rstring, 3); wait(5000); //Output way out of sync, if we continue too fast. You should really use DebugView when you code hybrid scripts with R } }