6 registered members (TipmyPip, Niels, dBc, Ed_Love, 3run, 1 invisible),
17,577
guests, and 5
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Re: Possible to return character vector from R bridge?
[Re: jcl]
#476829
04/09/19 20:04
04/09/19 20:04
|
Joined: Oct 2018
Posts: 72
JamesHH
OP
Junior Member
|
OP
Junior Member
Joined: Oct 2018
Posts: 72
|
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.
|
|
|
Re: Possible to return character vector from R bridge?
[Re: JamesHH]
#476913
04/22/19 22:48
04/22/19 22:48
|
Joined: Oct 2018
Posts: 72
JamesHH
OP
Junior Member
|
OP
Junior Member
Joined: Oct 2018
Posts: 72
|
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; }
|
|
|
Re: Possible to return character vector from R bridge?
[Re: AndrewAMD]
#476928
04/23/19 16:56
04/23/19 16:56
|
Joined: Oct 2018
Posts: 72
JamesHH
OP
Junior Member
|
OP
Junior Member
Joined: Oct 2018
Posts: 72
|
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.
Last edited by JamesHH; 04/23/19 16:57.
|
|
|
Re: Possible to return character vector from R bridge?
[Re: AndrewAMD]
#476929
04/23/19 16:59
04/23/19 16:59
|
Joined: Oct 2018
Posts: 72
JamesHH
OP
Junior Member
|
OP
Junior Member
Joined: Oct 2018
Posts: 72
|
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.)
|
|
|
|