<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/rcppnloptexample-0-0-1-use-nlopt-from-c-c/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://dirk.eddelbuettel.com/blog/2018/10/13#rcppnloptexample_0.0.1"> Thinking inside the box </a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>A new package of ours, <a rel="nofollow" href="http://dirk.eddelbuettel.com/code/rcpp.nloptexample.html" target="_blank">RcppNLoptExample</a>, arrived on <a rel="nofollow" href="https://cran.r-project.org/" target="_blank">CRAN</a> yesterday after a somewhat longer-than-usual wait for new packages as <a rel="nofollow" href="https://cran.r-project.org/" target="_blank">CRAN</a> seems really busy these days. As always, a big and very grateful <em>Thank You!</em> for all they do to keep this community humming.</p>
<h4>So what does it do ?</h4>
<p><a rel="nofollow" href="https://nlopt.readthedocs.io/en/latest/" target="_blank">NLopt</a> is a very comprehensive library for nonlinear optimization. The <a rel="nofollow" href="https://github.com/jyypma/nloptr" target="_blank">nloptr</a> package by <a rel="nofollow" href="https://github.com/jyypma" target="_blank">Jelmer Ypma</a> has long been providing an excellent R interface.</p>
<p>Starting with its 1.2.0 release, the <a rel="nofollow" href="https://github.com/jyypma/nloptr" target="_blank">nloptr</a> package now exports several C symbols in a way that makes it accessible to other R packages <em>without linking</em> easing the installation on all operating systems.</p>
<p>The new package <a rel="nofollow" href="http://dirk.eddelbuettel.com/code/rcpp.nloptexample.html" target="_blank">RcppNLoptExample</a> illustrates this facility with an example drawn from the <a rel="nofollow" href="https://nlopt.readthedocs.io/en/latest/NLopt_Tutorial/" target="_blank">NLopt tutorial</a>. See the (currently single) file <a rel="nofollow" href="https://github.com/eddelbuettel/rcppnloptexample/blob/master/src/nlopt.cpp" target="_blank">src/nlopt.cpp</a>.</p>
<h4>How / Why ?</h4>
<p>R uses C interfaces. These C interfaces can be exported <em>between packages</em>. So when the usual <code>library(nloptr)</code> (or an import via <code>NAMESPACE</code>) happens, we now <em>also</em> get a number of C functions registered.</p>
<p>And those are enough to run optimization from C++ as we simply <em>rely on the C interface provided</em>. Look careful at the example code: the objective function and the constraint functions are C functions, and the body of our example invokes C functions from <a rel="nofollow" href="https://nlopt.readthedocs.io/en/latest/" target="_blank">NLopt</a>. <em>This just works</em>. For either C code, or C++ (where we rely on <a rel="nofollow" href="http://www.rcpp.org/" target="_blank">Rcpp</a> to marshal data back and forth with ease).</p>
<p>On the other hand, if we tried to use the <a rel="nofollow" href="https://nlopt.readthedocs.io/en/latest/" target="_blank">NLopt</a> C++ interface <em>which brings with it someinterface code</em> we would require linking to that code (which R cannot easily export across packages using its C interface). So C it is.</p>
<h4>Status</h4>
<p>The package is pretty basic but fully workable. Some more examples should get added, and a helper class or two for state would be nice. The (very short) NEWS entry follows:</p>
<blockquote>
<h4>Changes in version 0.0.1 (2018-10-01)</h4>
<ul>
<li>Initial basic package version with one example from NLopt tutorial</li>
</ul>
</blockquote>
<p>Code, issue tickets etc are at the <a rel="nofollow" href="https://github.com/eddelbuettel/rcppnloptexample" target="_blank">GitHub repository</a>.</p>
<p>
This post by <a rel="nofollow" href="http://dirk.eddelbuettel.com/" target="_blank">Dirk Eddelbuettel</a> originated on his <a rel="nofollow" href="http://dirk.eddelbuettel.com/blog/" target="_blank">Thinking inside the box</a> blog. Please report excessive re-aggregation in third-party for-profit settings.</p>
<p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://dirk.eddelbuettel.com/blog/2018/10/13#rcppnloptexample_0.0.1"> Thinking inside the box </a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/nnpKyxwAe70" height="1"></a></div>