{"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 – Rcpp"},"content":{"rendered":"\n
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 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 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 After obtaining our ‘cpp’ algorithm, we have to package it as a ‘zip’ 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 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 In my example, I created a package called ‘finaljarvismarch’, and write the path to my cpp file in ‘cpp_files’. If example_code=TRUE, the package will contain an example code.<\/p>\n\n\n\n This creates the package skeleton in the working directory. It contains three files and three folders:<\/p>\n\n\n\n We could manually put our further R function or C++ function in different folders.<\/p>\n\n\n\n Once we got the skeleton folder, run the command in terminal to create the package: <\/p>\n\n\n\n This builds the package tarball<\/a>, which can then be sent to and installed on any machine running R.<\/p>\n\n\n\n Until now, we have successfully create a package. However, we have to test if it could be install appropriately. <\/p>\n\n\n\n Run the command in the terminal in the directory:<\/p>\n\n\n\n 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: library(‘PackageTarBallName’)<\/span><\/p>\n\n\n\n We are asked to build a Jarvis March<\/a> package, you could download the tar file<\/a> here. After download it, it could be installed easily<\/p>\n\n\n\n Now you could use Jarvis march algorithm for 2 dimension data. In this package, it contains two functions: <\/p>\n\n\n\n For example, simulating 100 points, and run the functions:<\/p>\n\n\n\n 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 <\/p>\n","protected":false},"excerpt":{"rendered":" This blog is to give ideas how to build R package through Rcpp and C++. Here we assume our readers…<\/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}]}}
\n\n\n\nStep 1: Write your own algorithm in C++<\/h1>\n\n\n\n
<\/figure>\n\n\n\n
<\/figure>\n\n\n\n
<\/figure>\n\n\n\n
\n\n\n\nStep 2: Build package <\/h1>\n\n\n\n
1. Building Skeleton Folder<\/h2>\n\n\n\n
package.skeleton(\u201cThe name of package\u201d, cpp_files=\u201dpath to your c++ file\u201d, example_code=FALSE) <\/code><\/pre>\n\n\n\n
<\/figure>\n\n\n\n
<\/figure>\n\n\n\n2. Building Package<\/h2>\n\n\n\n
R CMD build PackageDirectory\/PackageSkeletonName <\/code><\/pre>\n\n\n\n
<\/figure>\n\n\n\n
\n\n\n\nStep 3: Checking instalment<\/h1>\n\n\n\n
R CMD INSTALL PackageTarBallName<\/code><\/pre>\n\n\n\n
<\/figure>\n\n\n\n
\n\n\n\nExample: Jarvis March algorithm<\/h1>\n\n\n\n
<\/figure>\n\n\n\n
<\/figure>\n\n\n\n