Share E-Book
Scan to open this page

Scan with your phone to open this page

Author: Hadley Wickham, Jennifer Bryan

Rating No ratings yet

Turn your R code into packages that others can easily install and use. With this fully updated edition, developers and data scientists will learn how to bundle reusable R functions, sample data, and documentation together by applying the package development philosophy used by the team that maintains the "tidyverse" suite of packages. In the process, you'll learn how to automate common development tasks using a set of R packages, including devtools, usethis, testthat, and roxygen2. Authors Hadley Wickham and Jennifer Bryan from Posit (formerly known as RStudio) help you create packages quickly, then teach you how to get better over time. You'll be able to focus on what you want your package to do as you progressively develop greater mastery of the structure of a package. With this book, you will: Learn the key components of an R package, including code, documentation, and tests Streamline your development process with devtools and the RStudio IDE Get tips on effective habits such as organizing functions into files Get caught up on important new features in the devtools ecosystem Learn about the art and science of unit testing, using features in the third edition of testthat Turn your existing documentation into a beautiful and user friendly website with pkgdown Gain an appreciation of the benefits of modern code hosting platforms, such as GitHub

AI Reading Assistant

Whole-book reading guide from stratified index samples; jump to passages in the text

AI guide
【One-Line Pitch】 A practical, hands-on guide for R users who want to turn scattered scripts into well-organized, testable, and shareable packages, written by the creators of the tidyverse toolchain. Ideal for data scientists and developers who have written R code and now want to professionalize it with modern tools like devtools, usethis, testthat, and roxygen2. 【Book Arc】 - **Opening (~0%–9%)**: Introduces the core philosophy that packages are the fundamental units of reproducible R code, and outlines the major updates in this edition, including the "conscious uncoupling" of devtools into smaller packages and new coverage of package websites and GitHub Actions. - **Early (~9%–25%)**: Walks through "The Whole Game"—a complete end-to-end example of creating a package from scratch, including system setup (Rtools on Windows, Xcode on macOS, r-base-dev on Linux), the initial file structure, and the first use of usethis functions to scaffold a project. - **Early (~25%–34%)**: Dives into the practical mechanics of package development, such as declaring dependencies with use_package(), adopting naming conventions, and understanding the role of DESCRIPTION and NAMESPACE files in managing package metadata and imports. - **Middle (~34%–47%)**: Covers foundational workflows and common pitfalls, including how to choose a good package name, the difference between build time and run time (with a memorable "Foxtrot" example showing why top-level assignments like `now <- Sys.time()` are dangerous), and why shipping data files requires special handling. - **Middle (~47%–53%)**: Continues with deeper lessons on package structure, emphasizing that most objects in a package should be functions that only use data they create or receive via arguments, and introduces the concept of testing with testthat as a core part of the development cycle. 【Key Takeaways】 - **Packages are the unit of reproducibility** (Opening): The book's central claim is that bundling functions, documentation, and sample data into a package is the best way to make R code reliable and shareable. This frames everything that follows. - **Start with a working skeleton, then improve** (Early): The "Whole Game" chapter deliberately shows a minimal but complete package workflow, reinforcing that your first version doesn't need to be perfect—just better than the last. This lowers the barrier to entry for beginners. - **System setup is platform-specific but essential** (Early): Windows users need Rtools (without editing PATH), macOS users need Xcode command-line tools, and Linux users need r-base-dev. Getting this right avoids frustrating build failures later. - **Dependencies should be declared, not assumed** (Early): Using `use_package("stringr")` adds the package to DESCRIPTION's Imports field, and the book recommends calling functions with `package::function()` syntax for clarity and namespace safety. - **Package names matter more than you think** (Middle): A good name is unique, Googleable, pronounceable, and avoids trademark issues. The book gives concrete examples like lubridate, rvest, and forcats to illustrate naming strategies. - **Build time vs. run time is a critical distinction** (Middle): The "Foxtrot" example shows that top-level assignments like `now <- Sys.time()` are evaluated at build time, not when the user runs the function. This is a subtle but crucial lesson for writing correct package code. - **Most package objects should be functions** (Middle): The book stresses that functions should only use data they create or receive via arguments, avoiding hidden global state that can break when code is distributed as a package. 【Reading Tips】 - **Skim the preface and "Whole Game" chapter first** (~0%–25%): These give you the big picture and a complete worked example, so you understand the end-to-end workflow before diving into details. You can always return to specifics later. - **Deep-read the "Foxtrot" example** (~47%): This is one of the most instructive parts of the book because it shows a realistic mistake (using `Sys.time()` at build time) and how to fix it. Understanding this will save you from a common class of bugs. - **Treat the system setup chapter as a checklist** (~9%–16%): Don't read it linearly—just find your operating system and follow the instructions. This is reference material, not narrative. - **Pay attention to the naming and dependency chapters** (~34%–44%): These are short but packed with practical advice that will save you pain later, especially if you plan to release your package publicly. - **Use the online version for code examples** (throughout): The book mentions that the electronic version at r-pkgs.org allows easy copy-paste of examples, which is invaluable when you're following along in your own R session. 【Coverage Limits】 This guide synthesizes the opening and early-middle portions of the book (roughly the first half). The excerpts do not cover the later chapters on testing with testthat, documentation with roxygen2, package websites with pkgdown, or GitHub Actions in detail—those topics are mentioned but not explored in depth here.
Excerpt 1
105 Raw Data File 106 Filepaths ...
View in text
Excerpt 2
outer (Wouter Saelens) xxvi | Preface #> RoxygenNote: 7.2.3 #> ✔ Writing 'NAMESPACE' #> ✔ Writing 'regexcite.Rproj' #> ✔ Adding '^regexcite\\.Rproj$' to '.Rb...
View in text
Excerpt 3
ll the r-base-dev package with: sudo apt install r-base-dev On Fedora and RedHat, the development tools (called R-core-devel) will be installed automatically...
View in text
Excerpt 4
, ~english, "beach", "US", "coast", "US", "seashore", "UK", "seaside", "UK" ) now <- Sys.time() timestamp <- function(time) fo...
View in text
Excerpt 5
First, it gives teachers and other expositors more to work with once they decide to use a specific dataset. If you’ve started teaching R with palmerpenguins:...
View in text
Excerpt 6
require() is in an example that uses a package your package Suggests, which is further discussed in “In Examples and Vignettes” on page 163. The .onLoad() an...
View in text
Excerpt 7
eone else’s code into your package, you need to first check that the bundled license is compatible with your license. When distributing code, you can add add...
View in text
Excerpt 8
carefully reviewed for possible conversion into a function. We have analogous advice for your test files: The test-*.R files below tests/testthat/ should con...
View in text
Tags
AI categories
Programmingdata science
Publish Year: 2023
Language: English
Pages: 381
File Format: PDF
File Size: 5.1 MB
Text Preview (First 20 pages)
Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

Generating text preview…