{"id":464,"date":"2021-03-02T20:48:45","date_gmt":"2021-03-02T20:48:45","guid":{"rendered":"http:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/matthew-davison\/?p=464"},"modified":"2021-04-28T09:30:14","modified_gmt":"2021-04-28T09:30:14","slug":"adding-r-package-documentation-rcpp-roxygen2","status":"publish","type":"post","link":"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/matthew-davison\/2021\/03\/02\/adding-r-package-documentation-rcpp-roxygen2\/","title":{"rendered":"Adding R Package Documentation"},"content":{"rendered":"\n<p><em>This blog will assume you are able to build a package for R and are comfortable with Linux. Fellow STOR-i student Hamish <a href=\"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/hamish-thorburn\/2020\/03\/29\/tutorial-building-packages-in-r\/\" data-type=\"URL\" data-id=\"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/hamish-thorburn\/2020\/03\/29\/tutorial-building-packages-in-r\/\">wrote a blog post on how to do this if you haven&#8217;t reached this point yet<\/a>. His post covers how add C++ code to this package also. In this post I will show you how to add documentation for both C++ functions and R functions.<\/em><\/p>\n\n\n\n<p>At any level of computer programming you may need help. I have worked with R for quite some time now and mainly use <a href=\"https:\/\/rstudio.com\/\" data-type=\"URL\" data-id=\"https:\/\/rstudio.com\/\">RStudio<\/a> for its friendly user interface. Despite this experience I still get stuck, forget what a function does, forget what I need to input into a function for it to work, etc.<\/p>\n\n\n\n<p>If this happens in R the quickest way to resolve these issues is to use the &#8220;help&#8221; function. For example, if I forget what &#8220;rnorm&#8221; does I can type &#8220;help(rnorm)&#8221; and it will direct me to the documentation telling me all about what it does. What if this documentation didn&#8217;t exist?<\/p>\n\n\n\n<p>I feel that providing documentation for the R packages you make is really important.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Required Packages<\/h2>\n\n\n\n<p>To use this tutorial you need both the &#8220;Rcpp&#8221; package (if you are implementing C++ functions) and &#8220;roxygen2&#8221; package. In R you can do this using the following code.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>install.packages(Rcpp) # Installs the Rcpp package\ninstall.packages(roxygen2) # Installs the roxygen2 package<\/code><\/pre>\n\n\n\n<p>The following parts will assume that you have an existing packages skeleton (<a href=\"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/hamish-thorburn\/2020\/03\/29\/tutorial-building-packages-in-r\/\" data-type=\"URL\" data-id=\"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/hamish-thorburn\/2020\/03\/29\/tutorial-building-packages-in-r\/\">see Step 1 of Hamish&#8217;s blog<\/a>), with either an R function in the &#8220;R&#8221; folder of the skeleton or a C++ file in the &#8220;src&#8221; folder.<\/p>\n\n\n\n<p>First use a text editor of choice to edit the file named &#8220;DESCRIPTION&#8221;. Simply edit the fields and save. Now navigate into the &#8220;man&#8221; folder and delete it&#8217;s contents, roxygen should generate new documentation files automatically.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Documentation For R Function<\/h2>\n\n\n\n<p>To add documentation for an R function open up the folder &#8220;R&#8221; and find the function you wish to add documentation to. Add the following code above your R function.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#' Name\n#'\n#' Description\n#' @param variable Parameter_Description\n#' @return Return_Description<\/code><\/pre>\n\n\n\n<p>Replace &#8220;Name&#8221; and &#8220;Description&#8221; with the name of your function and a short description of the function respectively.<\/p>\n\n\n\n<p>&#8220;@param&#8221; lets roxygen2 know it should add an argument named &#8220;variable&#8221; (note the naming needs to be the same as in the function). Replace &#8220;Parameter_Description&#8221; with a short description of the variable. Each variable of the function needs a line with it&#8217;s own details.<\/p>\n\n\n\n<p>&#8220;@return&#8221; lets roxygen2 know it should add a &#8220;value&#8221; section. Replace &#8220;Return_Description&#8221; with a short description of what the function outputs.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Documentation For C++ Function<\/h2>\n\n\n\n<p>To add documentation for a C++ function open up the folder &#8220;src&#8221; and find the function you wish to add documentation to. Add the following code above your C++ function (and above the [[Rcpp::export]] line).<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/' Name\n\/\/'\n\/\/' Description\n\/\/' @param variable Parameter_Description\n\/\/' @return Return_Description<\/code><\/pre>\n\n\n\n<p>All of these fields are the same as described in the R function section of this tutorial.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Building And Installing The Function<\/h2>\n\n\n\n<p>Navigate into the skeleton folder in a Linux terminal window and start R. You need to run the following code.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>library(Rcpp)\nlibrary(roxygen2)\nRcpp::compileAttributes()           # this updates the Rcpp layer from C++ to R\nroxygen2::roxygenize(roclets=\"rd\")  # this updates the documentation<\/code><\/pre>\n\n\n\n<p>You can now exit R and move up a folder level using the command &#8220;cd ..&#8221;<\/p>\n\n\n\n<p>To build the program run the following terminal command making sure to replace &#8220;PackageSkeletonName&#8221; with the name of your package skeleton. This will produce a package tarball that you can distribute and install.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>R CMD build PackageSkeletonName<\/code><\/pre>\n\n\n\n<p>For installation follow <a href=\"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/hamish-thorburn\/2020\/03\/29\/tutorial-building-packages-in-r\/\" data-type=\"URL\" data-id=\"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/hamish-thorburn\/2020\/03\/29\/tutorial-building-packages-in-r\/\">Step 3 in Hamish&#8217;s blog post<\/a>. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example Code<\/h2>\n\n\n\n<p>The example code contains an algorithm named &#8220;Jarvis March&#8221; implemented in C++ with documentation that takes a set of 2D points and returns the convex hull, and an R function with documentation that plots the convex hull of a set of 2D points. <\/p>\n\n\n\n<p>The details of these are not important for the tutorial but instead try to spot where the code in this blog has been used.<\/p>\n\n\n\n<p><a href=\"https:\/\/github.com\/Mattmandav\/roxygen2_tutorial\" data-type=\"URL\" data-id=\"https:\/\/github.com\/Mattmandav\/roxygen2_tutorial\">Example code can be found here<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Further Reading<\/h2>\n\n\n\n<p><a href=\"https:\/\/gallery.rcpp.org\/articles\/documenting-rcpp-packages\/\" data-type=\"URL\" data-id=\"https:\/\/gallery.rcpp.org\/articles\/documenting-rcpp-packages\/\">This post written by the authors of Rcpp helped me learn how to add documentation<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/cran.r-project.org\/web\/packages\/roxygen2\/vignettes\/roxygen2.html\" data-type=\"URL\" data-id=\"https:\/\/cran.r-project.org\/web\/packages\/roxygen2\/vignettes\/roxygen2.html\">Roxygen2 introduction<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/cran.r-project.org\/web\/packages\/Rcpp\/index.html\" data-type=\"URL\" data-id=\"https:\/\/cran.r-project.org\/web\/packages\/Rcpp\/index.html\">Rcpp CRAN page<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This blog will assume you are able to build a package for R and are comfortable with Linux. Fellow STOR-i student Hamish wrote a blog post on how to do this if you haven&#8217;t reached this point yet. His post covers how add C++ code to this package also. In this post I will show&hellip;&nbsp;<a href=\"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/matthew-davison\/2021\/03\/02\/adding-r-package-documentation-rcpp-roxygen2\/\" rel=\"bookmark\">Read More &raquo;<span class=\"screen-reader-text\">Adding R Package Documentation<\/span><\/a><\/p>\n","protected":false},"author":24,"featured_media":608,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"neve_meta_sidebar":"","neve_meta_container":"","neve_meta_enable_content_width":"","neve_meta_content_width":0,"neve_meta_title_alignment":"","neve_meta_author_avatar":"","neve_post_elements_order":"","neve_meta_disable_header":"","neve_meta_disable_footer":"","neve_meta_disable_title":"","footnotes":""},"categories":[9,2],"tags":[],"class_list":["post-464","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-programming","category-research"],"_links":{"self":[{"href":"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/matthew-davison\/wp-json\/wp\/v2\/posts\/464","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/matthew-davison\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/matthew-davison\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/matthew-davison\/wp-json\/wp\/v2\/users\/24"}],"replies":[{"embeddable":true,"href":"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/matthew-davison\/wp-json\/wp\/v2\/comments?post=464"}],"version-history":[{"count":24,"href":"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/matthew-davison\/wp-json\/wp\/v2\/posts\/464\/revisions"}],"predecessor-version":[{"id":615,"href":"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/matthew-davison\/wp-json\/wp\/v2\/posts\/464\/revisions\/615"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/matthew-davison\/wp-json\/wp\/v2\/media\/608"}],"wp:attachment":[{"href":"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/matthew-davison\/wp-json\/wp\/v2\/media?parent=464"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/matthew-davison\/wp-json\/wp\/v2\/categories?post=464"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.lancaster.ac.uk\/stor-i-student-sites\/matthew-davison\/wp-json\/wp\/v2\/tags?post=464"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}