{"id":203,"date":"2021-03-13T18:10:19","date_gmt":"2021-03-13T18:10:19","guid":{"rendered":"http:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/ziyang-yang\/?p=203"},"modified":"2021-04-30T12:58:26","modified_gmt":"2021-04-30T12:58:26","slug":"3-steps-to-build-own-r-package-rcpp","status":"publish","type":"post","link":"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/ziyang-yang\/2021\/03\/13\/3-steps-to-build-own-r-package-rcpp\/","title":{"rendered":"3 steps to build own R package &#8211; Rcpp"},"content":{"rendered":"\n<p><span style=\"color:#0a6b29\" class=\"has-inline-color\">This blog is to give ideas how to build R package through Rcpp and C++. Here we assume our readers are confident of C++, Linux and R.<\/span><\/p>\n\n\n\n<p>This semester we have been trained to use C++ and Rcpp to write the R package. It is well known that the computing speed of R is slower than C++. Rcpp is an R Package that combines C++ and R. With Rcpp, it could easily transfer the algorithm or functions between R and C++, providing high-performance statistical computing to most R users. It is useful when statisticians want to develop their own R package. So, I will write it in 3 steps and using an Example.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background is-style-wide\" style=\"background-color:#00a30a;color:#00a30a\" \/>\n\n\n\n<h1 class=\"wp-block-heading\">Step 1: Write your own algorithm in C++<\/h1>\n\n\n\n<p>Firstly, you have to write your own algorithm in C++ in a Linux system. And next, we have to add some code in C++ to make sure it could be translated by R:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Add #include&lt;Rcpp.h&gt; at the beginning<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/ziyang-yang\/wp-content\/uploads\/sites\/18\/2021\/03\/image.png\" alt=\"\" class=\"wp-image-204\" width=\"371\" height=\"175\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\"><li>Add \/\/[[Rcpp::export]] in your main function<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/ziyang-yang\/wp-content\/uploads\/sites\/18\/2021\/03\/image-1.png\" alt=\"\" class=\"wp-image-205\" width=\"670\" height=\"102\" srcset=\"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/ziyang-yang\/wp-content\/uploads\/sites\/18\/2021\/03\/image-1.png 602w, https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/ziyang-yang\/wp-content\/uploads\/sites\/18\/2021\/03\/image-1-300x46.png 300w\" sizes=\"auto, (max-width: 670px) 100vw, 670px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\"><li>Add user interrupt through <span class=\"has-inline-color has-quaternary-color\">Rcpp::checkUserInterrupt()<\/span>. It allows users to terminal algorithm when it runs too long.<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/ziyang-yang\/wp-content\/uploads\/sites\/18\/2021\/03\/image-7.png\" alt=\"\" class=\"wp-image-211\" width=\"681\" height=\"156\" srcset=\"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/ziyang-yang\/wp-content\/uploads\/sites\/18\/2021\/03\/image-7.png 602w, https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/ziyang-yang\/wp-content\/uploads\/sites\/18\/2021\/03\/image-7-300x69.png 300w\" sizes=\"auto, (max-width: 681px) 100vw, 681px\" \/><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background is-style-wide\" style=\"background-color:#00a30a;color:#00a30a\" \/>\n\n\n\n<h1 class=\"wp-block-heading\">Step 2: Build package <\/h1>\n\n\n\n<p>After obtaining our &#8216;cpp&#8217; algorithm, we have to package it as a &#8216;zip&#8217; file so it could be easily downloaded by anyone who wants to implement it in R. To do so, we have two simple steps:<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. Building Skeleton Folder<\/h2>\n\n\n\n<p>Skeleton folder just like the skeleton, containing all the main programs here. It is very simple to create: in R, run the code: <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package.skeleton(\u201cThe name of package\u201d, cpp_files=\u201dpath to your c++ file\u201d, example_code=FALSE) <\/code><\/pre>\n\n\n\n<p>In my example, I  created a package called &#8216;finaljarvismarch&#8217;, and write the path to my cpp file in &#8216;cpp_files&#8217;. If example_code=TRUE, the package will contain an example code.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"584\" height=\"225\" src=\"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/ziyang-yang\/wp-content\/uploads\/sites\/18\/2021\/03\/image-3.png\" alt=\"\" class=\"wp-image-207\" srcset=\"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/ziyang-yang\/wp-content\/uploads\/sites\/18\/2021\/03\/image-3.png 584w, https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/ziyang-yang\/wp-content\/uploads\/sites\/18\/2021\/03\/image-3-300x116.png 300w\" sizes=\"auto, (max-width: 584px) 100vw, 584px\" \/><\/figure>\n\n\n\n<p>This creates the package skeleton in the working directory. It contains three files and three folders:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Man: It contains the Rd file, which is the description shows in R.<\/li><li>R: It contains all &#8220;.R&#8221; files written by R.<\/li><li>Src: It contains all &#8220;.cpp&#8221; files written by C++.<\/li><\/ul>\n\n\n\n<p>We could manually put our further R function or C++ function in different folders.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"489\" height=\"130\" src=\"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/ziyang-yang\/wp-content\/uploads\/sites\/18\/2021\/03\/image-4.png\" alt=\"\" class=\"wp-image-208\" srcset=\"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/ziyang-yang\/wp-content\/uploads\/sites\/18\/2021\/03\/image-4.png 489w, https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/ziyang-yang\/wp-content\/uploads\/sites\/18\/2021\/03\/image-4-300x80.png 300w\" sizes=\"auto, (max-width: 489px) 100vw, 489px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">2. Building Package<\/h2>\n\n\n\n<p>Once we got the skeleton folder, run the command in terminal to create the package: <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>R CMD build PackageDirectory\/PackageSkeletonName <\/code><\/pre>\n\n\n\n<p>This builds the package&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Tar_(computing)\">tarball<\/a>, which can then be sent to and installed on any machine running R.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/ziyang-yang\/wp-content\/uploads\/sites\/18\/2021\/03\/image-5.png\" alt=\"\" class=\"wp-image-209\" width=\"635\" height=\"175\" srcset=\"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/ziyang-yang\/wp-content\/uploads\/sites\/18\/2021\/03\/image-5.png 541w, https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/ziyang-yang\/wp-content\/uploads\/sites\/18\/2021\/03\/image-5-300x83.png 300w\" sizes=\"auto, (max-width: 635px) 100vw, 635px\" \/><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background is-style-wide\" style=\"background-color:#00a30a;color:#00a30a\" \/>\n\n\n\n<h1 class=\"wp-block-heading\">Step 3: Checking instalment<\/h1>\n\n\n\n<p>Until now, we have successfully create a package. However, we have to test if it could be install appropriately. <\/p>\n\n\n\n<p>Run the command in the terminal in the directory:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> R CMD INSTALL PackageTarBallName<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/ziyang-yang\/wp-content\/uploads\/sites\/18\/2021\/03\/image-6.png\" alt=\"\" class=\"wp-image-210\" width=\"654\" height=\"301\" srcset=\"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/ziyang-yang\/wp-content\/uploads\/sites\/18\/2021\/03\/image-6.png 577w, https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/ziyang-yang\/wp-content\/uploads\/sites\/18\/2021\/03\/image-6-300x138.png 300w\" sizes=\"auto, (max-width: 654px) 100vw, 654px\" \/><\/figure>\n\n\n\n<p>Luckily without any error! Now, our package could be downloaded as a tarball by any user, and successfully install in R. To use package, directly run: <span class=\"has-inline-color has-quaternary-color\">library(&#8216;PackageTarBallName&#8217;)<\/span><\/p>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background is-style-wide\" style=\"background-color:#00a30a;color:#00a30a\" \/>\n\n\n\n<h1 class=\"wp-block-heading\">Example: Jarvis March algorithm<\/h1>\n\n\n\n<p>We are asked to build a<a href=\"https:\/\/en.wikipedia.org\/wiki\/Gift_wrapping_algorithm\"> Jarvis March<\/a> package, you could download the <a rel=\"noreferrer noopener\" href=\"https:\/\/livelancsac-my.sharepoint.com\/:u:\/g\/personal\/yangz40_lancaster_ac_uk\/EZaiLkjQfzVBvixEsiKZHc0Bjj9adHBO261iv7gYzH-NFg\" target=\"_blank\">tar file<\/a> here. After download it, it could be installed easily<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Run the command in the terminal: <span class=\"has-inline-color has-quaternary-color\">R CMD INSTALL finaljarvismarch<\/span><\/li><li>Run the code in R: <span class=\"has-inline-color has-quaternary-color\">library(finaljarvismarch)<\/span><\/li><\/ul>\n\n\n\n<p>Now you could use Jarvis march algorithm for 2 dimension data. In this package, it contains two functions: <\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>findpoint_jarvis(x,y): inputting the x and y, it outputs the points in the convex hull.<\/li><li>plot_jarvis(x,y): inputting x and y, it returns a plot containing all the data points and convex hull.<\/li><\/ul>\n\n\n\n<p>For example, simulating 100 points, and run the functions:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/ziyang-yang\/wp-content\/uploads\/sites\/18\/2021\/03\/image-9.png\" alt=\"\" class=\"wp-image-215\" width=\"586\" height=\"187\" srcset=\"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/ziyang-yang\/wp-content\/uploads\/sites\/18\/2021\/03\/image-9.png 586w, https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/ziyang-yang\/wp-content\/uploads\/sites\/18\/2021\/03\/image-9-300x96.png 300w\" sizes=\"auto, (max-width: 586px) 100vw, 586px\" \/><\/figure>\n\n\n\n<p>x and y is the corresponded coordinates of points on the convex hull, and we could also draw the plot:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"522\" height=\"532\" src=\"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/ziyang-yang\/wp-content\/uploads\/sites\/18\/2021\/03\/image-10.png\" alt=\"\" class=\"wp-image-216\" srcset=\"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/ziyang-yang\/wp-content\/uploads\/sites\/18\/2021\/03\/image-10.png 522w, https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/ziyang-yang\/wp-content\/uploads\/sites\/18\/2021\/03\/image-10-294x300.png 294w\" sizes=\"auto, (max-width: 522px) 100vw, 522px\" \/><\/figure>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This blog is to give ideas how to build R package through Rcpp and C++. Here we assume our readers&hellip;<\/p>\n","protected":false},"author":25,"featured_media":216,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4],"tags":[5,7,6],"class_list":["post-203","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blogs","tag-jarvis-march","tag-r-package","tag-rcpp"],"_links":{"self":[{"href":"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/ziyang-yang\/wp-json\/wp\/v2\/posts\/203","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/ziyang-yang\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/ziyang-yang\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/ziyang-yang\/wp-json\/wp\/v2\/users\/25"}],"replies":[{"embeddable":true,"href":"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/ziyang-yang\/wp-json\/wp\/v2\/comments?post=203"}],"version-history":[{"count":9,"href":"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/ziyang-yang\/wp-json\/wp\/v2\/posts\/203\/revisions"}],"predecessor-version":[{"id":384,"href":"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/ziyang-yang\/wp-json\/wp\/v2\/posts\/203\/revisions\/384"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/ziyang-yang\/wp-json\/wp\/v2\/media\/216"}],"wp:attachment":[{"href":"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/ziyang-yang\/wp-json\/wp\/v2\/media?parent=203"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/ziyang-yang\/wp-json\/wp\/v2\/categories?post=203"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/ziyang-yang\/wp-json\/wp\/v2\/tags?post=203"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}