title: Issue with calling ggplot using R_tryEval
body: <p>I am trying to build a system allowing to execute arbitrary R code from a C++ application.
  In orded to achieve that, the core of the method follows what is described here: <a href="http://www.hep.by/gnu/r-patched/r-exts/R-exts_121.html" rel="nofollow noreferrer">http://www.hep.by/gnu/r-patched/r-exts/R-exts_121.html</a></p>
  
  <p>This worked fine for any R code so far. However, I encoutered an issue with using ggplot. I try to send ggplot resulting graph to a png file. It works fine if done directly in a R console, but when I use my C++ interface, the resulting file is empty.</p>
  
  <p>More precisely, the R code is organized as follows: </p>
  
  <ul>
  <li>Load library ggplot2</li>
  <li>Define some dummy data to be plotted.</li>
  <li>Call png to create a graphics device.</li>
  <li>Call ggplot.</li>
  <li>Call dev.off() to close the graphics device.</li>
  </ul>
  
  <p>When reduced to a minimal case, here is my code: </p>
  
  <pre><code>#include &lt;QDebug&gt;
  
  #include &lt;Rembedded.h&gt;
  #include &lt;Rinternals.h&gt;
  #include &lt;R_ext/Parse.h&gt;
  
  int main(int argc, char *argv[])
  {
      const char *argvrf[] = {"RConsole"};
      int argcrf = sizeof(argvrf) / sizeof(argvrf[0]);
  
      Rf_initEmbeddedR(argcrf, (char**)argvrf);
  
      // Create some dummy data for a ggplot.
      // For the purpose of this minimal example, R code is just directly defined as a QString here.
  
      QString RScript = "library(\"ggplot2\") \n" ;
      RScript += "id&lt;-c(1,2,3,4,5) \n" ;
      RScript += "names&lt;-c(\"A\",\"B\",\"C\",\"D\",\"E\") \n" ;
      RScript += "notes&lt;-c(12,20,13,15,10) \n" ;
      RScript += "df1&lt;-data.frame(id,names,notes) \n" ;
  
      // Define a file as graphicsDevice (adapt adress to your own filesystem)
      RScript += "png(\"C:/Users/Evain/Desktop/tests/testggplot.png\", width=480, height=480, res=72) \n" ;
      // Drawing the ggplot.
      RScript += "ggplot(data=df1,aes(x=id,y=notes))+geom_line() \n" ;
      // Closing the graphic device.
      RScript += "dev.off() \n" ;
  
  
      ParseStatus status;
      SEXP cmdSexp, cmdexpr = R_NilValue;
      int i, errorOccurred, retVal=0;
  
      // Convert the command line to SEXP
      PROTECT(cmdSexp = Rf_allocVector(STRSXP, 1));
      SET_STRING_ELT(cmdSexp, 0, Rf_mkChar(RScript.toLocal8Bit().data()));
      cmdexpr = PROTECT(R_ParseVector(cmdSexp, -1, &amp;status, R_NilValue));
  
      switch (status){
      case PARSE_OK: {
          // Loop is needed here as EXPSEXP might be of length &gt; 1
          for(i = 0; ((i &lt; Rf_length(cmdexpr)) &amp;&amp; (retVal==0)); i++){
              R_tryEval(VECTOR_ELT(cmdexpr, i), R_GlobalEnv, &amp;errorOccurred);
              if (errorOccurred) {
                  // Interrupt process.
                  qDebug() &lt;&lt; "Error occured" ;
                  retVal = -1;
              }
          }
          break ;
      }
      default: {
          qDebug() &lt;&lt; "Incorrect R command" ;
          break;
      }
      }
      return 0;
  }
  </code></pre>
  
  <p>Communication with R works fine if it is not a ggplot. For example, if I replace the </p>
  
  <pre><code>RScript += "ggplot(data=df1,aes(x=id,y=notes))+geom_line() \n" ;
  </code></pre>
  
  <p>With: </p>
  
  <pre><code>RScript += "plot(3) \n";
  </code></pre>
  
  <p>Then it works fine, a png file is create with the required plot.
  With ggplot, this code runs without apparent issue. The qDebug() messages are not triggered. The png file is even created (meaning that the call to png() is executed correctly). But the file is empty.</p>
  
  <p>It is not just an issue with the combination of png() and ggplot() or with my dummy data, since if I just launch the following R script in a R console, I get the expected result (the file is created and contains the plot): </p>
  
  <pre><code>library("ggplot2")
  id&lt;-c(1,2,3,4,5)
  names&lt;-c("A","B","C","D","E")
  notes&lt;-c(12,20,13,15,10)    
  df1&lt;-data.frame(id,names,notes)
  
  png("C:/Users/Evain/Desktop/tests/testggplot.png", width=480, height=480, res=72)
  ggplot(data=df1,aes(x=id,y=notes))+geom_line()
  dev.off()
  </code></pre>
  
  <p>Note: I'm on Windows 10, using R3.4.3.
  It is worth noting that the png() method behaves slightly differently on Linux. For windows, the file is created when png() is called, even if it has to remain empty. For Linux, the file wouldn't be created if nothing is written in it.</p>
  
  <p>If the png file already exist, it also gets replaced by an empty one. That's what we should expect when calling png() alone. It's just that the ggplot doesn't get added to it.</p>
  
  <p>It feels like combining R_tryEval() with png() works fine. Combining png() with ggplot works fine, but combining R_tryEval() with png() and ggplot() doesn't. Any idea ?</p>