<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>C&#43;&#43; on R Views</title>
    <link>https://rviews.rstudio.com/tags/c&#43;&#43;/</link>
    <description>Recent content in C&#43;&#43; on R Views</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en-us</language>
    <lastBuildDate>Mon, 28 Sep 2020 00:00:00 +0000</lastBuildDate>
    <atom:link href="https://rviews.rstudio.com/tags/c&#43;&#43;/" rel="self" type="application/rss+xml" />
    
    
    
    
    <item>
      <title>R Package Integration with Modern Reusable C&#43;&#43; Code Using Rcpp - Part 6</title>
      <link>https://rviews.rstudio.com/2020/09/28/r-package-integration-with-modern-reusable-c-code-using-rcpp-part-6/</link>
      <pubDate>Mon, 28 Sep 2020 00:00:00 +0000</pubDate>
      
      <guid>https://rviews.rstudio.com/2020/09/28/r-package-integration-with-modern-reusable-c-code-using-rcpp-part-6/</guid>
      <description>
        


&lt;p&gt;In this final post of the six part series on R package integration with modern reusable C++ Code using Rcpp, we will look at providing documentation for an R package. To review, we previously covered the following topics:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;https://rviews.rstudio.com/2020/07/08/r-package-integration-with-modern-reusable-c-code-using-rcpp/&#34;&gt;Installation and configuration&lt;/a&gt; of an &lt;code&gt;Rcpp&lt;/code&gt; package project in RStudio&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://rviews.rstudio.com/2020/07/14/r-package-integration-with-modern-reusable-c-code-using-rcpp-part-2/&#34;&gt;Design considerations&lt;/a&gt; for integrating R with reusable C++ code&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://rviews.rstudio.com/2020/07/31/r-package-integration-with-modern-reusable-c-code-using-rcpp-part-3/&#34;&gt;Rcpp interface code examples&lt;/a&gt; that are exported to R&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://rviews.rstudio.com/2020/08/18/r-package-integration-with-modern-reusable-c-code-using-rcpp-part-4/&#34;&gt;Creating an Rcpp Package Project&lt;/a&gt; in RStudio, and building and distributing an R package&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://rviews.rstudio.com/2020/08/24/r-package-integration-with-modern-reusable-c-code-using-rcpp-part-5/&#34;&gt;An introductory example&lt;/a&gt; of integrating independent and standard C++ code in an R package, with sample code provided &lt;a href=&#34;https://github.com/QuantDevHacks/RcppBlogCode/tree/master/CodePart05/src&#34;&gt;on GitHub&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Let’s now look at how to provide html documentation that conforms to the familiar form one finds in R packages.&lt;/p&gt;
&lt;div id=&#34;documentation&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Documentation&lt;/h2&gt;
&lt;p&gt;In the discussion that follows, we will look at a quick and relatively easy way to provide documentation for each of the C++ functions that is exported as an R function in an &lt;code&gt;Rcpp&lt;/code&gt;-based package. It should be noted that there are alternatives that are often preferred for packages that are submitted to CRAN for publication; for example, using the &lt;a href=&#34;https://cran.r-project.org/web/packages/roxygen2/vignettes/roxygen2.html&#34;&gt;&lt;code&gt;ROxygen2&lt;/code&gt; package&lt;/a&gt; for generating the documentation from inline statements in the interface code. This, however, involves sophistication and complexity beyond the scope of this “getting started” series.&lt;/p&gt;
&lt;p&gt;The approach we will examine here involves filling a file template for each exported function, as well as a file that provides information common to the entire package. These file templates carry an &lt;code&gt;Rd&lt;/code&gt; extension, as we shall see. The corresponding html files are then generated when you build your package. The good news is that &lt;code&gt;RStudio&lt;/code&gt; makes this a relatively painless process.&lt;/p&gt;
&lt;p&gt;If you create an &lt;code&gt;Rcpp&lt;/code&gt; package project as discussed in the previous posts in this series, you will find the following two files provided by default under the &lt;code&gt;man&lt;/code&gt; subdirectory:&lt;/p&gt;
&lt;div style=&#34;page-break-after: always;&#34;&gt;&lt;/div&gt;
&lt;div class=&#34;figure&#34;&gt;
&lt;img src=&#34;man_subdirectory.png&#34; alt=&#34;&#34; /&gt;
&lt;p class=&#34;caption&#34;&gt;Default documentation files&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;These files are provided by default. You ultimately won’t need the &lt;em&gt;hello world&lt;/em&gt; help file, but the &lt;code&gt;RcppProject-package.Rd&lt;/code&gt; will contain common information for the entire package; this latter file will be introduced shortly.&lt;/p&gt;
&lt;div id=&#34;documentation-for-exported-functions&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Documentation for Exported Functions&lt;/h3&gt;
&lt;p&gt;Let’s first cover how to create a help file for an exported &lt;code&gt;Rcpp&lt;/code&gt; interface function. To show this, let’s go back to the sample code provided on GitHub for &lt;a href=&#34;https://github.com/QuantDevHacks/RcppBlogCode&#34;&gt;Part 5&lt;/a&gt;. Suppose you wanted to provide documentation for the &lt;code&gt;rAdd(.)&lt;/code&gt; function that is exported from the &lt;code&gt;CppInterface.cpp&lt;/code&gt; file. How would you do this?&lt;/p&gt;
&lt;p&gt;With RStudio, the solution is quite easy. First, you would select &lt;code&gt;New File/Documentation...&lt;/code&gt; from the &lt;code&gt;File&lt;/code&gt; menu at the top:&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;RDocFileSelect.png&#34; alt=&#34;Select a new documentation file&#34; /&gt;&lt;br /&gt;

You will then see an input panel as follows. Under &lt;em&gt;Topic name&lt;/em&gt;, type in the function name &lt;code&gt;rAdd&lt;/code&gt;, and be sure the selection under &lt;em&gt;Rd template&lt;/em&gt; is set to &lt;code&gt;function&lt;/code&gt; (the default):&lt;/p&gt;
&lt;div class=&#34;figure&#34;&gt;
&lt;img src=&#34;NewRDocumentationFile.png&#34; alt=&#34;&#34; /&gt;
&lt;p class=&#34;caption&#34;&gt;New R documentation file selection&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;After clicking &lt;code&gt;OK&lt;/code&gt;, you will see the &lt;code&gt;rAdd.Rd&lt;/code&gt; file appear in the &lt;code&gt;man&lt;/code&gt; subdirectory in the RStudio files pane:&lt;/p&gt;
&lt;div class=&#34;figure&#34;&gt;
&lt;img src=&#34;NewDocumentationFileInMan.png&#34; alt=&#34;&#34; /&gt;
&lt;p class=&#34;caption&#34;&gt;New documentation file in &lt;code&gt;man&lt;/code&gt; folder&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Next, double click on this file to open it in RStudio. Note that RStudio is smart enough to glean the proper number of arguments from the function and place this information in the file:&lt;/p&gt;
&lt;div class=&#34;figure&#34;&gt;
&lt;img src=&#34;rAddRdFile_01.png&#34; alt=&#34;&#34; /&gt;
&lt;p class=&#34;caption&#34;&gt;Top of the created &lt;code&gt;rAdd.Rd&lt;/code&gt; file&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Work down the file section by section and replace the comments in green (beginning with a &lt;code&gt;%&lt;/code&gt; character) with the relevant information as described to fill in the necessary information; also, the commented line: &lt;code&gt;%- Also NEED an &#39;\alias&#39; for EACH other topic documented here.&lt;/code&gt; can just be removed. The contents for each section should be placed inside the curly brackets &lt;code&gt;{ }&lt;/code&gt; following each of the following items, as will be demonstrated in the example that follows these descriptions.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;name&lt;/code&gt; and &lt;code&gt;alias&lt;/code&gt;: Just leave these as they are, with the function name.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;title&lt;/code&gt;: Provide brief description of what the function does .&lt;/p&gt;
&lt;p&gt;&lt;code&gt;description&lt;/code&gt;: Provide a more detailed description of what the function does.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;usage&lt;/code&gt;: The easiest thing to do here is to leave it as is.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;arguments&lt;/code&gt;: You will notice the description of each argument &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt; is nested. Just enter the type of each variable, and a short description of each if desired.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;details&lt;/code&gt;: This can be removed if no further details are required; otherwise, place additional details about the functionality here.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;value&lt;/code&gt;: Indicate the return type here.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;references&lt;/code&gt;: Place references you wish to cite here; otherwise, this can be removed if not needed.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;author&lt;/code&gt;: Provide the names of authors/contributors for this package function.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;note&lt;/code&gt; and &lt;code&gt;seealso&lt;/code&gt;: Enter any additional information about the function; may be removed if not needed.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;examples&lt;/code&gt;: Place working example(s) of your function here. This should be completely self-contained, including any input data (if needed).&lt;/p&gt;
&lt;p&gt;After the examples section, remove the trailing comments at the bottom of the file.&lt;/p&gt;
&lt;p&gt;A complete sample file could then be as follows. You could also use this as a blueprint for your own function documentation if you wish.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;\name{rAdd}
\alias{rAdd}
\title{Add two numbers}
\description{Adds two real numbers}
\usage{
rAdd(x, y)
}
\arguments{
  \item{x}{A real number}
  \item{y}{A real number}
}
\details{This is a trivial example}
\value{numeric}
\references{Courant and Hilbert, Methods of Mathematical Physics, Volumes 1 &amp;amp; 2}
\author{Fred Sanford}
\note{Nothing to say}
\seealso{https://rviews.rstudio.com/}
\examples{
x &amp;lt;- 586.3
y &amp;lt;- 922.6
rAdd(x, y)
}&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To generate the html help file, rebuild your project. When complete, and with the package reloaded into your current R session, look up &lt;code&gt;rAdd&lt;/code&gt; in the usual help section in RStudio. The result should look like this:&lt;/p&gt;
&lt;div class=&#34;figure&#34;&gt;
&lt;img src=&#34;rAddGenDoc.png&#34; width=&#34;400&#34; alt=&#34;&#34; /&gt;
&lt;p class=&#34;caption&#34;&gt;The generated help file for the &lt;code&gt;rAdd(.)&lt;/code&gt; function&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;As a matter of best practice, you will want to follow the same instructions per the above for all of the remaining exported interface functions in your package. The remaining &lt;code&gt;.Rd&lt;/code&gt; files have been uploaded to the &lt;code&gt;man&lt;/code&gt; directory in the &lt;a href=&#34;https://github.com/QuantDevHacks/RcppBlogCode/tree/master/CodePart06/man&#34;&gt;GitHub repository for this post&lt;/a&gt;. These help files accompany the source files that were &lt;a href=&#34;https://github.com/QuantDevHacks/RcppBlogCode/tree/master/CodePart05/src&#34;&gt;provided for Part 5&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;documentation-of-common-package-information&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Documentation of Common Package Information&lt;/h3&gt;
&lt;p&gt;A default file is also provided with an RStudio &lt;code&gt;Rcpp&lt;/code&gt; project that is to contain an overall help file for the entire package; it is of the form &lt;code&gt;PackageName-package.Rd&lt;/code&gt;. However, modifying this file for your particular package can sometimes be a source of build errors and package check errors, so I recommend just keeping it simple, at least until you become more accustomed to writing your own R packages. In particular, you will probably save yourself some trouble by removing the code examples section from this file and relegating them to the individual function help files, at least to get started.&lt;/p&gt;
&lt;p&gt;As an example, the &lt;code&gt;RcppBlogCode-package.Rd&lt;/code&gt; file is also included in the GitHub folder for this post, as noted above. As you can see, this is short and sweet, but if applied to a realistic situation, it should be reasonably informative. The generated result is shown here:&lt;/p&gt;
&lt;div class=&#34;figure&#34;&gt;
&lt;img src=&#34;CommonPackageHelpFileHtml.png&#34; alt=&#34;&#34; /&gt;
&lt;p class=&#34;caption&#34;&gt;Generated help file containing common package information&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;the-check-package-utility&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;The &lt;code&gt;Check Package&lt;/code&gt; Utility&lt;/h2&gt;
&lt;p&gt;After building and testing your package as discussed earlier on, there is one more task that one should run before deploying an R package, and this is the &lt;em&gt;Check Package&lt;/em&gt; procedure, which can be found under the &lt;code&gt;Build&lt;/code&gt; menu in RStudio:&lt;/p&gt;
&lt;div class=&#34;figure&#34;&gt;
&lt;img src=&#34;CheckPackageMenuChoice.png&#34; alt=&#34;&#34; /&gt;
&lt;p class=&#34;caption&#34;&gt;Starting the Check Package process&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;As described in the &lt;a href=&#34;https://www.rdocumentation.org/packages/devtools/versions/1.3/topics/check&#34;&gt;R documentation (devtools)&lt;/a&gt;, &lt;em&gt;Check Package&lt;/em&gt; “automatically builds and checks a source package, using all know best practices”. While this is more of a critical step for submitting a package to CRAN, even for internal deployment it’s a good idea to run it to be sure that your documentation is complete, and the examples in your documentation run properly. If you are missing an &lt;code&gt;.Rd&lt;/code&gt; file for a function exported to your package, or if an example in your documentation does not run, the check procedure will flag an error.&lt;/p&gt;
&lt;p&gt;The heavy details related to checking a package for CRAN submission are located &lt;a href=&#34;https://cran.r-project.org/doc/manuals/r-release/R-exts.html#Checking-and-building-packages&#34;&gt;on the CRAN website&lt;/a&gt;, and of course many other sources are available on the internet. These are beyond the scope of this series, but if at some point you are contemplating a CRAN submission, it will be necessary to work through the details.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;summary&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Summary&lt;/h2&gt;
&lt;p&gt;This concludes our series on integrating independent, standard, and reusable C++ code in an R package using RStudio and the &lt;code&gt;Rcpp&lt;/code&gt; package. Hopefully, this will help you avoid some of the frustrations one can encounter from the proverbial fire hose of ubiquitous information that often obscures the essentials at the outset. Happy packaging!&lt;/p&gt;
&lt;/div&gt;

        &lt;script&gt;window.location.href=&#39;https://rviews.rstudio.com/2020/09/28/r-package-integration-with-modern-reusable-c-code-using-rcpp-part-6/&#39;;&lt;/script&gt;
      </description>
    </item>
    
    <item>
      <title>R Package Integration with Modern Reusable C&#43;&#43; Code Using Rcpp - Part 5</title>
      <link>https://rviews.rstudio.com/2020/08/24/r-package-integration-with-modern-reusable-c-code-using-rcpp-part-5/</link>
      <pubDate>Mon, 24 Aug 2020 00:00:00 +0000</pubDate>
      
      <guid>https://rviews.rstudio.com/2020/08/24/r-package-integration-with-modern-reusable-c-code-using-rcpp-part-5/</guid>
      <description>
        


&lt;p&gt;&lt;em&gt;Daniel Hanson is a full-time lecturer in the Computational Finance &amp;amp; Risk Management program within the Department of Applied Mathematics at the University of Washington.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;In the &lt;a href=&#34;https://rviews.rstudio.com/...part-4/&#34;&gt;previous post&lt;/a&gt;, we went through the build processes for a simple package containing a single C++ &lt;code&gt;.cpp&lt;/code&gt; file, using the &lt;code&gt;rcpp_hello_world.cpp&lt;/code&gt; example that is included by default when one creates a new R package using &lt;code&gt;Rcpp&lt;/code&gt; in RStudio.&lt;/p&gt;
&lt;p&gt;What we’ll do now is examine a more realistic case which involves multiple reusable C++ files as well as the C++ interface files that export functions to R. For this example, you should download the C++ files from &lt;a href=&#34;https://github.com/QuantDevHacks/RcppBlogCode/tree/master/CodePart05/src&#34;&gt;this GitHub repository&lt;/a&gt;. In addition, you should download a set of test functions in R, &lt;a href=&#34;https://github.com/QuantDevHacks/RcppBlogCode/tree/master/TestsPart05&#34;&gt;here (RStudioBlogTests.R)&lt;/a&gt;. Please note that this file is &lt;em&gt;not&lt;/em&gt; part of the package code; we will discuss this shortly.&lt;/p&gt;
&lt;div id=&#34;building-an-rcpp-package-with-reusable-c&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Building an &lt;code&gt;Rcpp&lt;/code&gt; Package with Reusable C++&lt;/h2&gt;
&lt;p&gt;The code you have downloaded from the first link is the same as that referenced in &lt;a href=&#34;https://rviews.rstudio.com/2020/07/31/r-package-integration-with-modern-reusable-c-code-using-rcpp-part-3/&#34;&gt;Part 3 of this series&lt;/a&gt;. You will now create an &lt;code&gt;Rcpp&lt;/code&gt; package project in RStudio and import this code.&lt;/p&gt;
&lt;div id=&#34;create-an-rstudio-project&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Create an RStudio Project&lt;/h3&gt;
&lt;p&gt;This procedure is the same as what we covered in the previous post. For convenience later, I suggest you name your project &lt;code&gt;RcppBlogCode&lt;/code&gt;. Make sure to modify the &lt;code&gt;NAMESPACE&lt;/code&gt; file as we discussed before. Also, while not mandatory, you may find it simpler to delete the &lt;code&gt;rcpp_hello_world.cpp&lt;/code&gt; file; we will not use it in this example.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;import-the-c-code-into-the-project&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Import the C++ Code into the Project&lt;/h3&gt;
&lt;p&gt;Next, copy or move the code you downloaded from the &lt;code&gt;/src&lt;/code&gt; subdirectory on GitHub into the &lt;code&gt;/src&lt;/code&gt; subdirectory of your &lt;code&gt;RStudio&lt;/code&gt; project. When this is done, you should see the files present in this location in the files pane in RStudio:&lt;br /&gt;
&lt;img src=&#34;FilesPaneSrc.png&#34; width=&#34;500&#34; alt=&#34;C++ Source Code Files&#34; /&gt;&lt;/p&gt;
&lt;p&gt;Note that this source code contains:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Declaration (header files) for the reusable C++ code&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;NonmemberCppFcns.h&lt;/code&gt;: Declarations of nonmember functions&lt;/li&gt;
&lt;li&gt;&lt;code&gt;ConcreteShapes.h&lt;/code&gt;: Declarations of the &lt;code&gt;Square&lt;/code&gt; and &lt;code&gt;Circle&lt;/code&gt; classes&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Files containing implementations of functions and classes in the reusable C++ code&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;NonmemberCppFcns.cpp&lt;/code&gt;: Nonmember function implementations&lt;/li&gt;
&lt;li&gt;&lt;code&gt;ConcreteShapes.cpp&lt;/code&gt;: &lt;code&gt;Square&lt;/code&gt; and &lt;code&gt;Circle&lt;/code&gt; class implementations&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;C++ files that call reusable code and export functions to R (&lt;code&gt;.cpp&lt;/code&gt; files only); each exported function is indicated by the &lt;code&gt;//&lt;/code&gt; &lt;code&gt;[[Rcpp::export]]&lt;/code&gt; tag&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;CppInterface.cpp&lt;/code&gt;: Calls nonmember functions in the code base&lt;/li&gt;
&lt;li&gt;&lt;code&gt;CppInterface2.cpp&lt;/code&gt;: Creates and uses instances of classes in the code base&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div id=&#34;build-and-run-functions-in-the-r-package&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Build and Run Functions in the R Package&lt;/h3&gt;
&lt;p&gt;Next, let’s build this package, just as we did for our simpler example in the previous post. Select &lt;code&gt;Clean and Rebuild&lt;/code&gt; from the &lt;code&gt;Build&lt;/code&gt; menu at the top of the RStudio IDE. When complete, R will restart, and the &lt;code&gt;RcppBlogCode&lt;/code&gt; package will be loaded into your R session. You can now call functions from this package in the console; e.g. ,&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Calculate the product of the LCM and GCD of 10 and 20: &lt;code&gt;rProdLcmGcd(10, 20)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Calculate the area of a square with length 4: &lt;code&gt;squareArea(4)&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div class=&#34;figure&#34;&gt;
&lt;img src=&#34;RunInConsole.png&#34; width=&#34;500&#34; alt=&#34;&#34; /&gt;
&lt;p class=&#34;caption&#34;&gt;Try package functions in console&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Congratulations again! You have now successfully imported resuable C++ code, independent of R and &lt;code&gt;Rcpp&lt;/code&gt;, and used it via an interface as an R function in your generated package. However, this of course is not a realistic scenario. What we really want is to use this package in a regular and independent R session.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;use-the-package-independently-of-the-rcpp-project&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Use the Package Independently of the &lt;code&gt;Rcpp&lt;/code&gt; Project&lt;/h2&gt;
&lt;p&gt;So far, you have built an R package containing C++ code, and you have called a couple of functions from the console, but that of course is not what a real R user does. What you’ll want to do is open a new (and empty) RStudio instance. As the &lt;code&gt;RcppBlogCode&lt;/code&gt; package is installed on your machine, all you need to do is load it as you would any other R package:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;library(RcppBlogCode)&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Next, open up the &lt;code&gt;RStudioBlogTests.R&lt;/code&gt; test file that you downloaded at the outset. Again, make sure this file is located outside of the &lt;code&gt;Rcpp&lt;/code&gt; package directory structure. You can now use the package functions that call the C++ nonmember functions internally in the package:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;rAdd(531, 9226)    

(x &amp;lt;- c(5:1))
rSortVec(x)

rProdLcmGcd(10, 20) 
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;You can also run the R functions that create instances of the &lt;code&gt;Square&lt;/code&gt; and &lt;code&gt;Circle&lt;/code&gt; classes in the C++ code:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;squareArea(4)
circleArea(1)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The actual mathematics here are obviously nothing exciting, but you should now see that you have a blueprint that takes in independent and reusable C++ source code, wraps its functionality in C++ interface functions that get exported by &lt;code&gt;Rcpp&lt;/code&gt;, and allows you to use these exported functions just as you would any other R package functions.&lt;/p&gt;
&lt;p&gt;Perhaps more interesting is to see how you can take the results of these package functions and use them in other R functions and packages. For this next example, you will need to install and load the &lt;code&gt;plotly&lt;/code&gt; package. We will define vectors of square sides and circle radii, and compute the corresponding areas using our package functions. This is again nothing earth-shattering, but we can then use them in &lt;code&gt;plotly&lt;/code&gt; to generate some reasonably professional quality graphs:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;library(plotly)

squareSides &amp;lt;- c(1:10)
circleRadii &amp;lt;- c(1:10)

squareAreas &amp;lt;- sapply(squareSides, squareArea)
circleAreas &amp;lt;- sapply(circleRadii, circleArea)

### Use plotly to visualize some results:
dfSq &amp;lt;- matrix(data=c(squareSides, squareAreas), nrow=length(squareSides), ncol=2)
dfSq &amp;lt;- as.data.frame(dfSq)
colnames(dfSq) = c(&amp;quot;Side&amp;quot;, &amp;quot;Area&amp;quot;)
rownames(dfSq) = NULL
plot_ly(dfSq, x = ~Side, y = ~ Area, type = &amp;quot;bar&amp;quot;) %&amp;gt;% 
  layout(title=&amp;quot;Areas of Squares&amp;quot;)


dfCirc &amp;lt;- matrix(data=c(circleRadii, circleAreas), nrow=length(squareSides), ncol=2)
dfCirc &amp;lt;- as.data.frame(dfCirc)
colnames(dfCirc) = c(&amp;quot;Radius&amp;quot;, &amp;quot;Area&amp;quot;)
rownames(dfCirc) = NULL
plot_ly(dfCirc, x = ~Radius, y = ~ Area, type = &amp;#39;scatter&amp;#39;, 
        mode = &amp;#39;lines&amp;#39;, color = &amp;#39;red&amp;#39;) %&amp;gt;%
  layout(title=&amp;quot;Areas of Circles&amp;quot;)
&lt;/code&gt;&lt;/pre&gt;
&lt;div class=&#34;figure&#34;&gt;
&lt;img src=&#34;Areas.png&#34; width=&#34;500&#34; alt=&#34;&#34; /&gt;
&lt;p class=&#34;caption&#34;&gt;Using results from package in Plotly graphs&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Remark 1:&lt;/strong&gt; If you wish to go back and modify the package code, you will need to close your R session that is using the package; otherwise, the code will not build.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Remark 2:&lt;/strong&gt; As discussed last time, you can also export the package to a binary and deploy it on another machine. In general, this allows you to design and build your own &lt;code&gt;Rcpp&lt;/code&gt; R package, and deploy it for your colleagues to use, if you wish.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;summary&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Summary&lt;/h2&gt;
&lt;p&gt;You have now seen how to import standard and reusable C++ code into an R package using &lt;code&gt;Rcpp&lt;/code&gt;, use the package in R, and deploy it as a binary. The one remaining topic to cover is documentation, which we will take up next time.&lt;/p&gt;
&lt;/div&gt;

        &lt;script&gt;window.location.href=&#39;https://rviews.rstudio.com/2020/08/24/r-package-integration-with-modern-reusable-c-code-using-rcpp-part-5/&#39;;&lt;/script&gt;
      </description>
    </item>
    
    <item>
      <title>R Package Integration with Modern Reusable C&#43;&#43; Code Using Rcpp - Part 4</title>
      <link>https://rviews.rstudio.com/2020/08/18/r-package-integration-with-modern-reusable-c-code-using-rcpp-part-4/</link>
      <pubDate>Tue, 18 Aug 2020 00:00:00 +0000</pubDate>
      
      <guid>https://rviews.rstudio.com/2020/08/18/r-package-integration-with-modern-reusable-c-code-using-rcpp-part-4/</guid>
      <description>
        

&lt;p&gt;&lt;em&gt;Daniel Hanson is a full-time lecturer in the Computational Finance &amp;amp; Risk Management program within the Department of Applied Mathematics at the University of Washington.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In the &lt;a href=&#34;https://rviews.rstudio.com/2020/07/31/r-package-integration-with-modern-reusable-c-code-using-rcpp-part-3/&#34;&gt;previous post&lt;/a&gt; in this series, we looked at how to write interface files using &lt;code&gt;Rcpp&lt;/code&gt; to call functions and instantiate classes in standard and reusable C++, with a code interface and reusable code examples shown in the discussion.  My original plan for this week was to show how to import that code into an RStudio &lt;code&gt;Rcpp&lt;/code&gt; project and build it into an R package, but as there are a number of steps in the setup and build process, we&amp;rsquo;ll first look at a very simple example to demonstrate these, and then we&amp;rsquo;ll turn our attention to importing the reusable C++ code next time.&lt;/p&gt;

&lt;p&gt;The following discussion will be a step by step guide in the project configuration and build process with a single example &lt;code&gt;.cpp&lt;/code&gt; file that is included by default when creating an &lt;code&gt;Rcpp&lt;/code&gt; project in the RStudio IDE.&lt;/p&gt;

&lt;h2 id=&#34;creating-an-rcpp-package-project-in-the-rstudio-ide&#34;&gt;Creating an Rcpp Package Project in the RStudio IDE&lt;/h2&gt;

&lt;p&gt;Open the &lt;a href=&#34;https://rstudio.com/products/rstudio/&#34;&gt;RStudio IDE&lt;/a&gt;, and select &lt;code&gt;New Project...&lt;/code&gt; from the &lt;code&gt;File&lt;/code&gt; menu at the top, and select &lt;code&gt;New Directory&lt;/code&gt; as shown here:&lt;/p&gt;

&lt;p&gt;&lt;img src=&#34;NewProjectWizardFirst.png&#34; alt=&#34;New Project Wizard&#34; /&gt;&lt;/p&gt;

&lt;p&gt;Next, you will see the following selections, from which you should choose &lt;code&gt;R Package using Rcpp&lt;/code&gt;. Be sure to make this selection, and not &lt;code&gt;R package&lt;/code&gt; alone as shown above:&lt;/p&gt;

&lt;p&gt;&lt;img src=&#34;NewProjectWizard.png&#34; alt=&#34;Select R Package using Rcpp&#34; /&gt;&lt;/p&gt;

&lt;p&gt;Next, enter the desired directory path and new subdirectory name, and create the project; the subdirectory will be the name of your R package, e.g. &lt;code&gt;RcppProject&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;img src=&#34;PackageNameChoice.png&#34; alt=&#34;Type in your package name&#34; /&gt;&lt;/p&gt;

&lt;p&gt;When finished, your RStudio session should look something like this:&lt;/p&gt;

&lt;p&gt;&lt;img src=&#34;NewRcppProject.png&#34; alt=&#34;RStudio Rcpp project&#34; /&gt;&lt;/p&gt;

&lt;p&gt;There is one more step to complete in order to ensure your interface functions will be exposed as R functions to your package users.  In the &lt;code&gt;Files&lt;/code&gt; pane at lower right, you should see a file called &lt;code&gt;NAMESPACE&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;img src=&#34;Files_NAMESPACE_Red.png&#34; alt=&#34;The NAMESPACE file inside the package file structure&#34; /&gt;&lt;/p&gt;

&lt;p&gt;Double click on this file to open it in RStudio; you will see the following:&lt;/p&gt;

&lt;p&gt;&lt;img src=&#34;Orig_NAMESPACE.png&#34; alt=&#34;Original NAMESPACE file&#34; /&gt;&lt;/p&gt;

&lt;p&gt;Now, delete line 2, and then append a new line 3, as shown below.  This will allow your tagged C++ interface files to be exportable to R.  Leave line 4 blank, just as it is in the original.  Then, save the file:&lt;/p&gt;

&lt;p&gt;&lt;img src=&#34;Modified_NAMESPACE.png&#34; alt=&#34;Updated NAMESPACE file&#34; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Remark:&lt;/strong&gt;  There are more advanced ways to configure the &lt;code&gt;NAMESPACE&lt;/code&gt; file when building an R package, which would require in-depth explanation, distracting us from the main task of getting up and running with &lt;code&gt;Rcpp&lt;/code&gt;.  As such, we&amp;rsquo;ll just use this simple fix for our discussion.&lt;/p&gt;

&lt;h2 id=&#34;building-an-r-package&#34;&gt;Building an R Package&lt;/h2&gt;

&lt;p&gt;Returning to the &lt;code&gt;Files&lt;/code&gt; pane in the RStudio IDE (see &lt;em&gt;Figure 5&lt;/em&gt; above), note the following sudirectories:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;man&lt;/code&gt;:  For documentation files (we will return to this in a later installment).&lt;/li&gt;
&lt;li&gt;&lt;code&gt;R&lt;/code&gt;:  For R code to be included in a package; for now, we will only be concerned with C++ code.&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;code&gt;src&lt;/code&gt;:  This is where C++ code is located in the package:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Both header (&lt;code&gt;.h&lt;/code&gt;) and implementation (&lt;code&gt;.cpp&lt;/code&gt;) files&lt;/li&gt;
&lt;li&gt;Both interface and reusable C++ code files&lt;br /&gt;
&lt;br /&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By clicking on the &lt;code&gt;src&lt;/code&gt; subdirectory, you will see that there are two &lt;code&gt;C++&lt;/code&gt; files that are present by default in a blank &lt;code&gt;RStudio Rcpp&lt;/code&gt; project:&lt;/p&gt;

&lt;p&gt;&lt;img src=&#34;DefaultCppFiles.png&#34; alt=&#34;Default C++ files&#34; /&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;rcpp_hello_world.cpp&lt;/code&gt;:  Simple interface function included as an example, by default.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;RcppExports.cpp&lt;/code&gt;:  This is a C++ file that is generated each time the &lt;code&gt;Rcpp&lt;/code&gt; project is built in the RStudio environment.  You need not be concerned about its contents, but it is crucial that you &lt;em&gt;never modify this file&lt;/em&gt; on your own.&lt;br /&gt;
&lt;br /&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&#34;the-rcpp-hello-world-cpp-file&#34;&gt;The &lt;code&gt;rcpp_hello_world.cpp&lt;/code&gt; file&lt;/h3&gt;

&lt;p&gt;Let&amp;rsquo;s look at this simple example first.  It should look somewhat similar to the C++ interface files presented last time.  The main difference is it does not call any functions in an external file; it simply returns an R &lt;code&gt;List&lt;/code&gt; object to the function user in R.  Note the &lt;code&gt;// [[Rcpp::export]]&lt;/code&gt; tag; this will export the &lt;code&gt;rcpp_hello_world()&lt;/code&gt; function to R:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;#include &amp;lt;Rcpp.h&amp;gt;
using namespace Rcpp;
// [[Rcpp::export]]
List rcpp_hello_world() {
    CharacterVector x = CharacterVector::create( &amp;quot;foo&amp;quot;, &amp;quot;bar&amp;quot; )  ;
    NumericVector y   = NumericVector::create( 0.0, 1.0 ) ;
    List z            = List::create( x, y ) ;
    return z ;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;h3 id=&#34;your-first-r-package-with-c-code&#34;&gt;Your First R Package with C++ Code&lt;/h3&gt;

&lt;p&gt;Now, let&amp;rsquo;s build the package with this single C++ function.  To do this, from the &lt;code&gt;Build&lt;/code&gt; menu at the top of the RStudio IDE, and select &lt;code&gt;Clean and Rebuild&lt;/code&gt;.  In the upper right hand pane in the IDE, you will then see the C++ being compiled, and the package being built.&lt;/p&gt;

&lt;p&gt;&lt;img src=&#34;CleanAndRebuild.png&#34; alt=&#34;Select &#39;Clean and Rebuild&#39; to build the R package&#34; /&gt;&lt;/p&gt;

&lt;p&gt;When the build is complete, your R session will restart, and your package will be loaded into your current R session, as shown in the console at the bottom of the R Studio IDE:&lt;/p&gt;

&lt;p&gt;&lt;img src=&#34;RestartingRSession.png&#34; alt=&#34;R restarts and loads the package after the build is complete&#34; /&gt;&lt;/p&gt;

&lt;p&gt;Now, type in &lt;code&gt;rcpp_hello_world()&lt;/code&gt; at the console prompt, and check your results.  You should see the following:&lt;/p&gt;

&lt;p&gt;&lt;img src=&#34;rcpp_hello_world().png&#34; alt=&#34;Run &#39;rcpp_hello_world()&#39; in R&#34; /&gt;&lt;/p&gt;

&lt;p&gt;Congratulations!  You have just built your first R package with integrated C++, and you called the exported function from an R session.  You can also check that the package contents have been placed in the usual &lt;code&gt;.../R-4.0.x/library&lt;/code&gt; directory, in a subdirectory with the package name, just like any other R package you load from CRAN.&lt;/p&gt;

&lt;h3 id=&#34;distribute-the-package-as-a-binary&#34;&gt;Distribute the Package as a Binary&lt;/h3&gt;

&lt;p&gt;You can also export the package in binary form to a &lt;code&gt;.zip&lt;/code&gt; file on Windows, or a &lt;code&gt;tar.gz&lt;/code&gt; file on the Mac or on Linux.  To do this, again from the &lt;code&gt;Build&lt;/code&gt; menu, select &lt;code&gt;Build Binary Package&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;img src=&#34;BuildBinaryPackage.png&#34; alt=&#34;Build the package binary for distribution&#34; /&gt;&lt;/p&gt;

&lt;p&gt;You will again see the compile and build process in the upper right hand corner of the RStudio IDE.  When complete, you can find your distributable file, e.g. &lt;code&gt;RcppProject.zip&lt;/code&gt;, in the directory one level up from your project directory.  To deploy it, either copy it to another machine with the same OS and R setup, or delete the package subdirectory, e.g. &lt;code&gt;.../R-4.0.x/library/RcppProject&lt;/code&gt;.  Then, open an new RStudio session, and install the package just as you would any other package locally:&lt;br /&gt;
\newpage&lt;/p&gt;

&lt;p&gt;&lt;img src=&#34;InstallPackage.png&#34; alt=&#34;Install package locally&#34; /&gt;&lt;/p&gt;

&lt;p&gt;Next, load the package in your session, e.g. &lt;code&gt;library(RcppProject)&lt;/code&gt;, and then call the exported function again to verify it works.&lt;/p&gt;

&lt;h2 id=&#34;summary&#34;&gt;Summary&lt;/h2&gt;

&lt;p&gt;We have now covered the process of building an R package containing C++ code in RStudio IDE, by integrating the code into an &lt;code&gt;Rcpp&lt;/code&gt; project.  The C++ code in this case consisted of a single &lt;code&gt;.cpp&lt;/code&gt; file, with a single interface function tagged for export to R, to keep the discussion focused more on the process itself.  Next time, we will revisit the C++ code file examples in the previous post, and show how to integrate them into an R package.  The process is essentially the same as above, but with multiple source code files and multiple interface files, it will involve some additional management and other details.&lt;/p&gt;

        &lt;script&gt;window.location.href=&#39;https://rviews.rstudio.com/2020/08/18/r-package-integration-with-modern-reusable-c-code-using-rcpp-part-4/&#39;;&lt;/script&gt;
      </description>
    </item>
    
    <item>
      <title>R Package Integration with Modern Reusable C&#43;&#43; Code Using Rcpp - Part 3</title>
      <link>https://rviews.rstudio.com/2020/07/31/r-package-integration-with-modern-reusable-c-code-using-rcpp-part-3/</link>
      <pubDate>Fri, 31 Jul 2020 00:00:00 +0000</pubDate>
      
      <guid>https://rviews.rstudio.com/2020/07/31/r-package-integration-with-modern-reusable-c-code-using-rcpp-part-3/</guid>
      <description>
        


&lt;p&gt;&lt;em&gt;Daniel Hanson is a full-time lecturer in the Computational Finance &amp;amp; Risk Management program within the Department of Applied Mathematics at the University of Washington.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;In the &lt;a href=&#34;https://rviews.rstudio.com/2020/07/14/r-package-integration-with-modern-reusable-c-code-using-rcpp-part-2/&#34;&gt;previous post&lt;/a&gt; in this series, we looked at some design considerations when integrating standard and reusable C++ code into an R package. Specific emphasis was on Rcpp’s role in facilitating a means of communication between R and the C++ code, particularly highlighting a few of the C++ functions in the &lt;code&gt;Rcpp&lt;/code&gt; namespace that conveniently and efficiently pass data between an R &lt;code&gt;numeric&lt;/code&gt; vector and a C++ &lt;code&gt;std::vector&amp;lt;double&amp;gt;&lt;/code&gt; object.&lt;/p&gt;
&lt;p&gt;Today, we will look at a specific example of implementing the interface. We will see how to configure code that allows the use of standard reusable C++ in an R package, without having to modify it with any R or Rcpp-specific syntax. It is admittedly a simple and toy example, but the goal is to provide a starting point that can be easily extended for more real world examples.&lt;/p&gt;
&lt;div id=&#34;the-code&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;The Code&lt;/h2&gt;
&lt;p&gt;To get started, let’s have a look at the code we will use for our demonstration. It is broken into three categories, consistent with the design considerations from the previous post:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Standard and reusable C++: No dependence on R or Rcpp&lt;/li&gt;
&lt;li&gt;Interface level C++: Uses functions in the &lt;code&gt;Rcpp&lt;/code&gt; namespace&lt;/li&gt;
&lt;li&gt;R functions exported by the interface level: Same names as in the interface level&lt;/li&gt;
&lt;/ul&gt;
&lt;div id=&#34;standard-and-reusable-c-code&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Standard and Reusable C++ Code&lt;/h3&gt;
&lt;p&gt;In this example, we will use a small set of C++ non-member functions, and two classes. There is a declaration (header) file for the non-member functions, say &lt;code&gt;NonmemberCppFcns.h&lt;/code&gt;, and another with class declarations for two shape classes, &lt;code&gt;Square&lt;/code&gt; and &lt;code&gt;Circle&lt;/code&gt;, called &lt;code&gt;ConcreteShapes.h&lt;/code&gt;. Each of these is accompanied by a corresponding implementation file, with file extension &lt;code&gt;.cpp&lt;/code&gt;, as one might expect in a more realistic C++ code base.&lt;/p&gt;
&lt;div id=&#34;nonmember-c-functions&#34; class=&#34;section level4&#34;&gt;
&lt;h4&gt;Nonmember C++ Functions&lt;/h4&gt;
&lt;p&gt;These functions are shown and described here, as declared in the following C++ header file:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;#include &amp;lt;vector&amp;gt;
// Adds two real numbers
double add(double x, double y);
// Sorts a vector of real numbers and returns it
std::vector&amp;lt;double&amp;gt; sortVec(std::vector&amp;lt;double&amp;gt; v);
// Computes the product of the LCM and GCD of two integers, 
// using C++17 functions std::lcm(.) and std::gcd(.)
int prodLcmGcd(int m, int n);&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The last function uses recently added features in the C++ Standard Library, to show that we can use C++17.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;c-classes&#34; class=&#34;section level4&#34;&gt;
&lt;h4&gt;C++ Classes&lt;/h4&gt;
&lt;p&gt;The two classes in our reusable code base are declared in the &lt;code&gt;ConcreteShapes.h&lt;/code&gt; file, as shown and described here. Much like textbook C++ examples, we’ll write classes for two geometric shapes, each with a member function to compute the area of its corresponding object.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;#include &amp;lt;cmath&amp;gt;
class Circle
{
public:
  Circle(double radius);
  
  // Computes the area of a circle with given radius
  double area() const;
  
private:
  double radius_;
};
class Square
{
public:
  Square(double side);
  
  // Computes the area of a square with given side length
  double area() const;
private:
  double side_;
};&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;interface-level-c&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Interface Level C++&lt;/h3&gt;
&lt;p&gt;Now, the next step is to employ Rcpp, namely for the following essential tasks:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Export the interface functions to R&lt;/li&gt;
&lt;li&gt;Facilitate data exchange between R and C++ container objects&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;An interface file containing functions designated for export to R does not require a header file with declarations; one can think of it as being analogous to a &lt;code&gt;.cpp&lt;/code&gt; file that contains the &lt;code&gt;main()&lt;/code&gt; function in a C++ executable project. In addition, the interface can be contained in one file, or split into multiple files. For demonstration, I have written two such files: &lt;code&gt;CppInterface.cpp&lt;/code&gt;, which provides the interface to the non-member functions above, and &lt;code&gt;CppInterface2.cpp&lt;/code&gt;, which does the same for the two C++ classes.&lt;/p&gt;
&lt;div id=&#34;interface-to-non-member-c-functions&#34; class=&#34;section level4&#34;&gt;
&lt;h4&gt;Interface to Non-Member C++ Functions:&lt;/h4&gt;
&lt;p&gt;Let’s first have a look the &lt;code&gt;CppInterface.cpp&lt;/code&gt; interface file, which connects R with the nonmember functions in our C++ code base:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;#include &amp;quot;NonmemberCppFcns.h&amp;quot;
#include &amp;lt;vector&amp;gt;
#include &amp;lt;Rcpp.h&amp;gt;     
// Nonmember Function Interfaces:
// [[Rcpp::export]]
int rAdd(double x, double y)
{
  // Call the add(.) function in the reusable C++ code base:
  return add(x, y);
}
// [[Rcpp::export]]
Rcpp::NumericVector rSortVec(Rcpp::NumericVector v)
{
  // Transfer data from NumericVector to std::vector&amp;lt;double&amp;gt;
  auto stlVec = Rcpp::as&amp;lt;std::vector&amp;lt;double&amp;gt;&amp;gt;(v); 
  
  // Call the reusable sortVec(.) function, with the expected
  // std::vector&amp;lt;double&amp;gt; argument:
  stlVec = sortVec(stlVec);
  
  // Reassign the results from the vector&amp;lt;double&amp;gt; return object
  // to the same NumericVector v, using Rcpp::wrap(.):
  v = Rcpp::wrap(stlVec);
  
  // Return as an Rcpp::NumericVector:
  return v;
}
// C++17 example:
// [[Rcpp::export]]
int rProdLcmGcd(int m, int n)
{
  return prodLcmGcd(m, n);
}&lt;/code&gt;&lt;/pre&gt;
&lt;div id=&#34;included-declarations&#34; class=&#34;section level5&#34;&gt;
&lt;h5&gt;Included Declarations:&lt;/h5&gt;
&lt;p&gt;The &lt;code&gt;NonmemberCppFcns.h&lt;/code&gt; declaration file is included at the top with &lt;code&gt;#include&lt;/code&gt;, just as it would in a standalone C++ application, so that the interface will recognize these functions that reside in the reusable code base. The STL &lt;code&gt;vector&lt;/code&gt; declaration is required, as we shall soon see. And, the key in making the interface work resides in the &lt;code&gt;Rcpp.h&lt;/code&gt; file, which provides access to very useful C++ functions in the &lt;code&gt;Rcpp&lt;/code&gt; namespace.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;function-implementations&#34; class=&#34;section level5&#34;&gt;
&lt;h5&gt;Function implementations:&lt;/h5&gt;
&lt;p&gt;Each of these functions is designated for export to R when the package is built, by placing the
&lt;code&gt;//&lt;/code&gt; &lt;code&gt;[[Rcpp::export]]&lt;/code&gt; tag just above the each function signature, as shown above. In this particular example, each interface function simply calls a function in the reusable code base. For example, the &lt;code&gt;rAdd(.)&lt;/code&gt; function simply calls the &lt;code&gt;add(.)&lt;/code&gt; function in the reusable C++ code. In the absence of a user-defined namespace, the interface function name must be different from the function it calls to prevent name clash errors during the build, so I have simply chosen to prefix an &lt;code&gt;r&lt;/code&gt; to the name of each interface function.&lt;/p&gt;
&lt;p&gt;Note that the &lt;code&gt;rSort(.)&lt;/code&gt; function takes in an &lt;code&gt;Rcpp::NumericVector&lt;/code&gt; object, &lt;code&gt;v&lt;/code&gt;. This type will accept data passed in from R as a &lt;code&gt;numeric&lt;/code&gt; vector and present it as a C++ object. Then, so that we can call a function in our code base, such as &lt;code&gt;sort(.)&lt;/code&gt;, which expects a &lt;code&gt;std::vector&amp;lt;double&amp;gt;&lt;/code&gt; type as its input, &lt;code&gt;Rcpp&lt;/code&gt; also provides the &lt;code&gt;Rcpp::as&amp;lt;.&amp;gt;(.)&lt;/code&gt; function that facilitates the transfer of data from an &lt;code&gt;Rcpp::NumericVector&lt;/code&gt; object to the STL container:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;auto stlVec = Rcpp::as&amp;lt;std::vector&amp;lt;double&amp;gt;&amp;gt;(v);&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;Rcpp&lt;/code&gt; also gives us a function that will transfer data from a &lt;code&gt;std::vector&amp;lt;double&amp;gt;&lt;/code&gt; type being returned from our reusable C++ code back into an &lt;code&gt;Rcpp::NumericVector&lt;/code&gt;, so that the results can be passed back to R as a familiar &lt;code&gt;numeric&lt;/code&gt; vector type:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;v = Rcpp::wrap(stlVec);&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;As the &lt;code&gt;std::vector&amp;lt;double&amp;gt;&lt;/code&gt; object is the workhorse C++ STL containers in quantitative work, these two &lt;code&gt;Rcpp&lt;/code&gt; functions are a godsend.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Remark 1:&lt;/strong&gt; There is no rule that says an interface function can only call a single function in the reusable code; one can use whichever functions or classes that are needed to get the job done, just like with any other C++ function. I’ve merely kept it simple here for demonstration purposes.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Remark 2:&lt;/strong&gt; The tag &lt;code&gt;// [[Rcpp::plugins(cpp17)]]&lt;/code&gt; is sometimes placed at the top of a C++ source file in online examples related to &lt;code&gt;Rcpp&lt;/code&gt; and C++17. I have not found this necessary in my own code, however, as long as the &lt;code&gt;Makeconf&lt;/code&gt; file has been updated for C++17, as described in the &lt;a href=&#34;https://rviews.rstudio.com/2020/07/08/r-package-integration-with-modern-reusable-c-code-using-rcpp/&#34;&gt;first post&lt;/a&gt; in this series.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;interface-to-c-classes&#34; class=&#34;section level4&#34;&gt;
&lt;h4&gt;Interface to C++ Classes:&lt;/h4&gt;
&lt;p&gt;We now turn our attention to the second interface file, &lt;code&gt;CppInterface2.cpp&lt;/code&gt;, which connects R with the C++ classes in our reusable code. It is shown here:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;#include &amp;quot;ConcreteShapes.h&amp;quot;
// Class Member Function Interfaces:
// Interface to Square member
// function area(.):
// [[Rcpp::export]]
double squareArea(double side)
{
  Square sq(side);
  return sq.area();
}
// Interface to Circle member
// function area(.):
// [[Rcpp::export]]
double circleArea(double radius)
{
  Circle circ(radius);
  return circ.area();
}&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is again nothing terribly sophisticated, but the good news is it shows the process of creating instances of classes from the code base is not difficult at all. We first &lt;code&gt;#include&lt;/code&gt; only the header file containing these class declarations; &lt;code&gt;Rcpp.h&lt;/code&gt; is not required here, as we are not using any functions in the the Rcpp namespace.&lt;/p&gt;
&lt;p&gt;To compute the area of a square, the &lt;code&gt;side&lt;/code&gt; length is input in R as a simple &lt;code&gt;numeric&lt;/code&gt; type and passed to the interface function as a C++ &lt;code&gt;double&lt;/code&gt;. The &lt;code&gt;Square&lt;/code&gt; object, &lt;code&gt;sq&lt;/code&gt;, is constructed with the &lt;code&gt;side&lt;/code&gt; argument, and its &lt;code&gt;area()&lt;/code&gt; member function performs said calculation and returns the result. The process is trivally similar for the &lt;code&gt;circleArea(.)&lt;/code&gt; function.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;r-functions-exported-by-the-interface-level&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;R Functions Exported by the Interface Level&lt;/h3&gt;
&lt;p&gt;To wrap up this discussion, let’s look at the functions an R user will have available after we build the package in RStudio (coming next in this series). Each of these functions will be exported from their respective C++ interface functions as regular R functions, namely:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;rAdd(.)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;rSortVec(.)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;rProdLcmGcd(.)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;squareArea(.)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;circleArea(.)&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The package user will not need to know or care that the core calculations are being performed in C++. Visually, we can represent the associations as shown in the following diagram:&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;CompositeCodeDiagram.png&#34; alt = &#34;Mapping R Package Functions to Reusable C++&#34; height = &#34;400&#34; width=&#34;600&#34;&gt;&lt;/p&gt;
&lt;p&gt;The solid red line represents a “Chinese Wall” that separates our code base from the interface and allows us to maintain it as standard and reusable C++.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;summary&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Summary&lt;/h2&gt;
&lt;p&gt;This concludes our example of configuring code that allows the use of standard reusable C++ in an R package, without having to modify it with any R or Rcpp-specific syntax. In the next post, we will examine how to actually build this code into an R package by leveraging the convenience of Rcpp and RStudio, and deploy it for any number of R users. The source code will also be made available so that you can try it out for yourself.&lt;/p&gt;
&lt;/div&gt;

        &lt;script&gt;window.location.href=&#39;https://rviews.rstudio.com/2020/07/31/r-package-integration-with-modern-reusable-c-code-using-rcpp-part-3/&#39;;&lt;/script&gt;
      </description>
    </item>
    
  </channel>
</rss>
