R packages

7,252 views
7,061 views

Published on

Published in: Technology
0 Comments
14 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
7,252
On SlideShare
0
From Embeds
0
Number of Embeds
25
Actions
Shares
0
Downloads
114
Comments
0
Likes
14
Embeds 0
No embeds

No notes for slide

R packages

  1. 1. November 2010 Hadley Wickham Assistant Professor / Dobelman Family Junior Chair Department of Statistics / Rice University Making an R package Tuesday, 9 November 2010
  2. 2. https://github.com/ hadley/devtools/wiki What I’ve learned from developing 20+ R packages Tuesday, 9 November 2010
  3. 3. 1. Getting started 2. Development cycle 3. Documentation 4. What to learn next W arning: opinionated advice Tuesday, 9 November 2010
  4. 4. Use a mac! (or linux) Otherwise, start by downloading and installing http://www.murdoch-sutherland.com/Rtools/ Tuesday, 9 November 2010
  5. 5. Getting started Tuesday, 9 November 2010
  6. 6. 1. Decide on a name (stringr) 2. Create stringr/ and stringr/R/ directories 3. Copy in your existing code !"" R #   !"" file1.r #   !"" file2.r Tuesday, 9 November 2010
  7. 7. 4. Add a description file !"" DESCRIPTION !"" R Tuesday, 9 November 2010
  8. 8. Package: stringr Type: Package Title: Make it easier to work with strings. Version: 0.5 Author: Hadley Wickham <h.wickham@gmail.com> Maintainer: Hadley Wickham <h.wickham@gmail.com> Description: stringr is a set of simple wrappers that make R's string functions more consistent, simpler and easier to use. It does this by ensuring that: function and argument names (and positions) are consistent, all functions deal with NA's and zero length character appropriately, and the output data structures from each function matches the input data structures of other functions. Imports: plyr Depends: R (>= 2.11.0) Suggests: testthat (>= 0.3) License: GPL-2 https://github.com/hadley/devtools/wiki/Package-basics Tuesday, 9 November 2010
  9. 9. 4. Document your files 5. Run roxygenize() !"" DESCRIPTION !"" man !"" R Tuesday, 9 November 2010
  10. 10. #' The length of a string (in characters). #' #' @param string input character vector #' @return numeric vector giving number of characters in #' each element of the character vector. Missing strings have #' missing length. #' @keywords character #' @seealso code{link{nchar}} which this function wraps #' @export #' @examples #' str_length(letters) #' str_length(c("i", "like", "programming", NA)) str_length <- function(string) { string <- check_string(string) nc <- nchar(string, allowNA = TRUE) is.na(nc) <- is.na(string) nc } https://github.com/hadley/devtools/wiki/docs-function Tuesday, 9 November 2010
  11. 11. 6. Run R CMD check 7. Use R CMD build to create a package file 8. Upload file to CRAN: ftp -u ftp://cran.R- project.org/incoming/ stringr_0.5.tar.gz !"" DESCRIPTION !"" man !"" R https://github.com/hadley/devtools/wiki/Release Tuesday, 9 November 2010
  12. 12. You’re done! Tuesday, 9 November 2010
  13. 13. Development cycle Tuesday, 9 November 2010
  14. 14. Modify and save code Reload in R Does it work? Identify the task Write an automated testYES NO Exploratory programming Tuesday, 9 November 2010
  15. 15. Confirmatory programming Modify and save code Reload in R Does it work? Write an automated test YES NO You’re done Tuesday, 9 November 2010
  16. 16. # These patterns are facilitated by the devtools # package: https://github.com/hadley/devtools # Once installed and set up you can easily: # * Reload code and data load_all("stringr") # * Run automated tests test("stringr") # * Update documentation document("stringr") Tuesday, 9 November 2010
  17. 17. Documentation Tuesday, 9 November 2010
  18. 18. Documentation Function-level: gives precise details about individual functions Package-level: gives details about how to use the functions to solve real (complex) problems. Both are essential! Tuesday, 9 November 2010
  19. 19. Function-level Keep code and documentation close together with roxygen. Writing documentation gets easier with time! Tuesday, 9 November 2010
  20. 20. Package-level Package help topic NEWS Vignettes + CITATION Demos README https://github.com/hadley/devtools/wiki/docs-package Tuesday, 9 November 2010
  21. 21. Next... Tuesday, 9 November 2010
  22. 22. Learning more Testing Namespaces Code style Git + github Tuesday, 9 November 2010
  23. 23. Learn from others Read the source of other packages! https://github.com/hadley/plyr https://github.com/hadley/stringr https://github.com/hadley/lubridate https://github.com/hadley/evaluate https://github.com/hadley/reshape Tuesday, 9 November 2010
  24. 24. Tuesday, 9 November 2010
  25. 25. This work is licensed under the Creative Commons Attribution-Noncommercial 3.0 United States License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc/ 3.0/us/ or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA. Tuesday, 9 November 2010

×