{"id":305,"date":"2021-03-07T14:55:23","date_gmt":"2021-03-07T14:55:23","guid":{"rendered":"http:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/rebecca-hamm\/?p=305"},"modified":"2021-04-29T08:42:17","modified_gmt":"2021-04-29T08:42:17","slug":"this-week-on-the-stor-i-programme-rcpp","status":"publish","type":"post","link":"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/rebecca-hamm\/2021\/03\/07\/this-week-on-the-stor-i-programme-rcpp\/","title":{"rendered":"This Week on the STOR-i Programme: Rcpp"},"content":{"rendered":"\n<p>This week I thought I would write a slightly different blog post. Instead of discussing an area of Statistics or Operational Research I have recently learnt about, I will be discussing a useful tool for Statistics or Operational Research I have recently learnt about. This tool is an R package, Rcpp. <\/p>\n\n\n\n<p><strong>What does Rcpp do?<\/strong> <\/p>\n\n\n\n<p>Rcpp is a package which allows us to connect R and C++ code.  In particular I will discuss using it to develop functions in C++ which can then be used in R.<\/p>\n\n\n\n<p><strong>Why do we want to do this?<\/strong><\/p>\n\n\n\n<p>If you have ever used C++  and are like me you will think that it is quite painful to code in. Writing in C++ can be a lot more time consuming than writing in R. R is also full of lots of packages and functions which can make whatever maths you are doing a lot nicer. You may be wondering if R is easier to write in why are we developing our functions in C++. The answer is relatively simple: speed. R tends to be used in an interpreter whereas C++ is compiled. Interpreters run each line of code at a time whereas a complier processes all the code at once. If you are doing a large task in R interpreting one line at a time may take a while. Creating a function in C++ that is compiled then able to use in R can speed up the program by more than 100 times faster. <\/p>\n\n\n\n<p><strong>How do we do this?<\/strong><\/p>\n\n\n\n<p>There may be different things to consider depending on what you are doing with your function. But for now lets just talk about the basics. There are two ways to go about this, one is to source your C++ file and another is to create an R package of your C++. Either way we need to add a few lines to our C++ code.<\/p>\n\n\n\n<div class=\"wp-block-media-text alignwide has-media-on-the-right is-stacked-on-mobile\"><figure class=\"wp-block-media-text__media\"><img loading=\"lazy\" decoding=\"async\" width=\"193\" height=\"37\" src=\"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/rebecca-hamm\/wp-content\/uploads\/sites\/29\/2021\/03\/Rcpp1.png\" alt=\"\" class=\"wp-image-307 size-full\" \/><\/figure><div class=\"wp-block-media-text__content\">\n<p class=\"has-normal-font-size\">At the begin of your C++ code include:<\/p>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-media-text alignwide has-media-on-the-right is-stacked-on-mobile\"><figure class=\"wp-block-media-text__media\"><img loading=\"lazy\" decoding=\"async\" width=\"163\" height=\"21\" src=\"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/rebecca-hamm\/wp-content\/uploads\/sites\/29\/2021\/03\/Rcpp2.png\" alt=\"\" class=\"wp-image-309 size-full\" \/><\/figure><div class=\"wp-block-media-text__content\">\n<p class=\"has-normal-font-size\">The next line of code should be added just before the function that you wish to use. This is so Rcpp knows what function to include.<\/p>\n<\/div><\/div>\n\n\n\n<p><strong>Sourcing your C++<\/strong><\/p>\n\n\n\n<div class=\"wp-block-media-text alignwide is-stacked-on-mobile\"><figure class=\"wp-block-media-text__media\"><img loading=\"lazy\" decoding=\"async\" width=\"252\" height=\"43\" src=\"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/rebecca-hamm\/wp-content\/uploads\/sites\/29\/2021\/03\/Rcpp3.png\" alt=\"\" class=\"wp-image-310 size-full\" \/><\/figure><div class=\"wp-block-media-text__content\">\n<p class=\"has-normal-font-size\">Sourcing C++ in R is simple. Just open R in the directory you have saved your C++ file in. Install and library the Rcpp package, then source your file.<\/p>\n<\/div><\/div>\n\n\n\n<p>One issue is you have to repeat this every time you want to use it so if it&#8217;s a function you will be using multiple times it may be easier to make a package. This way you can also send it to other people to use a lot easier.<\/p>\n\n\n\n<p><strong>Making a Package<\/strong><\/p>\n\n\n\n<div class=\"wp-block-media-text alignwide has-media-on-the-right is-stacked-on-mobile\"><figure class=\"wp-block-media-text__media\"><img loading=\"lazy\" decoding=\"async\" width=\"782\" height=\"328\" src=\"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/rebecca-hamm\/wp-content\/uploads\/sites\/29\/2021\/03\/Rcpp4.png\" alt=\"\" class=\"wp-image-311 size-full\" srcset=\"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/rebecca-hamm\/wp-content\/uploads\/sites\/29\/2021\/03\/Rcpp4.png 782w, https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/rebecca-hamm\/wp-content\/uploads\/sites\/29\/2021\/03\/Rcpp4-300x126.png 300w, https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/rebecca-hamm\/wp-content\/uploads\/sites\/29\/2021\/03\/Rcpp4-768x322.png 768w\" sizes=\"auto, (max-width: 782px) 100vw, 782px\" \/><\/figure><div class=\"wp-block-media-text__content\">\n<p class=\"has-normal-font-size\">First, in R you need to library Rcpp and build the skeleton of your package. <\/p>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-media-text alignwide has-media-on-the-right is-stacked-on-mobile\"><figure class=\"wp-block-media-text__media\"><img loading=\"lazy\" decoding=\"async\" width=\"793\" height=\"685\" src=\"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/rebecca-hamm\/wp-content\/uploads\/sites\/29\/2021\/03\/Rcpp5.png\" alt=\"\" class=\"wp-image-312 size-full\" srcset=\"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/rebecca-hamm\/wp-content\/uploads\/sites\/29\/2021\/03\/Rcpp5.png 793w, https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/rebecca-hamm\/wp-content\/uploads\/sites\/29\/2021\/03\/Rcpp5-300x259.png 300w, https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/rebecca-hamm\/wp-content\/uploads\/sites\/29\/2021\/03\/Rcpp5-768x663.png 768w\" sizes=\"auto, (max-width: 793px) 100vw, 793px\" \/><\/figure><div class=\"wp-block-media-text__content\">\n<p class=\"has-normal-font-size\">Then in your terminal build and install the package.<\/p>\n<\/div><\/div>\n\n\n\n<p>You then have a package you can use in R.<\/p>\n\n\n\n<p><strong>Things to think about<\/strong><\/p>\n\n\n\n<p>Rcpp is good at converting common data structures. However, if you have defined your own data structures then you will bump into some issues. One way to remedy this is to have an input and output of your function be a common data type and convert to your defined data structure within your function.<\/p>\n\n\n\n<p><strong>Adding  to your package<\/strong><\/p>\n\n\n\n<p>You may find that you want to edit or add to your package which you can do with the following simple steps:<\/p>\n\n\n\n<div class=\"wp-block-media-text alignwide is-stacked-on-mobile\"><figure class=\"wp-block-media-text__media\"><img loading=\"lazy\" decoding=\"async\" width=\"682\" height=\"312\" src=\"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/rebecca-hamm\/wp-content\/uploads\/sites\/29\/2021\/03\/Rcpp6.png\" alt=\"\" class=\"wp-image-313 size-full\" srcset=\"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/rebecca-hamm\/wp-content\/uploads\/sites\/29\/2021\/03\/Rcpp6.png 682w, https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/rebecca-hamm\/wp-content\/uploads\/sites\/29\/2021\/03\/Rcpp6-300x137.png 300w\" sizes=\"auto, (max-width: 682px) 100vw, 682px\" \/><\/figure><div class=\"wp-block-media-text__content\">\n<p class=\"has-normal-font-size\">Edit the file in your package or add a new file to your package folder.<\/p>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-media-text alignwide has-media-on-the-right is-stacked-on-mobile\"><figure class=\"wp-block-media-text__media\"><img loading=\"lazy\" decoding=\"async\" width=\"198\" height=\"43\" src=\"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/rebecca-hamm\/wp-content\/uploads\/sites\/29\/2021\/03\/Rcpp7.png\" alt=\"\" class=\"wp-image-315 size-full\" \/><\/figure><div class=\"wp-block-media-text__content\">\n<p class=\"has-normal-font-size\">Open R with your package as the directory library Rcpp and run the following line:<\/p>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-media-text alignwide is-stacked-on-mobile\"><figure class=\"wp-block-media-text__media\"><img loading=\"lazy\" decoding=\"async\" width=\"712\" height=\"202\" src=\"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/rebecca-hamm\/wp-content\/uploads\/sites\/29\/2021\/03\/Rcpp8.png\" alt=\"\" class=\"wp-image-316 size-full\" srcset=\"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/rebecca-hamm\/wp-content\/uploads\/sites\/29\/2021\/03\/Rcpp8.png 712w, https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/rebecca-hamm\/wp-content\/uploads\/sites\/29\/2021\/03\/Rcpp8-300x85.png 300w\" sizes=\"auto, (max-width: 712px) 100vw, 712px\" \/><\/figure><div class=\"wp-block-media-text__content\">\n<p class=\"has-normal-font-size\">Rebuild your package and you are good to go.<\/p>\n<\/div><\/div>\n\n\n\n<p>I hope you find this useful! To learn more about the Rcpp package look<a href=\"http:\/\/rcpp.org\/\" data-type=\"URL\" data-id=\"http:\/\/rcpp.org\/\"> here<\/a> or<a href=\"https:\/\/cran.r-project.org\/web\/packages\/Rcpp\/vignettes\/Rcpp-introduction.pdf\" data-type=\"URL\" data-id=\"https:\/\/cran.r-project.org\/web\/packages\/Rcpp\/vignettes\/Rcpp-introduction.pdf\"> here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This week I thought I would write a slightly different blog post. Instead of discussing an area of Statistics or&hellip;<\/p>\n","protected":false},"author":30,"featured_media":0,"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":[1],"tags":[8,10,7,6],"class_list":["post-305","post","type-post","status-publish","format-standard","hentry","category-blog","tag-c","tag-programming","tag-r","tag-rcpp"],"_links":{"self":[{"href":"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/rebecca-hamm\/wp-json\/wp\/v2\/posts\/305","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/rebecca-hamm\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/rebecca-hamm\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/rebecca-hamm\/wp-json\/wp\/v2\/users\/30"}],"replies":[{"embeddable":true,"href":"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/rebecca-hamm\/wp-json\/wp\/v2\/comments?post=305"}],"version-history":[{"count":13,"href":"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/rebecca-hamm\/wp-json\/wp\/v2\/posts\/305\/revisions"}],"predecessor-version":[{"id":379,"href":"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/rebecca-hamm\/wp-json\/wp\/v2\/posts\/305\/revisions\/379"}],"wp:attachment":[{"href":"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/rebecca-hamm\/wp-json\/wp\/v2\/media?parent=305"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/rebecca-hamm\/wp-json\/wp\/v2\/categories?post=305"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/rebecca-hamm\/wp-json\/wp\/v2\/tags?post=305"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}