<div dir="ltr"><div style="font-family:arial,sans-serif;font-size:13px">Are you saying that DataFrame::create doesn't work properly with std::vector? I think I have seen examples and test units doing exactly that.</div>
<div style="font-family:arial,sans-serif;font-size:13px"><br></div><span style="font-family:arial,sans-serif;font-size:13px">In my example, I'd like the c++ layer doing the actual work to stay pure c++, so that this code could be used without Rcpp (in a c++ program for instance). Thus, the results from this layer are std::vector. Other than that, I just want to float it up to R, where the final result (in R) makes most sense to be a data frame. It seemed most efficient to build the data frame in c++. And since I saw that a data frame can be created from std::vectors - I went for the direct approach.</span><div style="font-family:arial,sans-serif;font-size:13px">
<br></div><div style="font-family:arial,sans-serif;font-size:13px">Under these circumstances, can you think of a better way building the data frame without creating the extra R objects on the fly? In fact, the intermediate vectors are not a big deal - the gain in speed of using c++ dwarfs the conversion, but still, if there is an easy optimization ...<br>
<div><br></div><div>Of course, I can return anything and convert it to a data.frame in R. Just trying to understand what is the recommended way doing that. Any other suggestions/ideas are welcome.<div><br></div><div>Thanks in advance,</div>
<div>Ivan</div></div></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Wed, May 15, 2013 at 11:30 PM, Dirk Eddelbuettel <span dir="ltr"><<a href="mailto:edd@debian.org" target="_blank">edd@debian.org</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im"><br>
On 15 May 2013 at 20:23, Ivan Popivanov wrote:<br>
| Two versions of the code, see at the end. The first converts my std::vectors to<br>
| Rcpp equivalents. The second builds the data frame from the std::vectors.<br>
<br>
</div>A date.frame is an R list with some special sauce, and it is probably easier<br>
to construct this from R types -- reduces temp copies.  We can after not<br>
(directly) send a std::vector back to R, though the implicit use of wrap()<br>
comes close.<br>
<div class="im"><br>
| Calling the function containing this code numerous times (hundreds if not<br>
| thousands) returning the std::vectors directly seems to cause random crashes or<br>
| hangs somewhere after the return statement. The other version seems to work<br>
| properly.<br>
<br>
</div>The devil is in the detail.<br>
<br>
There was some discussion here on the list over the years, and we have one<br>
contributed 'faster' data.frame creator here:<br>
<br>
   <a href="http://gallery.rcpp.org/articles/faster-data-frame-creation/" target="_blank">http://gallery.rcpp.org/articles/faster-data-frame-creation/</a><br>
<br>
Dirk<br>
<div><div class="h5"><br>
| Thanks,<br>
|<br>
|   return Rcpp::DataFrame::create(<br>
|                Rcpp::Named("Entry") = Rcpp::IntegerVector(ibeg.begin(),<br>
| ibeg.end()),<br>
|                Rcpp::Named("Exit") = Rcpp::IntegerVector(iendOut.begin<br>
| (),iendOut.end()),<br>
|                Rcpp::Named("Position") = Rcpp::IntegerVector<br>
| (position.begin(), position.end()),<br>
|                Rcpp::Named("StopLoss") = Rcpp::NumericVector<br>
| (stopLoss.begin(), stopLoss.end()),<br>
|                Rcpp::Named("StopTrailing") = Rcpp::NumericVector<br>
| (stopTrailing.begin(), stopTrailing.end()),<br>
|                Rcpp::Named("ProfitTarget") = Rcpp::NumericVector<br>
| (profitTarget.begin(), profitTarget.end()),<br>
|                Rcpp::Named("ExitPrice") = Rcpp::NumericVector<br>
| (exitPrice.begin(), exitPrice.end()),<br>
|                Rcpp::Named("Gain") = Rcpp::NumericVector(gain.begin(),<br>
| gain.end()),<br>
|                Rcpp::Named("MAE") = Rcpp::NumericVector(mae.begin(),<br>
| mae.end()),<br>
|                Rcpp::Named("MFE") = Rcpp::NumericVector(mfe.begin(),<br>
| mfe.end()),<br>
|                Rcpp::Named("Reason") = Rcpp::IntegerVector(reason.begin<br>
| (), reason.end()));<br>
|    /*<br>
|    return Rcpp::DataFrame::create(<br>
|                Rcpp::Named("Entry") = ibeg,<br>
|                Rcpp::Named("Exit") = iendOut,<br>
|                Rcpp::Named("Position") = position,<br>
|                Rcpp::Named("StopLoss") = stopLoss,<br>
|                Rcpp::Named("StopTrailing") = stopTrailing,<br>
|                Rcpp::Named("ProfitTarget") = profitTarget,<br>
|                Rcpp::Named("ExitPrice") = exitPrice,<br>
|                Rcpp::Named("Gain") = gain,<br>
|                Rcpp::Named("MAE") = mae,<br>
|                Rcpp::Named("MFE") = mfe,<br>
|                Rcpp::Named("Reason") = reason);<br>
|                */<br>
|<br>
</div></div>| ----------------------------------------------------------------------<br>
| _______________________________________________<br>
| Rcpp-devel mailing list<br>
| <a href="mailto:Rcpp-devel@lists.r-forge.r-project.org">Rcpp-devel@lists.r-forge.r-project.org</a><br>
| <a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a><br>
<span class="HOEnZb"><font color="#888888">--<br>
Dirk Eddelbuettel | <a href="mailto:edd@debian.org">edd@debian.org</a> | <a href="http://dirk.eddelbuettel.com" target="_blank">http://dirk.eddelbuettel.com</a><br>
</font></span></blockquote></div><br></div>