Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Everything you wanted to know
about making R packages but
were afraid to ask
Emily Robinson
@robinson_es
Five Topics
Motivating
Making
Documenting
Improving
Marketing
Why make a
package?
Isn’t making packages for “real programmers”?
Can you open and run R / RStudio?
Can you install a package?
Can you writ...
Does making a package take a lot of time?
“It took me such little time that I was hit with that familiar feeling of the
jo...
When is it time to write a package?
If you’ve copied and pasted
code three times, write a
function
If you’ve used the same...
Benefits of making a package
Yourself
Your company
or team
Public
• Saves yourself time
• Reduces the risk of error
• Help...
How do you make
a package?
Two helper packages
Create the package structure, add a function
library(usethis)
create_package(“~/Desktop/nyrconf”)
use_r(“i_love_nyrconf”)
Add function documentation
use_roxygen_md()
• Write documentation
early, soon after you
finish a function
• It’s useful no...
Add function documentation
use_roxygen_md()
• Write documentation
early, soon after you
finish a function
• It’s useful no...
View your documentation
devtools::document()
?i_love_nyrconf
How do you
document your
package?
DESCRIPTION File
The Package Documentation Pyramid
Name
Title
(one sentence)
Description
(one paragraph)
README
Vignette
What’s in a name?
 Not taken on CRAN
 Not too long
 Is easy to google
 Evokes what it does
Other considerations …
https://twitter.com/hadleywickham/status/1008799676533964800
Title
• Describes what the package does
• One sentence, ideally < 65 characters
Description
• One paragraph focused on the value of your package
Summary
sentence
Benefits
Main
functions
README – Overview
README – Installation instructions
README – Examples
Vignette
• It’s a case study
• Use real and interesting data
• If it’s statistics package, make
a model; if it’s visualiza...
Vignette
• It’s a case study
• Use real and interesting data
• If it’s statistics package, make
a model; if it’s visualiza...
How can you improve
your package?
“This worked fine before …”
Solution: Unit Tests
Write tests with testthat
• Write tests early and often
so then you can safely
develop
• Whenever you fix a bug,
put in a ...
How do you market
your package?
Marketing is not a bad word
Publish it on GitHub
• GitHub lets you easily share
your package
• If you’ve never used GitHub,
check out Jenny Bryan’s
Ha...
Make a website with pkgdown
• pkgdown::build_site()
Blog and tweet about it!
• Use #rstats
• Give a one line
motivation
• Can remix your
README and Vignette
Submit your package to CRAN
“If you want your package to have significant traction in the R community,
you need to submit ...
If you’re successful …
Resources
Drum roll please …
There’s a new R packages book in the works!!
https://r-pkgs.org/
Other resources
• Hilary Parker on writing an R package from scratch
• Jim Hester: “You can make a package in 20 minutes”
...
Thank you!
bit.ly/nyrconf19
@robinson_es
hookedondata.org
Upcoming SlideShare
Loading in …5
×

Everything you wanted to know about making an R package but were afraid to ask

197 views

Published on

Why should you make a package? What tools can help you? How can you make your package better? How do you let people know about it? In this talk, given at NY R Conference 2019, I cover my answers to all these questions.

Published in: Technology
  • Be the first to comment

Everything you wanted to know about making an R package but were afraid to ask

  1. 1. Everything you wanted to know about making R packages but were afraid to ask Emily Robinson @robinson_es
  2. 2. Five Topics Motivating Making Documenting Improving Marketing
  3. 3. Why make a package?
  4. 4. Isn’t making packages for “real programmers”? Can you open and run R / RStudio? Can you install a package? Can you write R code? Can you write an R function? Can you learn to write an R function? Excellent, you can write a package! From Susan Johnston, as used in Jim Hester’s RStudio 2018 conference talk https://resources.rstudio.com/rstudio-conf-2018/you-can-make-a-package-in-20-minutes-jim-hester
  5. 5. Does making a package take a lot of time? “It took me such little time that I was hit with that familiar feeling of the joy of optimization combined with the regret of past inefficiencies (joygret?). I wish I could go back in time and create the package the first moment I thought about it” - Hilary Parker https://hilaryparker.com/2014/04/29/writing-an-r-package-from-scratch/
  6. 6. When is it time to write a package? If you’ve copied and pasted code three times, write a function If you’ve used the same function across three analyses, write a package
  7. 7. Benefits of making a package Yourself Your company or team Public • Saves yourself time • Reduces the risk of error • Helps you share your code • Includes documentation with your functions • Give back to your community • Builds your brand
  8. 8. How do you make a package?
  9. 9. Two helper packages
  10. 10. Create the package structure, add a function library(usethis) create_package(“~/Desktop/nyrconf”) use_r(“i_love_nyrconf”)
  11. 11. Add function documentation use_roxygen_md() • Write documentation early, soon after you finish a function • It’s useful not just for other people, but for your own future self
  12. 12. Add function documentation use_roxygen_md() • Write documentation early, soon after you finish a function • It’s useful not just for other people, but for your own future self
  13. 13. View your documentation devtools::document() ?i_love_nyrconf
  14. 14. How do you document your package?
  15. 15. DESCRIPTION File
  16. 16. The Package Documentation Pyramid Name Title (one sentence) Description (one paragraph) README Vignette
  17. 17. What’s in a name?  Not taken on CRAN  Not too long  Is easy to google  Evokes what it does
  18. 18. Other considerations … https://twitter.com/hadleywickham/status/1008799676533964800
  19. 19. Title • Describes what the package does • One sentence, ideally < 65 characters
  20. 20. Description • One paragraph focused on the value of your package Summary sentence Benefits Main functions
  21. 21. README – Overview
  22. 22. README – Installation instructions
  23. 23. README – Examples
  24. 24. Vignette • It’s a case study • Use real and interesting data • If it’s statistics package, make a model; if it’s visualization package, make a graph
  25. 25. Vignette • It’s a case study • Use real and interesting data • If it’s statistics package, make a model; if it’s visualization package, make a graph
  26. 26. How can you improve your package?
  27. 27. “This worked fine before …”
  28. 28. Solution: Unit Tests
  29. 29. Write tests with testthat • Write tests early and often so then you can safely develop • Whenever you fix a bug, put in a test that would have caught it use_testthat() use_test(“i_love_nyrconf”)
  30. 30. How do you market your package?
  31. 31. Marketing is not a bad word
  32. 32. Publish it on GitHub • GitHub lets you easily share your package • If you’ve never used GitHub, check out Jenny Bryan’s Happy Git and GitHub for the useR https://happygitwithr.com/
  33. 33. Make a website with pkgdown • pkgdown::build_site()
  34. 34. Blog and tweet about it! • Use #rstats • Give a one line motivation • Can remix your README and Vignette
  35. 35. Submit your package to CRAN “If you want your package to have significant traction in the R community, you need to submit it to CRAN. Submitting to CRAN is a lot more work than just providing a version on GitHub, but the vast majority of R users do not install packages from GitHub, because CRAN provides discoverability, ease of installation and a stamp of authenticity.” - Hadley Wickham http://r-pkgs.had.co.nz/release.html
  36. 36. If you’re successful …
  37. 37. Resources
  38. 38. Drum roll please …
  39. 39. There’s a new R packages book in the works!! https://r-pkgs.org/
  40. 40. Other resources • Hilary Parker on writing an R package from scratch • Jim Hester: “You can make a package in 20 minutes” • Karl Broman’s tutorial • Sharla Gelfand on usethis for reporting • usethis website • Maëlle Salmon on developing good R packages for open science • Julia Silge’s beginner’s guide to TravisCI
  41. 41. Thank you! bit.ly/nyrconf19 @robinson_es hookedondata.org

×