Gamestudio Links
Zorro Links
Newest Posts
lookback setting performance issue
by 7th_zorro. 04/16/24 03:08
folder management functions
by 7th_zorro. 04/15/24 10:10
zorro 64bit command line support
by 7th_zorro. 04/15/24 09:36
Zorro FIX plugin - Experimental
by flink. 04/14/24 07:48
Zorro FIX plugin - Experimental
by flink. 04/14/24 07:46
LPDIRECT3DCUBETEXTUR
E9

by Ayumi. 04/12/24 11:00
Sam Foster Sound | Experienced Game Composer for Hire
by titanicpiano14. 04/11/24 14:56
SGT_FW
by Aku_Aku. 04/10/24 16:09
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
2 registered members (7th_zorro, Quad), 373 guests, and 3 spiders.
Key: Admin, Global Mod, Mod
Newest Members
11honza11, ccorrea, sakolin, rajesh7827, juergen_wue
19045 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Possible to return character vector from R bridge? #476823
04/08/19 19:25
04/08/19 19:25
Joined: Oct 2018
Posts: 72
J
JamesHH Offline OP
Junior Member
JamesHH  Offline OP
Junior Member
J

Joined: Oct 2018
Posts: 72
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?

Re: Possible to return character vector from R bridge? [Re: JamesHH] #476827
04/09/19 07:25
04/09/19 07:25
Joined: Jul 2000
Posts: 27,978
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,978
Frankfurt
Are the symbols from a fixed set? You could then return an index in stead of the symbol name.

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
J
JamesHH Offline OP
Junior Member
JamesHH  Offline OP
Junior Member
J

Joined: Oct 2018
Posts: 72
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.

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
J
JamesHH Offline OP
Junior Member
JamesHH  Offline OP
Junior Member
J

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: JamesHH] #476919
04/23/19 05:53
04/23/19 05:53
Joined: Jul 2000
Posts: 27,978
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,978
Frankfurt
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.

Re: Possible to return character vector from R bridge? [Re: jcl] #476927
04/23/19 16:39
04/23/19 16:39
Joined: Feb 2017
Posts: 1,724
Chicago
AndrewAMD Offline
Serious User
AndrewAMD  Offline
Serious User

Joined: Feb 2017
Posts: 1,724
Chicago
One alternative might be to use rcpp to send a string to a Windows named pipe, and then retrieve it zorro-side.

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
J
JamesHH Offline OP
Junior Member
JamesHH  Offline OP
Junior Member
J

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
J
JamesHH Offline OP
Junior Member
JamesHH  Offline OP
Junior Member
J

Joined: Oct 2018
Posts: 72
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.)


Moderated by  Petra 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1