{"id":190887,"date":"2023-05-31T09:31:42","date_gmt":"2023-05-31T13:31:42","guid":{"rendered":"https:\/\/ibkrcampus.com\/?p=190887"},"modified":"2023-05-31T09:31:17","modified_gmt":"2023-05-31T13:31:17","slug":"a-step-by-step-guide-to-write-an-r-package-that-uses-c-code-ubuntu","status":"publish","type":"post","link":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/a-step-by-step-guide-to-write-an-r-package-that-uses-c-code-ubuntu\/","title":{"rendered":"A Step by Step Guide to Write an R package That Uses C++ Code (Ubuntu)"},"content":{"rendered":"\n<p><em>The post &#8220;A Step by Step Guide to Write an R package That Uses C++ Code (Ubuntu)<\/em>&#8221; <em>was originally posted on <a href=\"https:\/\/pacha.dev\/blog\/2023\/05\/21\/r-package-with-c-code-ubuntu\/\">Pacha.dev Blog<\/a>.<\/em><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-motivation\">Motivation<\/h3>\n\n\n\n<p>A large part of my research interest requires to estimate computationally intensive models, such as the General Equilibrium Poisson Pseudo Maximum Likelihood (GEPPML) estimator derived from the equilibrium conditions introduced by Anderson and Van Wincoop (2004) for estimation and inference.<\/p>\n\n\n\n<p>The GEPPML estimator is a computationally intensive estimator that requires to solve a system of non-linear equations, and for this task we might be better-off by using a compiled language such as C++. The good news is that we can use C++ code within R and Python, and this blog post is about using C++ functions from R.<\/p>\n\n\n\n<p>Also, I do not pretend to be an expert on C++ or debate if R is better than Python. I use both from Visual Studio Code. I do want to share my experience on how to use C++ code within R.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-honest-disclaimer\">Honest disclaimer<\/h3>\n\n\n\n<p>This blog post is a summary of what worked after hours of fails for my future self. I hope it helps you too.<\/p>\n\n\n\n<p>I am a Statistician and Political Scientist,&nbsp;<strong>not<\/strong>&nbsp;a Computer Scientist!<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-setup\">Setup<\/h3>\n\n\n\n<p>Because I have been already learning C++ version 11, I decided to install&nbsp;<code>llvm-11<\/code>&nbsp;on my laptop that has Linux Mint installed, and which is based on Ubuntu 22.04.<\/p>\n\n\n\n<p>Ubuntu and its derived distributions use&nbsp;<code>gcc<\/code>&nbsp;as the default C++ compiler, and&nbsp;<code>clang<\/code>&nbsp;is not installed by default. Different resources mention that&nbsp;<code>clang<\/code>&nbsp;provides more informative error messages when the compilation fails and when we debug code.<\/p>\n\n\n\n<p>Counting on informative error messages is highly useful resource when we are learning C++ or when our code is failing in two different ways, being one that it does not compile, and the other that it compiles but then when we call a function from RStudio (or VSCode) it crashes the R session.<\/p>\n\n\n\n<p>I installed the R packages&nbsp;<code>cpp11<\/code>&nbsp;and&nbsp;<code>usethis<\/code>.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">install.packages(c(\"cpp11\", \"usethis\"))<\/pre>\n\n\n\n<p>I created a file&nbsp;<code>~\/.Rprofile<\/code>&nbsp;containing the following lines.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">library(devtools)\nlibrary(usethis)\nlibrary(cpp11)<\/pre>\n\n\n\n<p>I run&nbsp;<code>nano ~\/.R\/Makevars<\/code>&nbsp;from bash and then saved with CTRL+O+ENTER and closed it with CTRL+X. It is the same as creating it with the text editor from Gnome or any other desktop environment.<\/p>\n\n\n\n<p>Now forget about&nbsp;<code>devtools::install()<\/code>. After reopening your editor, every time you use RStudio (or VSCode) you just call&nbsp;<code>install()<\/code>, and the same applies to&nbsp;<code>usethis::use_*()<\/code>&nbsp;and&nbsp;<code>cpp11::cpp_*()<\/code>&nbsp;functions.<\/p>\n\n\n\n<p>To install&nbsp;<code>llvm-11<\/code>&nbsp;I downloaded the installation script from the official LLVM repository, and it also installed&nbsp;<code>clang-11<\/code>.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">cd Downloads\nwget https:\/\/apt.llvm.org\/llvm.sh\nchmod +x llvm.sh\nsudo .\/llvm.sh 11<\/pre>\n\n\n\n<p>Up to this point I still had the following error messages when compiling C++ code.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">fatal error: 'cstdio' file not found\nfatal error: 'vector' file not found\ncannot find -lc++abi: No such file or directory<\/pre>\n\n\n\n<p>I had to install additional packages. This took me a few hours searching on the Internet until I figured it out.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">sudo apt install g++-11 libc++-11-dev libc++abi-11-dev<\/pre>\n\n\n\n<p>To be sure that the&nbsp;<code>install()<\/code>&nbsp;function in R uses the correct version of&nbsp;<code>clang++<\/code>&nbsp;I created the&nbsp;<code>~\/.R\/Makevars<\/code>&nbsp;file. The contents of the file are the following.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">CLANGVER=-11\nCLANGLIB=-stdlib=libc++\nCXX=$(CCACHE) clang++$(CLANGVER) $(CLANGLIB)\nCXX11=$(CCACHE) clang++$(CLANGVER) $(CLANGLIB)\nCC=$(CCACHE) clang$(CLANGVER)\nSHLIB_CXXLD=clang++$(CLANGVER) $(CLANGLIB)\nCXXFLAGS=-Wall -O0 -pedantic\nCXX11FLAGS=-Wall -O0 -pedantic<\/pre>\n\n\n\n<p>For both&nbsp;<code>CXXFLAGS<\/code>&nbsp;and&nbsp;<code>CXX11FLAGS<\/code>&nbsp;I am using&nbsp;<code>-O0<\/code>&nbsp;to avoid optimization, which is useful for debugging. After the code is working, I can change it to&nbsp;<code>-O3<\/code>&nbsp;to optimize the compiled code.<\/p>\n\n\n\n<p>If later on I need to compile with&nbsp;<code>gcc<\/code>, I can open&nbsp;<code>~\/.R\/Makevars<\/code>, comment all the lines, restart RStudio or VSCode, and run&nbsp;<code>install()<\/code>&nbsp;again.<\/p>\n\n\n\n<p>If you close RStudio (or VSCode) and open it again, you can check that the changes were implemented by running&nbsp;<code>pkgbuild::check_build_tools(debug = TRUE)<\/code>, which should return the following output.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Trying to compile a simple C file\nRunning \/usr\/lib\/R\/bin\/R CMD SHLIB foo.c\nusing C compiler: \u2018Ubuntu clang version 11.1.0-6\u2019\nclang-11 -I\"\/usr\/share\/R\/include\" -DNDEBUG       -fpic  -g -O2 -ffile-prefix-map=\/build\/r-base-JhpCKt\/r-base-4.3.0=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2  -c foo.c -o foo.o\nclang-11 -shared -L\/usr\/lib\/R\/lib -Wl,-Bsymbolic-functions -flto=auto -ffat-lto-objects -flto=auto -Wl,-z,relro -o foo.so foo.o -L\/usr\/lib\/R\/lib -lR<\/pre>\n\n\n\n<p>If I were using gcc, the output would have been as in the following lines.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Trying to compile a simple C file\nRunning \/usr\/lib\/R\/bin\/R CMD SHLIB foo.c\nusing C compiler: \u2018gcc (Ubuntu 11.3.0-1ubuntu1~22.04.1) 11.3.0\u2019\ngcc -I\"\/usr\/share\/R\/include\" -DNDEBUG       -fpic  -g -O2 -ffile-prefix-map=\/build\/r-base-JhpCKt\/r-base-4.3.0=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2  -c foo.c -o foo.o\ngcc -shared -L\/usr\/lib\/R\/lib -Wl,-Bsymbolic-functions -flto=auto -ffat-lto-objects -flto=auto -Wl,-z,relro -o foo.so foo.o -L\/usr\/lib\/R\/lib -lR<\/pre>\n\n\n\n<p>The key here is that when I use&nbsp;<code>clang<\/code>&nbsp;the lines start with&nbsp;<code>clang<\/code>, not with&nbsp;<code>gcc<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Creating a dummy package<\/h3>\n\n\n\n<p>From RStudio (or VSCode) we can create a new package by running&nbsp;<code>create_package(\"~\/cpp11dummypackage\")<\/code>. This will create a new folder with the name&nbsp;<code>cpp11dummypackage<\/code>. Then I run&nbsp;<code>use_cpp11()<\/code>&nbsp;to add the required files to use C++ code within R.<\/p>\n\n\n\n<p>Then I run&nbsp;<code>use_r(\"cpp11dummypackage-package\")<\/code>&nbsp;to create a new R script file with the name&nbsp;<code>cpp11dummypackage-package.R<\/code>&nbsp;within the&nbsp;<code>R<\/code>&nbsp;folder, and added the following code to it.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#' @useDynLib cpp11dummypackage, .registration = TRUE\nNULL<\/pre>\n\n\n\n<p>The&nbsp;<code>usethis<\/code>&nbsp;skeleton also created the file&nbsp;<code>src\/code.cpp<\/code>&nbsp;for us. I added a simple function to transpose a matrix to it, by replacing the file contents by the following lines.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#include &lt;cpp11.hpp>\n\nusing namespace cpp11;\n    \n[[cpp11::register]] doubles_matrix&lt;> transpose_(doubles_matrix&lt;> X)\n{\n    int NX = X.nrow();\n    int MX = X.ncol();\n\n    writable::doubles_matrix&lt;> R(MX, NX);\n\n    for (int i = 0; i &lt; MX; i++)\n    {\n        for (int j = 0; j &lt; NX; j++)\n        {\n            R(i, j) = X(j, i);\n        }\n    }\n\n    return R;\n}<\/pre>\n\n\n\n<p>In order to export the function, I added the following lines to&nbsp;<code>cpp11dummypackage-package.R<\/code>.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#' Transpose a matrix\n#' @export\n#' @param X numeric matrix\n#' @return numeric matrix\n#' @examples\n#' set.seed(1234)\n#' X &lt;- matrix(rnorm(4), nrow = 2, ncol = 2)\n#' X\n#' transpose(X)\ntranspose &lt;- function(X) {\n  transpose_(X)\n}<\/pre>\n\n\n\n<p>I tested the functions after running&nbsp;<code>cpp11_register()<\/code>&nbsp;and&nbsp;<code>load_all()<\/code>.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">> set.seed(1234)\n\n> X &lt;- matrix(rnorm(4), nrow = 2, ncol = 2)\n\n> X\n           [,1]      [,2]\n[1,] -1.2070657  1.084441\n[2,]  0.2774292 -2.345698\n\n> transpose(X)\n          [,1]       [,2]\n[1,] -1.207066  0.2774292\n[2,]  1.084441 -2.3456977<\/pre>\n\n\n\n<p>If I would have passed&nbsp;<code>1:4<\/code>&nbsp;instead of&nbsp;<code>rnorm(4)<\/code>&nbsp;to&nbsp;<code>matrix()<\/code>, I would have obtained the following error message.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">> transpose(X)\nError: Invalid input type, expected 'double' actual 'integer'<\/pre>\n\n\n\n<p>This is because I declared the function to accept a&nbsp;<code>doubles_matrix&lt;&gt;<\/code>&nbsp;as input, and not an&nbsp;<code>integers_matrix&lt;&gt;<\/code>.<\/p>\n\n\n\n<p>To install the recently created package, I run the following lines in the R console.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">clean_dll()\ncpp_register()\ndocument()\ninstall()<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Debugging the package<\/h3>\n\n\n\n<p>In order to access debugging symbols, I created a new&nbsp;<code>Makevars<\/code>&nbsp;file within the&nbsp;<code>src<\/code>&nbsp;folder, and added the following lines.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">CXX_STD = CXX11\nPKG_CPPFLAGS = -UDEBUG -g<\/pre>\n\n\n\n<p>Then I reinstalled the package compiled with debugging symbols, and in bash I run&nbsp;<code>R -d lldb-11<\/code>. From there I could follow&nbsp;<a href=\"https:\/\/blog.davisvaughan.com\/posts\/2019-04-05-debug-r-package-with-cpp\/\">this<\/a>&nbsp;excellent guide to debug R and C++ code.<\/p>\n\n\n\n<p>I shouldn\u2019t generally leave the&nbsp;<code>-g<\/code>&nbsp;flag on in a Makevars file, that will insert trace symbols in the compiled binary, both increasing compilation times (often by a large margin), and creating larger binaries. Once the package is compiled and I am sure that it works properly, I need to remove the&nbsp;<code>PKG_CPPFLAGS = -UDEBUG -g<\/code>&nbsp;line.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">A&nbsp;more complex example<\/h3>\n\n\n\n<p>I created a package containing a set of simple functions to obtain the Ordinary Least Squares (OLS) estimator by calling a C++ function that calls other C++ functions. My approach was to create one function per step, which meant to create one function to obtain&nbsp;X<sup>t<\/sup>X, another for&nbsp;(X<sup>t<\/sup>X)<sup>\u22121<\/sup>&nbsp;which consisted in implementing the Gauss-Jordan method to invert a matrix, another for&nbsp;X<sup>t<\/sup>Y&nbsp;and then call each of those functions to obtain&nbsp;<img decoding=\"async\" width=\"172\" height=\"31\" class=\"wp-image-191135 lazyload\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2023\/05\/pacha-dev-blog-function.png\" alt=\"\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 172px; aspect-ratio: 172\/31;\">.<\/p>\n\n\n\n<p>This implementation is extremely naive, but it is enough to show how to use C++ code within R. Please see it from my&nbsp;<a href=\"https:\/\/github.com\/pachadotdev\/cpp11dummyols\">GitHub profile<\/a>.<\/p>\n\n\n\n<p>A good challenge would be to implement the QR decomposition used by the&nbsp;<code>lm()<\/code>&nbsp;function in R and use it to obtain the OLS estimator in C++. This would require some effort, but&nbsp;<a href=\"https:\/\/madrury.github.io\/jekyll\/update\/statistics\/2016\/07\/20\/lm-in-R.html\">here<\/a>&nbsp;you can find a good starting point.<\/p>\n\n\n\n<p>In any case, it would be extremely hard to beat the performance of the&nbsp;<code>lm()<\/code>&nbsp;function in R, which has some internals written in C, and how computationally robust&nbsp;<code>lm()<\/code>&nbsp;is means another feature that is hard to beat.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">References<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/mpadge.github.io\/blog\/blog012.html\">Debugging in R with a single command<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/blog.davisvaughan.com\/posts\/2019-04-05-debug-r-package-with-cpp\/\">Debugging an R package with C++<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/gatowololo.github.io\/blog\/clangmissingheaders\/\">Clang++ missing C++ header?<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/stackoverflow.com\/questions\/76300775\/how-to-i-tell-rstudio-not-to-ignore-the-indication-to-use-clang-in-makevars\">How to I tell RStudio not to ignore the indication to use clang in Makevars?<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/stackoverflow.com\/questions\/68959897\/rs-makevars-pkg-cxxflags-vs-pkg-cxx11flags\/68959927#68959927\">R\u2019s Makevars: PKG_CXXFLAGS vs.&nbsp;PKG_CXX11FLAGS<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/reside-ic.github.io\/blog\/debugging-memory-errors-with-valgrind-and-gdb\/\">Debugging memory errors with valgrind and gdb<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/madrury.github.io\/jekyll\/update\/statistics\/2016\/07\/20\/lm-in-R.html\">A Deep Dive Into How R Fits a Linear Model<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>The GEPPML estimator is a computationally intensive estimator that requires to solve a system of non-linear equations, and for this task we might be better-off by using a compiled language such as C++.<\/p>\n","protected":false},"author":1156,"featured_media":191138,"comment_status":"open","ping_status":"closed","sticky":true,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[346,339,343,338,341,342],"tags":[854,15360,15358,15356,487,508,15357,15359,15153],"contributors-categories":[15332],"class_list":{"0":"post-190887","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-c-plusplus-development","8":"category-data-science","9":"category-programing-languages","10":"category-ibkr-quant-news","11":"category-quant-development","12":"category-r-development","13":"tag-cplusplus","14":"tag-cpp11","15":"tag-devtools","16":"tag-general-equilibrium-poisson-pseudo-maximum-likelihood-geppml-estimator","17":"tag-r","18":"tag-rstudio","19":"tag-ubuntu","20":"tag-usethis","21":"tag-visual-studio-code","22":"contributors-categories-pacha-dev"},"pp_statuses_selecting_workflow":false,"pp_workflow_action":"current","pp_status_selection":"publish","acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v26.9 (Yoast SEO v27.8) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>A Step by Step Guide to Write an R package That Uses C++ Code (Ubuntu)<\/title>\n<meta name=\"description\" content=\"The GEPPML estimator is a computationally intensive estimator that requires to solve a system of non-linear equations, and for this task we might be...\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.interactivebrokers.com\/campus\/wp-json\/wp\/v2\/posts\/190887\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"A Step by Step Guide to Write an R package That Uses C++ Code (Ubuntu) | IBKR Campus US\" \/>\n<meta property=\"og:description\" content=\"The GEPPML estimator is a computationally intensive estimator that requires to solve a system of non-linear equations, and for this task we might be better-off by using a compiled language such as C++.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/a-step-by-step-guide-to-write-an-r-package-that-uses-c-code-ubuntu\/\" \/>\n<meta property=\"og:site_name\" content=\"IBKR Campus US\" \/>\n<meta property=\"article:published_time\" content=\"2023-05-31T13:31:42+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/05\/r-programming-keyboard.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1000\" \/>\n\t<meta property=\"og:image:height\" content=\"563\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Mauricio Vargas Sepulveda\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Mauricio Vargas Sepulveda\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\n\t    \"@context\": \"https:\\\/\\\/schema.org\",\n\t    \"@graph\": [\n\t        {\n\t            \"@type\": \"NewsArticle\",\n\t            \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/a-step-by-step-guide-to-write-an-r-package-that-uses-c-code-ubuntu\\\/#article\",\n\t            \"isPartOf\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/a-step-by-step-guide-to-write-an-r-package-that-uses-c-code-ubuntu\\\/\"\n\t            },\n\t            \"author\": {\n\t                \"name\": \"Mauricio Vargas Sepulveda\",\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/person\\\/038e6aa7a101487541d7cbc7fe67bacc\"\n\t            },\n\t            \"headline\": \"A Step by Step Guide to Write an R package That Uses C++ Code (Ubuntu)\",\n\t            \"datePublished\": \"2023-05-31T13:31:42+00:00\",\n\t            \"mainEntityOfPage\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/a-step-by-step-guide-to-write-an-r-package-that-uses-c-code-ubuntu\\\/\"\n\t            },\n\t            \"wordCount\": 1183,\n\t            \"commentCount\": 1,\n\t            \"publisher\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#organization\"\n\t            },\n\t            \"image\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/a-step-by-step-guide-to-write-an-r-package-that-uses-c-code-ubuntu\\\/#primaryimage\"\n\t            },\n\t            \"thumbnailUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2023\\\/05\\\/r-programming-keyboard.jpg\",\n\t            \"keywords\": [\n\t                \"C++\",\n\t                \"cpp11\",\n\t                \"devtools\",\n\t                \"General Equilibrium Poisson Pseudo Maximum Likelihood (GEPPML) estimator\",\n\t                \"R\",\n\t                \"RStudio\",\n\t                \"Ubuntu\",\n\t                \"usethis\",\n\t                \"Visual Studio Code\"\n\t            ],\n\t            \"articleSection\": [\n\t                \"C++ Development\",\n\t                \"Data Science\",\n\t                \"Programming Languages\",\n\t                \"Quant\",\n\t                \"Quant Development\",\n\t                \"R Development\"\n\t            ],\n\t            \"inLanguage\": \"en-US\",\n\t            \"potentialAction\": [\n\t                {\n\t                    \"@type\": \"CommentAction\",\n\t                    \"name\": \"Comment\",\n\t                    \"target\": [\n\t                        \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/a-step-by-step-guide-to-write-an-r-package-that-uses-c-code-ubuntu\\\/#respond\"\n\t                    ]\n\t                }\n\t            ]\n\t        },\n\t        {\n\t            \"@type\": \"WebPage\",\n\t            \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/a-step-by-step-guide-to-write-an-r-package-that-uses-c-code-ubuntu\\\/\",\n\t            \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/a-step-by-step-guide-to-write-an-r-package-that-uses-c-code-ubuntu\\\/\",\n\t            \"name\": \"A Step by Step Guide to Write an R package That Uses C++ Code (Ubuntu) | IBKR Campus US\",\n\t            \"isPartOf\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#website\"\n\t            },\n\t            \"primaryImageOfPage\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/a-step-by-step-guide-to-write-an-r-package-that-uses-c-code-ubuntu\\\/#primaryimage\"\n\t            },\n\t            \"image\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/a-step-by-step-guide-to-write-an-r-package-that-uses-c-code-ubuntu\\\/#primaryimage\"\n\t            },\n\t            \"thumbnailUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2023\\\/05\\\/r-programming-keyboard.jpg\",\n\t            \"datePublished\": \"2023-05-31T13:31:42+00:00\",\n\t            \"description\": \"The GEPPML estimator is a computationally intensive estimator that requires to solve a system of non-linear equations, and for this task we might be better-off by using a compiled language such as C++.\",\n\t            \"inLanguage\": \"en-US\",\n\t            \"potentialAction\": [\n\t                {\n\t                    \"@type\": \"ReadAction\",\n\t                    \"target\": [\n\t                        \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/a-step-by-step-guide-to-write-an-r-package-that-uses-c-code-ubuntu\\\/\"\n\t                    ]\n\t                }\n\t            ]\n\t        },\n\t        {\n\t            \"@type\": \"ImageObject\",\n\t            \"inLanguage\": \"en-US\",\n\t            \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/a-step-by-step-guide-to-write-an-r-package-that-uses-c-code-ubuntu\\\/#primaryimage\",\n\t            \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2023\\\/05\\\/r-programming-keyboard.jpg\",\n\t            \"contentUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2023\\\/05\\\/r-programming-keyboard.jpg\",\n\t            \"width\": 1000,\n\t            \"height\": 563,\n\t            \"caption\": \"R Programming\"\n\t        },\n\t        {\n\t            \"@type\": \"WebSite\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#website\",\n\t            \"url\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/\",\n\t            \"name\": \"IBKR Campus US\",\n\t            \"description\": \"Financial Education from Interactive Brokers\",\n\t            \"publisher\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#organization\"\n\t            },\n\t            \"potentialAction\": [\n\t                {\n\t                    \"@type\": \"SearchAction\",\n\t                    \"target\": {\n\t                        \"@type\": \"EntryPoint\",\n\t                        \"urlTemplate\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/?s={search_term_string}\"\n\t                    },\n\t                    \"query-input\": {\n\t                        \"@type\": \"PropertyValueSpecification\",\n\t                        \"valueRequired\": true,\n\t                        \"valueName\": \"search_term_string\"\n\t                    }\n\t                }\n\t            ],\n\t            \"inLanguage\": \"en-US\"\n\t        },\n\t        {\n\t            \"@type\": \"Organization\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#organization\",\n\t            \"name\": \"Interactive Brokers\",\n\t            \"alternateName\": \"IBKR\",\n\t            \"url\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/\",\n\t            \"logo\": {\n\t                \"@type\": \"ImageObject\",\n\t                \"inLanguage\": \"en-US\",\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/logo\\\/image\\\/\",\n\t                \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2024\\\/05\\\/ibkr-campus-logo.jpg\",\n\t                \"contentUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2024\\\/05\\\/ibkr-campus-logo.jpg\",\n\t                \"width\": 669,\n\t                \"height\": 669,\n\t                \"caption\": \"Interactive Brokers\"\n\t            },\n\t            \"image\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/logo\\\/image\\\/\"\n\t            },\n\t            \"publishingPrinciples\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/about-ibkr-campus\\\/\",\n\t            \"ethicsPolicy\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/cyber-security-notice\\\/\"\n\t        },\n\t        {\n\t            \"@type\": \"Person\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/person\\\/038e6aa7a101487541d7cbc7fe67bacc\",\n\t            \"name\": \"Mauricio Vargas Sepulveda\",\n\t            \"description\": \"Mauricio Vargas Sepulveda provides personalized and group-based R\\\/Shiny training sessions that may be reserved through Buy me a Coffee. Additionally, he offers training services in the Spanish language and is available to discuss means by which he may contribute to your Shiny project.\",\n\t            \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/author\\\/mauricio-vargas-sepulveda\\\/\"\n\t        }\n\t    ]\n\t}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"A Step by Step Guide to Write an R package That Uses C++ Code (Ubuntu)","description":"The GEPPML estimator is a computationally intensive estimator that requires to solve a system of non-linear equations, and for this task we might be...","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.interactivebrokers.com\/campus\/wp-json\/wp\/v2\/posts\/190887\/","og_locale":"en_US","og_type":"article","og_title":"A Step by Step Guide to Write an R package That Uses C++ Code (Ubuntu) | IBKR Campus US","og_description":"The GEPPML estimator is a computationally intensive estimator that requires to solve a system of non-linear equations, and for this task we might be better-off by using a compiled language such as C++.","og_url":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/a-step-by-step-guide-to-write-an-r-package-that-uses-c-code-ubuntu\/","og_site_name":"IBKR Campus US","article_published_time":"2023-05-31T13:31:42+00:00","og_image":[{"width":1000,"height":563,"url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/05\/r-programming-keyboard.jpg","type":"image\/jpeg"}],"author":"Mauricio Vargas Sepulveda","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Mauricio Vargas Sepulveda","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"NewsArticle","@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/a-step-by-step-guide-to-write-an-r-package-that-uses-c-code-ubuntu\/#article","isPartOf":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/a-step-by-step-guide-to-write-an-r-package-that-uses-c-code-ubuntu\/"},"author":{"name":"Mauricio Vargas Sepulveda","@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/person\/038e6aa7a101487541d7cbc7fe67bacc"},"headline":"A Step by Step Guide to Write an R package That Uses C++ Code (Ubuntu)","datePublished":"2023-05-31T13:31:42+00:00","mainEntityOfPage":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/a-step-by-step-guide-to-write-an-r-package-that-uses-c-code-ubuntu\/"},"wordCount":1183,"commentCount":1,"publisher":{"@id":"https:\/\/ibkrcampus.com\/campus\/#organization"},"image":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/a-step-by-step-guide-to-write-an-r-package-that-uses-c-code-ubuntu\/#primaryimage"},"thumbnailUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/05\/r-programming-keyboard.jpg","keywords":["C++","cpp11","devtools","General Equilibrium Poisson Pseudo Maximum Likelihood (GEPPML) estimator","R","RStudio","Ubuntu","usethis","Visual Studio Code"],"articleSection":["C++ Development","Data Science","Programming Languages","Quant","Quant Development","R Development"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/a-step-by-step-guide-to-write-an-r-package-that-uses-c-code-ubuntu\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/a-step-by-step-guide-to-write-an-r-package-that-uses-c-code-ubuntu\/","url":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/a-step-by-step-guide-to-write-an-r-package-that-uses-c-code-ubuntu\/","name":"A Step by Step Guide to Write an R package That Uses C++ Code (Ubuntu) | IBKR Campus US","isPartOf":{"@id":"https:\/\/ibkrcampus.com\/campus\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/a-step-by-step-guide-to-write-an-r-package-that-uses-c-code-ubuntu\/#primaryimage"},"image":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/a-step-by-step-guide-to-write-an-r-package-that-uses-c-code-ubuntu\/#primaryimage"},"thumbnailUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/05\/r-programming-keyboard.jpg","datePublished":"2023-05-31T13:31:42+00:00","description":"The GEPPML estimator is a computationally intensive estimator that requires to solve a system of non-linear equations, and for this task we might be better-off by using a compiled language such as C++.","inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/a-step-by-step-guide-to-write-an-r-package-that-uses-c-code-ubuntu\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/a-step-by-step-guide-to-write-an-r-package-that-uses-c-code-ubuntu\/#primaryimage","url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/05\/r-programming-keyboard.jpg","contentUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/05\/r-programming-keyboard.jpg","width":1000,"height":563,"caption":"R Programming"},{"@type":"WebSite","@id":"https:\/\/ibkrcampus.com\/campus\/#website","url":"https:\/\/ibkrcampus.com\/campus\/","name":"IBKR Campus US","description":"Financial Education from Interactive Brokers","publisher":{"@id":"https:\/\/ibkrcampus.com\/campus\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/ibkrcampus.com\/campus\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/ibkrcampus.com\/campus\/#organization","name":"Interactive Brokers","alternateName":"IBKR","url":"https:\/\/ibkrcampus.com\/campus\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/logo\/image\/","url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/05\/ibkr-campus-logo.jpg","contentUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/05\/ibkr-campus-logo.jpg","width":669,"height":669,"caption":"Interactive Brokers"},"image":{"@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/logo\/image\/"},"publishingPrinciples":"https:\/\/www.interactivebrokers.com\/campus\/about-ibkr-campus\/","ethicsPolicy":"https:\/\/www.interactivebrokers.com\/campus\/cyber-security-notice\/"},{"@type":"Person","@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/person\/038e6aa7a101487541d7cbc7fe67bacc","name":"Mauricio Vargas Sepulveda","description":"Mauricio Vargas Sepulveda provides personalized and group-based R\/Shiny training sessions that may be reserved through Buy me a Coffee. Additionally, he offers training services in the Spanish language and is available to discuss means by which he may contribute to your Shiny project.","url":"https:\/\/www.interactivebrokers.com\/campus\/author\/mauricio-vargas-sepulveda\/"}]}},"jetpack_featured_media_url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/05\/r-programming-keyboard.jpg","_links":{"self":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/posts\/190887","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/users\/1156"}],"replies":[{"embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/comments?post=190887"}],"version-history":[{"count":0,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/posts\/190887\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/media\/191138"}],"wp:attachment":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/media?parent=190887"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/categories?post=190887"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/tags?post=190887"},{"taxonomy":"contributors-categories","embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/contributors-categories?post=190887"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}