Dear list, I am trying to embed R into a C++ program. After some tinkering, reading the documentation and browsing the source code I have this more or less working. A very very condensed and very simplified version of the code is included below. The program can create plots. However, after the plot is initially drawn it is no longer updated. When scaling or updating the plot the window becomes blank, and the window also doesn't want to close. I suspect that my R_ReadConsole routine is the problem. This routine waits for user input and returns this (which I thought it should). It seems that this causes the event loop that updates the windows to also be on hold. I did have a look at 'Rstd_ReadConsole' in 'src/unix/sys-std.c', but I can't figure out what exactly happens in this routine. How do I ensure that the windows keep being updated? Presently I am working under Linux. However, I also want to be able to run my code under windows, so I hope there is a cross-platform solution. Thanks in advance. Regards, Jan van der Laan ===== The example code ====#include <iostream> #include <iomanip> #include <string> static void R_WriteConsoleEx (const char *buf, int buflen, int otype) { std::string output(buf, buflen); std::cout << output; } static void R_WriteConsole (const char *buf, int buflen) { R_WriteConsoleEx(buf, buflen, 0); } static int R_ReadConsole (const char *prompt, unsigned char *buf, int buflen, int hist) { std::cout << prompt; std::string input; std::cin >> input; for (unsigned int i = 0; i < input.length(); ++i) { buf[i] = input[i]; buf[i+1] = '\n'; buf[i+2] = '\0'; if ((int)i >= buflen-3) break; } return input.length(); } extern "C" { #define R_INTERFACE_PTRS #include <Rinterface.h> int Rf_initialize_R(int ac, char **av); /* in ../unix/system.c */ extern int R_running_as_main_program; /* in ../unix/system.c */ } int main(int ac, char **av) { R_running_as_main_program = 1; Rf_initialize_R(ac, av); ptr_R_WriteConsoleEx = &R_WriteConsoleEx; ptr_R_WriteConsole = &R_WriteConsole; ptr_R_ReadConsole = &R_ReadConsole; R_Outputfile = NULL; R_Consolefile = NULL; Rf_mainloop(); /* does not return */ return 0; }
Thomas Friedrichsmeier
2010-Jul-21 19:45 UTC
[Rd] Plot window does not update in embedded code
Hi, On Wednesday 21 July 2010, Jan van der Laan wrote:> How do I ensure that the windows keep being updated?in RKWard we run the following periodically during idle phases: // this basically copied from R's unix/sys-std.c (Rstd_ReadConsole) #ifndef Q_WS_WIN for (;;) { fd_set *what; what = R_checkActivityEx(R_wait_usec > 0 ? R_wait_usec : 50, 1, Rf_onintr); R_runHandlers(R_InputHandlers, what); if (what == NULL) break; } /* This seems to be needed to make Rcmdr react to events. Has this always been the case? It was commented out for a long time, without anybody noticing. */ R_PolledEvents (); #else R_ProcessEvents(); #endif Regards Thomas -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 198 bytes Desc: This is a digitally signed message part. URL: <https://stat.ethz.ch/pipermail/r-devel/attachments/20100721/c1088344/attachment.bin>