<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.thalesians.com/index.php?action=history&amp;feed=atom&amp;title=Programming%2FR%2FDebugging_R_packages_written_in_CPP</id>
	<title>Programming/R/Debugging R packages written in CPP - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.thalesians.com/index.php?action=history&amp;feed=atom&amp;title=Programming%2FR%2FDebugging_R_packages_written_in_CPP"/>
	<link rel="alternate" type="text/html" href="https://wiki.thalesians.com/index.php?title=Programming/R/Debugging_R_packages_written_in_CPP&amp;action=history"/>
	<updated>2026-06-01T21:36:09Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.37.1</generator>
	<entry>
		<id>https://wiki.thalesians.com/index.php?title=Programming/R/Debugging_R_packages_written_in_CPP&amp;diff=751&amp;oldid=prev</id>
		<title>Admin: Created page with &quot;=Debugging R packages written in C++=  Suppose you are faced with an R package, the bulk of which is written in C++. Such a package may be monolithic, and without looking at the C++ code it may be difficult to figure out what’s going on.  The R package &quot;specs&quot; (https://github.com/wijler/specs/) is an example of such a package.  First, a word of warning: don’t try to compile this code using Visual Studio. You will run into all sorts of compatibility issues with R head...&quot;</title>
		<link rel="alternate" type="text/html" href="https://wiki.thalesians.com/index.php?title=Programming/R/Debugging_R_packages_written_in_CPP&amp;diff=751&amp;oldid=prev"/>
		<updated>2023-12-24T15:22:29Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;=Debugging R packages written in C++=  Suppose you are faced with an R package, the bulk of which is written in C++. Such a package may be monolithic, and without looking at the C++ code it may be difficult to figure out what’s going on.  The R package &amp;quot;specs&amp;quot; (https://github.com/wijler/specs/) is an example of such a package.  First, a word of warning: don’t try to compile this code using Visual Studio. You will run into all sorts of compatibility issues with R head...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;=Debugging R packages written in C++=&lt;br /&gt;
&lt;br /&gt;
Suppose you are faced with an R package, the bulk of which is written in C++. Such a package may be monolithic, and without looking at the C++ code it may be difficult to figure out what’s going on.&lt;br /&gt;
&lt;br /&gt;
The R package &amp;quot;specs&amp;quot; (https://github.com/wijler/specs/) is an example of such a package.&lt;br /&gt;
&lt;br /&gt;
First, a word of warning: don’t try to compile this code using Visual Studio. You will run into all sorts of compatibility issues with R headers and libraries and can potentially waste quite a bit of time. This approach is therefore not recommended.&lt;br /&gt;
&lt;br /&gt;
This is what we are going to do instead. First, we are going to fork the R package, thus obtaining https://github.com/sydx/specs/ (sydx is my GitHub username).&lt;br /&gt;
&lt;br /&gt;
Next, we are going to install this package into R, from GitHub. For this we can either use the R package &amp;quot;pak&amp;quot; or the R package &amp;quot;devtools&amp;quot;. We will go for the more standard &amp;quot;devtools&amp;quot;.&lt;br /&gt;
It turns out it’s not a trivial task to install &amp;quot;devtools&amp;quot; itself, even on a new AWS instance with a clean Ubuntu installation. &amp;quot;devtools&amp;quot; has various prerequisites and we can get stuck at several steps. Let us consider these prerequisites and what can go wrong during their installation.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
install.packages(&amp;quot;openssl&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You may get the error message&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
-------------------------- [ERROR MESSAGE] ---------------------------&lt;br /&gt;
tools/version.c:1:10: fatal error: openssl/opensslv.h: No such file or directory&lt;br /&gt;
1 | #include &amp;lt;openssl/opensslv.h&amp;gt;&lt;br /&gt;
| ^~~~~~~~~~~~~~~~~~~~&lt;br /&gt;
compilation terminated.&lt;br /&gt;
--------------------------------------------------------------------&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This is solved with&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sudo apt-get install libssl-dev&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Next prerequisite:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
install.packages(&amp;quot;curl&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
You may get the error message&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
-------------------------- [ERROR MESSAGE] ---------------------------&lt;br /&gt;
&amp;lt;stdin&amp;gt;:1:10: fatal error: curl/curl.h: No such file or directory&lt;br /&gt;
compilation terminated.&lt;br /&gt;
--------------------------------------------------------------------&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This is solved with&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sudo apt-get install libcurl4-openssl-dev&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Next prerequisite:&lt;br /&gt;
install.packages(&amp;quot;usethis&amp;quot;)&lt;br /&gt;
Next prerequisite:&lt;br /&gt;
install.packages(&amp;quot;systemfonts&amp;quot;)&lt;br /&gt;
You may get the error message&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
-------------------------- [ERROR MESSAGE] ---------------------------&lt;br /&gt;
&amp;lt;stdin&amp;gt;:1:10: fatal error: fontconfig/fontconfig.h: No such file or directory&lt;br /&gt;
compilation terminated.&lt;br /&gt;
--------------------------------------------------------------------&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This is solved with&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sudo apt-get install libfontconfig1-dev&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Next prerequisite:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
install.packages(&amp;quot;textshaping&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
You may get the error message&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
-------------------------- [ERROR MESSAGE] ---------------------------&lt;br /&gt;
&amp;lt;stdin&amp;gt;:1:10: fatal error: hb-ft.h: No such file or directory&lt;br /&gt;
compilation terminated.&lt;br /&gt;
--------------------------------------------------------------------&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This is solved with&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sudo apt-get install libharfbuzz-dev&lt;br /&gt;
sudo apt-get install libfribidi-dev&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Next prerequisite:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
install.packages(&amp;quot;ragg&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
You may run into the error message&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
-------------------------- [ERROR MESSAGE] ---------------------------&lt;br /&gt;
&amp;lt;stdin&amp;gt;:1:10: fatal error: ft2build.h: No such file or directory&lt;br /&gt;
compilation terminated.&lt;br /&gt;
--------------------------------------------------------------------&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
And a few other problems when installing this package. This is solved with&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
sudo apt-get install libfreetype6-dev&lt;br /&gt;
sudo apt-get install libfreetype6-dev libpng-dev libtiff5-dev libjpeg-dev&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
and then installing a development version of this package from the source code repository using the R package &amp;quot;pak&amp;quot;:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
install.packages(&amp;quot;pak&amp;quot;)&lt;br /&gt;
pak::pak(&amp;quot;r-lib/ragg&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
(In fact, at this point, we could have installed our R package of interest, &amp;quot;specs&amp;quot;, using &amp;quot;pak&amp;quot;, but we decided to still install it using &amp;quot;devtools&amp;quot;.)&lt;br /&gt;
&lt;br /&gt;
Next prerequisites:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
install.packages(&amp;quot;pkgdown&amp;quot;)&lt;br /&gt;
install.packages(&amp;quot;rcmdcheck&amp;quot;)&lt;br /&gt;
install.packages(&amp;quot;rversions&amp;quot;)&lt;br /&gt;
install.packages(&amp;quot;urlchecker&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Finally, we can install &amp;quot;devtools&amp;quot; itself:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
install.packages(&amp;quot;devtools&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Using &amp;quot;devtools&amp;quot;, we install the &amp;quot;specs&amp;quot; package from our forked repository:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
library(&amp;quot;devtools&amp;quot;)&lt;br /&gt;
devtools::install_github(&amp;quot;sydx/specs&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
We can then load this package and work with it in R as usual:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
library(specs)&lt;br /&gt;
unemployment &amp;lt;- Unempl_GT[,1] #Extract the Dutch unemployment levels (x1,000)&lt;br /&gt;
GT &amp;lt;- Unempl_GT[,2:11] #Select the first ten Google Trends&lt;br /&gt;
my_specs &amp;lt;- specs(unemployment,GT,p=1) #Estimate specs&lt;br /&gt;
my_coefs &amp;lt;- my_specs$gammas #store the coefficients&lt;br /&gt;
y_d &amp;lt;- my_specs$y_d #Transformed dependent variable&lt;br /&gt;
z_l &amp;lt;- my_specs$v #Transformed independent variables&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This package does not provide much in terms of diagnostics, so we are going to add some debug output to the package source, which is in C++. First,&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
remove.packages('specs')&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
From shell, we&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
cd ~&lt;br /&gt;
mkdir dev&lt;br /&gt;
cd dev&lt;br /&gt;
git clone git@github.com:sydx/specs.git&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
(Make sure that we have the correct SSH keys on GitHub and on our operating system – see https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent)&lt;br /&gt;
&lt;br /&gt;
Now&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
cd specs/src&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
and&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
vim specs.cpp&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
(Of course, if you prefer emacs, use emacs, etc.) To the top of the file, add&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
To the beginning of the body of the specs_rcpp function, add&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;In specs_rcpp...&amp;quot; &amp;lt;&amp;lt; std::endl;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
git add specs.cpp&lt;br /&gt;
git commit -m &amp;quot;Added some debug output.&amp;quot;&lt;br /&gt;
git push&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Next time you go back to R&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
library(&amp;quot;devtools&amp;quot;)&lt;br /&gt;
devtools::install_github(&amp;quot;sydx/specs&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
and run the same R code, you will see the newly added debug output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;gt; library(specs)&lt;br /&gt;
&amp;gt; unemployment &amp;lt;- Unempl_GT[,1] #Extract the Dutch unemployment levels (x1,000)&lt;br /&gt;
GT &amp;lt;- Unempl_GT[,2:11] #Select the first ten Google Trends&lt;br /&gt;
my_specs &amp;lt;- specs(unemployment,GT,p=1) #Estimate specs&lt;br /&gt;
In specs_rcpp...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now you can use debug output (or indeed attach a debugger, such as gdb) to understand what the package is doing step by step and relate these actions to the corresponding research paper.&lt;/div&gt;</summary>
		<author><name>Admin</name></author>
	</entry>
</feed>