开发R包时,依赖lubridate、dplyr等tidyverse包,需在DESCRIPTION中声明哪些?
Great question! When building an R package that leverages functions from lubridate, dplyr, and other tidyverse tools, how you declare dependencies in your DESCRIPTION file depends entirely on how you're using those packages. Let's break this down clearly so you get it right:
The most common (and recommended) field for dependencies is Imports. This lets your package access functions from other packages without forcing them to load into the user's global environment (which avoids messy naming conflicts). Here's what you need to list:
lubridateanddplyr: If your code calls any of their functions (either with explicit namespace prefixes likelubridate::ymd()or via Roxygen2 imports), these two must go inImports.- Other tidyverse packages: Only list the specific ones you actually use. For example, if you rely on
tidyr::pivot_longer(), addtidyr; if you useggplot2::ggplot()for visualizations, addggplot2. Don't just list thetidyversemeta-package—this would force users to install dozens of packages they don't need.
Depends (Rare!) You almost never need Depends. This field loads the dependency package directly into the user's global environment, which can cause conflicts with other packages. The only time to consider it is if your package is a deep extension of a specific tidyverse tool, and users must have that package's functions readily available to use your package. Stick with Imports for 99% of cases.
To avoid manually updating DESCRIPTION (and missing dependencies), use Roxygen2 comments in your R scripts:
- For individual functions: Use
@importFrom lubridate ymd floor_dateto import only the specific functions you need. This keeps your package lightweight. - For full namespace access (not recommended unless you use dozens of functions from a package): Use
@import dplyrat the top of a script. - Run
devtools::document()after adding these comments, and Roxygen2 will automatically update yourDESCRIPTION'sImportsfield for you.
DESCRIPTION Snippet Here's how your DESCRIPTION might look if you use lubridate, dplyr, and tidyr:
Package: YourPackageName Title: Brief Description of Your Package's Purpose Version: 0.1.0 Authors@R: person("Your", "Name", email = "your.email@example.com", role = c("aut", "cre")) Description: A longer description explaining what your package does, mentioning its use of tidyverse tools for data manipulation and date handling. Imports: dplyr (>= 1.0.0), lubridate (>= 1.8.0), tidyr (>= 1.2.0) License: MIT + file LICENSE Encoding: UTF-8 LazyData: true RoxygenNote: 7.2.3
Pro tip: Always add minimum version numbers (like dplyr (>= 1.0.0)) if your code uses features introduced in a specific version. This prevents users with outdated packages from hitting errors.
内容的提问来源于stack exchange,提问作者Jelena膶uklina

