Possible to return character vector from R bridge?

Posted By: JamesHH

Possible to return character vector from R bridge? - 04/08/19 19:25

I want to get a list of symbols to trade from an R function.

However, it seems that R bridge only supports returning an int, var or vararray from a call to an R function.

Is it possible to get something like a char array?
Posted By: jcl

Re: Possible to return character vector from R bridge? - 04/09/19 07:25

Are the symbols from a fixed set? You could then return an index in stead of the symbol name.
Posted By: JamesHH

Re: Possible to return character vector from R bridge? - 04/09/19 20:04

Originally Posted By: jcl
Are the symbols from a fixed set? You could then return an index in stead of the symbol name.


Yes, I had thought of that.

But still, it would be even simpler if I did not have to index the symbols.
Posted By: JamesHH

Re: Possible to return character vector from R bridge? - 04/22/19 22:48

The reason the R bridge on supports a very limited set of return types is because it is a wrapper around the mt4 R interface, which only supports int, double and double vector types.

Indexing the symbols, and then sharing this information between R and C seems like an unpleasant kluge. Also, I might need string vectors from R for other purposes.

So I wrote my own "extension" of the R bridge that supports this. I used quotes because it involves "cheating" by passing values back from R using a JSON file.

Here is the example script demonstrating, in case anyone is interested:


#include <r.h>
#include <Rcom.c>

function main()
{
if(!Rstart()) {
printf("Error - R won't start!");
return;
}

// Initialize Rcom
Rcom_init();

// Example: Rstrings
// Get a vector of strings of unknown length:
// Listing of ZorroFolder
char* expression = strf("dir('%s')", slash(ZorroFolder));
char** zorro_dir;
int max_strings = 10000;
int num_files = Rstrings(expression, &zorro_dir, max_strings);
printf("n%d files:", num_files);
int i;
for (i = 0; i < num_files; ++i)
printf("n%s", zorro_dir[i]);

// Example: Rsv
// Get a vector of strings of known length:
// Column names of mtcars
Rx("data(mtcars)");
int ncols = Rd("ncol(mtcars)");
char** col_names = malloc(ncols * sizeof(char*));
Rsv("colnames(mtcars)", col_names, ncols);
printf("nnmtcars has %d columns:", ncols);
for (i= 0; i < ncols; ++i) {
printf("n%s", col_names[i]);
}

// Example: Rs2
// Get a single string:
// digest of mtcars
// Note that Rs is already defined in r.h for debugging purposes.
Rx("{ install.packages('digest', repos = 'https://cloud.r-project.org'); library(digest) }");
char* digest = Rs2("digest(mtcars)");
printf("nndigest of mtcars: %s", digest);

return;
}
Posted By: jcl

Re: Possible to return character vector from R bridge? - 04/23/19 05:53

Thank you for the solution. If R had an equivalent to the C typecast, sending symbols would be simple, by using a double vector. If you know a way to store characters in a double in R, we could implement a direct Rs function. But I think it's probably not possible.
Posted By: AndrewAMD

Re: Possible to return character vector from R bridge? - 04/23/19 16:39

One alternative might be to use rcpp to send a string to a Windows named pipe, and then retrieve it zorro-side.
Posted By: JamesHH

Re: Possible to return character vector from R bridge? - 04/23/19 16:56

The example code above requires my Rcom.c header. I can submit it (I think there is a place for user contributed code, besides pasting to the chat?) if anyone is interested.

My solution is adequate for my purposes. The only drawback to writing to a JSON file that I can see is that it is slow.

It actually is possible for R to store strings as double vectors, since R has access to C via Rcpp. Though I personally don't have the motivation to implement this, since it would take a bit of work.

Posted By: JamesHH

Re: Possible to return character vector from R bridge? - 04/23/19 16:59

Originally Posted By: AndrewAMD
One alternative might be to use rcpp to send a string to a Windows named pipe, and then retrieve it zorro-side.


Sounds promising, but I don't know any Windows programming myself. (I might not have even tried Zorro if it weren't for the fact that it runs on Linux using WINE.)
© 2024 lite-C Forums