Strings from Zorro to R

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:

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
	}
}
Posted By: Grant

Re: Strings from Zorro to R - 08/06/21 13:45

I did something similar using the sprintf function. My main motivation wasn't the code speed, but the fact that I run multi-currency models, hence the PairID variable.


Code
TmpC[256];

sprintf(TmpC, "PredictionsLong <- predict(ModelLong%i%s", PairID, ", ModelCase[1:1,])")
Rx(TmpC, 0);
Posted By: budfox

Re: Strings from Zorro to R - 02/21/22 19:08

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...

Code

#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!
Posted By: AndrewAMD

Re: Strings from Zorro to R - 02/22/22 17:32

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.htm

For 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.
Posted By: budfox

Re: Strings from Zorro to R - 02/23/22 17:28

Hey AndrewAMD,

thank you very much for the quick and detailed answer!

I know how pointers work, but I have problems from time to time to insert them correctly.

Also, I will definitely take another look at the manual! The devil is in the details...

As for your suggestion, that sounds like an even more practical solution to me, thanks for the tip!
© 2024 lite-C Forums