{"id":253,"date":"2020-03-29T12:38:00","date_gmt":"2020-03-29T12:38:00","guid":{"rendered":"http:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/hamish-thorburn\/?p=253"},"modified":"2021-11-08T10:30:02","modified_gmt":"2021-11-08T10:30:02","slug":"tutorial-building-packages-in-r","status":"publish","type":"post","link":"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/hamish-thorburn\/2020\/03\/29\/tutorial-building-packages-in-r\/","title":{"rendered":"Tutorial &#8211; Building packages in R"},"content":{"rendered":"\n<p>Slight change of pace in this post &#8211; today, we&#8217;re going to look at how to build a package in R (which can incorporate C++ code as well). This is based on a lesson we had with <a href=\"https:\/\/www.lancaster.ac.uk\/maths\/people\/daniel-grose\">Dan Grose<\/a>, the new software engineer at STOR-i.<\/p>\n\n\n\n<p>For the tutorial today, we assume that everyone is reasonably comfortable using R, and we&#8217;ll using a Linux operating system.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">An overview of R<\/h2>\n\n\n\n<p>If you have used R before, you would know it is a statistical software package and programming language. It is open source (i.e. free) and comes with a variety of built-in statistical functions, such as t-tests, power calculations and plotting functions.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"554\" src=\"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/hamish-thorburn\/wp-content\/uploads\/sites\/8\/2020\/04\/image-1-1024x554.png\" alt=\"\" class=\"wp-image-257\" srcset=\"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/hamish-thorburn\/wp-content\/uploads\/sites\/8\/2020\/04\/image-1-1024x554.png 1024w, https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/hamish-thorburn\/wp-content\/uploads\/sites\/8\/2020\/04\/image-1-300x162.png 300w, https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/hamish-thorburn\/wp-content\/uploads\/sites\/8\/2020\/04\/image-1-768x416.png 768w, https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/hamish-thorburn\/wp-content\/uploads\/sites\/8\/2020\/04\/image-1-1536x831.png 1536w, https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/hamish-thorburn\/wp-content\/uploads\/sites\/8\/2020\/04\/image-1-1600x866.png 1600w, https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/hamish-thorburn\/wp-content\/uploads\/sites\/8\/2020\/04\/image-1.png 1920w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Which have been responsible for many of the graphics in my blog posts<\/figcaption><\/figure>\n\n\n\n<p> <\/p>\n\n\n\n<p>While this is amazing (especially when you&#8217;ve previously been doing your t-tests by hand), one of the great aspects of R is that many statisticians around the world will use it to develop functions for more complicated methods (including <a href=\"http:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/hamish-thorburn\/2020\/02\/23\/model-based-clustering\/\">clustering<\/a> and <a href=\"http:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/hamish-thorburn\/2020\/01\/24\/factor-analysis-and-interpretability\/\">factor analysis<\/a>), and release these for public use, in the form of <strong>packages<\/strong>. Users then can download and install these packages, and then freely use the functions they contain.<\/p>\n\n\n\n<p>While packages are great for loading other functions written in R, they have another wonderful aspect as well. One of the drawbacks of R is that it can be much slower than other programming languages. However, when building a package in R, you can actually incorporate code written in C++ (the language R is built from), which is MUCH quicker.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 1 &#8211; building the package skeleton<\/h2>\n\n\n\n<p>The first step is to build the &#8220;package skeleton&#8221;. <\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/i.pinimg.com\/originals\/8b\/29\/22\/8b2922946c209eb1554700eec5100c67.jpg\" alt=\"DIY Halloween Skeleton Craft for Kids | Skeleton craft, Halloween ...\" \/><figcaption>No, it is not a skeleton made out of packages<\/figcaption><\/figure>\n\n\n\n<p>This is simply a folder on your computer in which we&#8217;re going to put all our &#8220;.r&#8221; and &#8220;.cpp&#8221; files with the functions we want to include. To do this is really simple, if we use the &#8220;Rcpp&#8221; package in R. This package gives us a command which simply builds the skeleton for us. <\/p>\n\n\n\n<p>To build the skeleton, we simply run the following command in R after loading the Rcpp package:<\/p>\n\n\n\n<p><strong>package.skeleton(&#8220;NameOfPackage&#8221;,code_files=&#8221;ListOfRFiles&#8221;,cpp_files=&#8221;ListOfC++Files&#8221;)<\/strong><\/p>\n\n\n\n<p>This creates the package skeleton (called &#8220;NameOfPackage&#8221;) in the working directory. In the skeleton, there are three folders:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><strong>man<\/strong> &#8211; unimportant for this tutorial<\/li><li><strong>R<\/strong> &#8211; a folder where we put all our &#8220;.R&#8221; files, containing the functions we want in the package, written in R<\/li><li><strong>src<\/strong> &#8211; a folder where we put all our &#8220;.cpp&#8221; files, containing the functions we want in the package, written in C++<\/li><\/ol>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"900\" height=\"590\" src=\"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/hamish-thorburn\/wp-content\/uploads\/sites\/8\/2020\/04\/image-3.png\" alt=\"\" class=\"wp-image-262\" srcset=\"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/hamish-thorburn\/wp-content\/uploads\/sites\/8\/2020\/04\/image-3.png 900w, https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/hamish-thorburn\/wp-content\/uploads\/sites\/8\/2020\/04\/image-3-300x197.png 300w, https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/hamish-thorburn\/wp-content\/uploads\/sites\/8\/2020\/04\/image-3-768x503.png 768w\" sizes=\"auto, (max-width: 900px) 100vw, 900px\" \/><figcaption>A package skeleton of a package named &#8220;TestPackage1&#8221;<\/figcaption><\/figure>\n\n\n\n<p>While we can manually put the files in these folders, the <strong>package.skeleton<\/strong> function will have automatically placed the files in named in the <strong>code_files<\/strong> and <strong>cpp_files<\/strong> arguments in the <strong>R<\/strong> and <strong>src<\/strong> respectively.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Step 2 &#8211; building the package<\/strong><\/h2>\n\n\n\n<p>This step is much simpler. Once we have the package skeleton, we simply run the following command from the terminal:<\/p>\n\n\n\n<p><strong>R CMD build PackageDirectory\/PackageSkeletonName<\/strong><\/p>\n\n\n\n<p>substituting the package directory and name in where appropriate.<\/p>\n\n\n\n<p>This builds the package <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<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"615\" height=\"62\" src=\"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/hamish-thorburn\/wp-content\/uploads\/sites\/8\/2020\/04\/image-4.png\" alt=\"\" class=\"wp-image-263\" srcset=\"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/hamish-thorburn\/wp-content\/uploads\/sites\/8\/2020\/04\/image-4.png 615w, https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/hamish-thorburn\/wp-content\/uploads\/sites\/8\/2020\/04\/image-4-300x30.png 300w\" sizes=\"auto, (max-width: 615px) 100vw, 615px\" \/><figcaption>The package tarball<\/figcaption><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Step 3 &#8211; installing the package<\/h2>\n\n\n\n<p>This step is even easier. Just run the following command from the terminal<\/p>\n\n\n\n<p><strong>R CMD INSTALL PackageDirectory\/PackageTarBallName<\/strong><\/p>\n\n\n\n<p>Once this is done, your package can be loaded as a library in R, and is ready to use!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example &#8211; Convex hulls using the Jarvis March Algorithm<\/h2>\n\n\n\n<p>As part of our lesson, we created an R package containing functions to find the convex hull of a set of points, using the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Gift_wrapping_algorithm\">Jarvis March algorithm<\/a>. In this package, we wrote a function called &#8220;ConvexHull&#8221; to find the convex hull of the points in C++, and then wrote a function to take a convex hull (the output of our function &#8220;ConvexHull&#8221;) and the set of points and plot these, which we wrote in R.<\/p>\n\n\n\n<p>All files used to create this package, and the created package tarball can be found <a href=\"https:\/\/drive.google.com\/open?id=1rZX4ec1yNyUyhSBW11FYoet9z0iM3Qk0\">here<\/a>. In that folder, you can find the tar ball for the package, which can be installed using the above instructions. If you would like to make the package from scratch yourself, you can do so using the provided  file &#8220;R_Package_Building_Code.r&#8221;. This which provides code to build the package skeleton,<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1008\" height=\"208\" src=\"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/hamish-thorburn\/wp-content\/uploads\/sites\/8\/2020\/04\/image-5.png\" alt=\"\" class=\"wp-image-267\" srcset=\"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/hamish-thorburn\/wp-content\/uploads\/sites\/8\/2020\/04\/image-5.png 1008w, https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/hamish-thorburn\/wp-content\/uploads\/sites\/8\/2020\/04\/image-5-300x62.png 300w, https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/hamish-thorburn\/wp-content\/uploads\/sites\/8\/2020\/04\/image-5-768x158.png 768w\" sizes=\"auto, (max-width: 1008px) 100vw, 1008px\" \/><\/figure>\n\n\n\n<p>load the package,<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"331\" height=\"38\" src=\"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/hamish-thorburn\/wp-content\/uploads\/sites\/8\/2020\/04\/image-6.png\" alt=\"\" class=\"wp-image-268\" srcset=\"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/hamish-thorburn\/wp-content\/uploads\/sites\/8\/2020\/04\/image-6.png 331w, https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/hamish-thorburn\/wp-content\/uploads\/sites\/8\/2020\/04\/image-6-300x34.png 300w\" sizes=\"auto, (max-width: 331px) 100vw, 331px\" \/><\/figure>\n\n\n\n<p>generate the points, and find the hull (using a C++ function in the &#8220;Hamish_CPP_HM_Functions.cpp&#8221; file, in the &#8220;JM_Package_CCode\/CPP_Code&#8221; directory),<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"494\" height=\"147\" src=\"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/hamish-thorburn\/wp-content\/uploads\/sites\/8\/2020\/04\/image-7.png\" alt=\"\" class=\"wp-image-269\" srcset=\"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/hamish-thorburn\/wp-content\/uploads\/sites\/8\/2020\/04\/image-7.png 494w, https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/hamish-thorburn\/wp-content\/uploads\/sites\/8\/2020\/04\/image-7-300x89.png 300w\" sizes=\"auto, (max-width: 494px) 100vw, 494px\" \/><\/figure>\n\n\n\n<p>and plot this function using a function written in R.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"232\" height=\"19\" src=\"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/hamish-thorburn\/wp-content\/uploads\/sites\/8\/2020\/04\/image-8.png\" alt=\"\" class=\"wp-image-270\" \/><\/figure>\n\n\n\n<p>which gives the following result:<\/p>\n\n\n\n<figure class=\"wp-block-gallery columns-2 is-cropped wp-block-gallery-1 is-layout-flex wp-block-gallery-is-layout-flex\"><ul class=\"blocks-gallery-grid\"><li class=\"blocks-gallery-item\"><figure><img loading=\"lazy\" decoding=\"async\" width=\"736\" height=\"657\" src=\"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/hamish-thorburn\/wp-content\/uploads\/sites\/8\/2020\/04\/Points.png\" alt=\"\" data-id=\"272\" data-full-url=\"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/hamish-thorburn\/wp-content\/uploads\/sites\/8\/2020\/04\/Points.png\" data-link=\"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/hamish-thorburn\/?attachment_id=272\" class=\"wp-image-272\" srcset=\"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/hamish-thorburn\/wp-content\/uploads\/sites\/8\/2020\/04\/Points.png 736w, https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/hamish-thorburn\/wp-content\/uploads\/sites\/8\/2020\/04\/Points-300x268.png 300w\" sizes=\"auto, (max-width: 736px) 100vw, 736px\" \/><\/figure><\/li><li class=\"blocks-gallery-item\"><figure><img loading=\"lazy\" decoding=\"async\" width=\"736\" height=\"657\" src=\"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/hamish-thorburn\/wp-content\/uploads\/sites\/8\/2020\/04\/Hull.png\" alt=\"\" data-id=\"271\" data-full-url=\"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/hamish-thorburn\/wp-content\/uploads\/sites\/8\/2020\/04\/Hull.png\" data-link=\"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/hamish-thorburn\/?attachment_id=271\" class=\"wp-image-271\" srcset=\"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/hamish-thorburn\/wp-content\/uploads\/sites\/8\/2020\/04\/Hull.png 736w, https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/hamish-thorburn\/wp-content\/uploads\/sites\/8\/2020\/04\/Hull-300x268.png 300w\" sizes=\"auto, (max-width: 736px) 100vw, 736px\" \/><\/figure><\/li><\/ul><figcaption class=\"blocks-gallery-caption\">The set of points (left) and the points with the hull (right)<\/figcaption><\/figure>\n\n\n\n<p>And that&#8217;s it! While there are some more tricks you can do (like adding <a href=\"https:\/\/en.wikipedia.org\/wiki\/Interrupt\">user-interrupts<\/a> to the code), this should be enough to get you started with building packages of your own.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Slight change of pace in this post &#8211; today, we&#8217;re going to look at how to build a package in&hellip;<\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-253","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/hamish-thorburn\/wp-json\/wp\/v2\/posts\/253","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/hamish-thorburn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/hamish-thorburn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/hamish-thorburn\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/hamish-thorburn\/wp-json\/wp\/v2\/comments?post=253"}],"version-history":[{"count":12,"href":"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/hamish-thorburn\/wp-json\/wp\/v2\/posts\/253\/revisions"}],"predecessor-version":[{"id":307,"href":"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/hamish-thorburn\/wp-json\/wp\/v2\/posts\/253\/revisions\/307"}],"wp:attachment":[{"href":"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/hamish-thorburn\/wp-json\/wp\/v2\/media?parent=253"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/hamish-thorburn\/wp-json\/wp\/v2\/categories?post=253"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/hamish-thorburn\/wp-json\/wp\/v2\/tags?post=253"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}