2 registered members (AndrewAMD, Imhotep),
567
guests, and 4
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Strings from Zorro to R
#483727
07/17/21 18:12
07/17/21 18:12
|
Joined: Dec 2014
Posts: 204 Germany
Smon
OP
Member
|
OP
Member
Joined: Dec 2014
Posts: 204
Germany
|
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:
#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
}
}
Last edited by Smon; 07/17/21 19:03.
|
|
|
Re: Strings from Zorro to R
[Re: Smon]
#485307
02/21/22 19:08
02/21/22 19:08
|
Joined: Dec 2021
Posts: 7
budfox
Newbie
|
Newbie
Joined: Dec 2021
Posts: 7
|
Hey Smon, thank you for the helpful post! I'm currently trying to read strings from R into Zorro. I tried using Rd and Rv but I'm just not getting it. I was wondering if you could share a simple example or give me a hint? I included my current status below...
#include <r.h>
typedef struct {
string Asset1;
string Asset2;
} Pairs;
function run()
{
set(PLOTNOW);
StartDate = 2015;
EndDate = 20190101;
BarPeriod = 5 * 1440;
static Pairs Pair1;
// ---------------------------------------
// Startup and data loading
// ---------------------------------------
if(is(INITRUN))
{
printf("Script is running!\n");
// start R
if(!Rstart("Test.R", 2))
{
print("Error - can't start R session!");
quit();
}
}
Rx("testnames <- testfunction()", 2);
Pair1.Asset1 = Rd("testnamen[[1]]"); // Doens't work due to Pointer Error - Can't figure it out
Rv("testnamen", Pair1.Asset1, 2); // No Error but no value in Pair.Asset1
printf("Test Wert 1: %s\n", Pair1.Asset1);
watch("!----\n");
}
R Code:
testfunction <- function() {
return(c("Asset", "Name"))
}
I was trying to store assetnames into a struct containing strings. Thanks for you help!
|
|
|
Re: Strings from Zorro to R
[Re: Smon]
#485312
02/22/22 17:32
02/22/22 17:32
|
Joined: Feb 2017
Posts: 1,718 Chicago
AndrewAMD
Serious User
|
Serious User
Joined: Feb 2017
Posts: 1,718
Chicago
|
Three things: 1) In Zorro, "string" is only a typedef of char*. Do you know how pointers work? 2) Rd is for retrieving a double, yet you're casting a double to a char*. How did you expect this to work? 3) Rv is for retrieving an array of doubles into a user-supplied array of doubles. That's not remotely what you seem to be wanting to do. Read this manual page for details on how the functions work: https://zorro-project.com/manual/en/rbridge.htmFor asset names, you might as well set up key-value pairs on the R side and retrieve assets names by index lookup on the Zorro side. See also this post.
|
|
|
|