Writing a package sounds big - and it can for sure be. But in its simplest form, itβs not that much more than putting a function in a package structure. The R community is great and came up with multiple great helpers that make your life easier!
Simply speaking, an R package allows you to put functions in a box and make them available for others to use. Ideally, your R package also comes with unit tests that make sure that your package works (or if it doesn’t throw meaningful errors and let you dive into the functions and explore why it doesn’t) and that it adheres to the common standards of developing a package.
A package structure usually consists of
DESCRIPTION
file that gives the user all core information about your packageNAMESPACE
file that contains information on exported and imported functionsR
folder where multiple R files live that contain your functions (it’s usually a good idea to write one R file per function and to also call the file name as you call your function. Trust me, this makes debugging (and also maneuvering through your functions) so much easier π₯³)man
folder that contains all manuals for your functions (it will be automatically generated once you created functions and called devtools::document()
)tests
folder where your unit tests live (this is where the {testthat}
package is extremely helpful!)This is the default package structure that you get when starting a new package project in RStudio:
Alternative text
Showing how the RStudio desktop version looks like with a typical package structure (gitignore, Rbuildignore, DESCRIPTION, man, NAMESAPCE, R/)
RStudio is great, just follow these steps: Select “File”, “New Project…”, “New Directory” and select “R Package”. You can now give your R package a meaningful name, select a path and hit “Create project”.
You’re now ready to go! Once executed, you have a fully functional package structure in your Rproject (that we already discussed) π Now it’s time to move your function to your R/
folder and populate it!
Let’s do this with a simple make_sum()
function:
make_sum <- function(a, b) {
c <- a + b
return(c)
}
Open a new package environment, add a new file called make_sum.R
in the R/
folder, and copy-paste the code from make_sum
. There’s already the very first function that lives in the package π€© Calling now devtools::load_all()
allows us to use the function.
Alternative text
There is an R file called “make_sum.R” with the function code to calculate the sum. We run “devtools::load_all()” and call “make_sum(1,3)” and get our result (“4”)
This is the very beginning of a package!
π©πΌβπ« I collected these steps to develop your package (and more) in my talk at CorrelAid’s CorrelCon 2021:
When building your R package, you can luckily rely on the work of others who provide an excellent framework to get you started:
If you want to know what else is out there, Indrajeet Patil collected many great packages (and resources) that can help you to build your package. π₯
If this inspired you and you want to write your package now, there are plenty of great resources out there!
πΊ Several R-Ladies talked about how to set up your package within no time:
π Dennis Hammerschmidt and I wrote a blog post about our experience when building our package {overviewR} and (hopefully) an easy how-to guide that also features more resources at the end (and it also comes with a checklist that I always use when I update our package and before sending it to CRAN). If you want a teaser, here’s more about it:
How to write an #rstats π¦ *and get it published on CRAN*. With helpful list of checks before submitting your pkg. #cranxiety
— Sharon Machlis now at @smach@masto.machlis.com (@sharon000) September 14, 2022
By @cosima_meyer and @d_hammers https://t.co/mSas6CYhVg pic.twitter.com/coBJWESZOj
π While it’s hard to put everything on one page, here’s my attempt to summarize the most important things to know about package development in R (also as πPDF for you to download here):
Alternative text
A summary reiterating the basic structure in package development (DESCRIPTION, NAMESPACE, R/, man/, and tests/) as well as helpful packages (devtools, use this, roxygen2, testthat, xpectr, cover, goodpractice, inteRgrate) reiterating the previous tweets.