Skip to content

Commit

Permalink
Use use_import_from() in other importing functions (r-lib#1396)
Browse files Browse the repository at this point in the history
* add `use_import_from()` to `use_pipe()`

* remove test re: advice since use_import_from requires pkg doc

* add `use_import_from()` to `use_tibble()` and update test

* add `use_import_from()` to `use_data_table()`, remove template, and update tests

* update `use_data_table()` docs

* add a more informative message when no pkg doc

* simplify sentence in data table doc

* tell user about data table dependency action

* check installed before checking fun exists

* update tests + test `use_data_table()` dependency action

* nvm, this is done in `check_has_package_doc()`

* just don't test when data.table not installed

* there are so many places where the package needs to be checked, better to do so immediately

* pacify tibble test

* simplify data.table tests

* mock use_data_table() tests to get around ns loading

* mock tibble tests, too, just in case

* remove dead code RIP
  • Loading branch information
malcolmbarrett authored Apr 16, 2021
1 parent 13b3d1d commit c8c1b94
Show file tree
Hide file tree
Showing 12 changed files with 97 additions and 81 deletions.
29 changes: 15 additions & 14 deletions R/data-table.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@
#'
#' @description
#'
#' `use_data_table` facilitates importing the data.table package by handling
#' up-front some common set-up tasks for using it in your package.
#'
#' This function does two main things:
#'
#' 1. Import the entire data.table namespace (with `@import`).
#'
#' 2. Block the usage of data.table as a dependency (`DESCRIPTION` field
#' `Depends`); `data.table` should be used as an _imported_ or _suggested_
#' package only. See this
#' `use_data_table()` imports the `data.table()` function from the data.table
#' package, as well as several important symbols: `:=`, `.SD`, `.BY`, `.N`,
#' `.I`, `.GRP`, `.NGRP`, `.EACHI`. This is a minimal setup to get `data.table`s
#' working with your package. See the [importing
#' data.table](https://rdatatable.gitlab.io/data.table/articles/datatable-importing.html)
#' vignette for other strategies. In addition to importing these function,
#' `use_data_table()` also blocks the usage of data.table in the `Depends` field
#' of the `DESCRIPTION` file; `data.table` should be used as an _imported_ or
#' _suggested_ package only. See this
#' [discussion](https://github.com/Rdatatable/data.table/issues/3076).
#' @export
use_data_table <- function() {
Expand All @@ -21,10 +20,12 @@ use_data_table <- function() {
deps <- desc::desc_get_deps(".")
if (any(deps$type == "Depends" & deps$package == "data.table")) {
ui_warn("data.table should be in Imports or Suggests, not Depends")
ui_done("Deleting data.table from {ui_field('Depends')}")
desc::desc_del_dep("data.table", "Depends", file = proj_get())
}
use_dependency("data.table", "Imports")
new <- use_template("data-table.R", "R/utils-data-table.R")
ui_todo("Run {ui_code('devtools::document()')}")
invisible(new)

use_import_from(
"data.table",
c("data.table", ":=", ".SD", ".BY", ".N", ".I", ".GRP", ".NGRP", ".EACHI")
)
}
19 changes: 2 additions & 17 deletions R/pipe.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,11 @@ use_pipe <- function(export = TRUE) {
check_is_package("use_pipe()")
check_uses_roxygen("use_pipe()")

use_dependency("magrittr", "Imports")

if (export) {
use_dependency("magrittr", "Imports")
use_template("pipe.R", "R/utils-pipe.R") && roxygen_remind()
return(invisible(TRUE))
}

if (has_package_doc()) {
roxygen_ns_append("@importFrom magrittr %>%") && roxygen_remind()
return(invisible(TRUE))
}

ui_todo(
"Copy and paste this line into some roxygen header, then run \\
{ui_code('devtools::document()')}:"
)
ui_code_block("#' @importFrom magrittr %>%", copy = FALSE)
ui_todo(
"Alternative recommendation: call {ui_code('use_package_doc()')}, then \\
call {ui_code('use_pipe()')} again."
)
invisible(FALSE)
use_import_from("magrittr", "%>%")
}
5 changes: 3 additions & 2 deletions R/tibble.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ use_tibble <- function() {
check_is_package("use_tibble()")
check_uses_roxygen("use_tibble()")

use_dependency("tibble", "Imports")
roxygen_ns_append("@importFrom tibble tibble") && roxygen_remind()
created <- use_import_from("tibble", "tibble")

ui_todo("Document a returned tibble like so:")
ui_code_block("#' @return a [tibble][tibble::tibble-package]", copy = FALSE)

invisible(created)
}
8 changes: 6 additions & 2 deletions R/use_import_from.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ use_import_from <- function(package, fun, load = is_interactive()) {
}
check_is_package("use_import_from()")
check_uses_roxygen("use_import_from()")
check_installed(package)
check_has_package_doc("use_import_from()")

purrr::walk2(package, fun, check_fun_exists)
check_functions_exist(package, fun)

use_dependency(package, "Imports")
changed <- roxygen_ns_append(glue("@importFrom {package} {fun}"))
Expand All @@ -41,6 +41,10 @@ use_import_from <- function(package, fun, load = is_interactive()) {
invisible(changed)
}

check_functions_exist <- function(package, fun) {
purrr::walk2(package, fun, check_fun_exists)
}

check_fun_exists <- function(package, fun) {
if (exists(fun, envir = asNamespace(package))) {
return()
Expand Down
12 changes: 0 additions & 12 deletions inst/templates/data-table.R

This file was deleted.

18 changes: 8 additions & 10 deletions man/use_data_table.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions tests/testthat/_snaps/data-table.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# use_data_table() Imports data.table

Code
roxygen_ns_show()
Output
[1] "#' @importFrom data.table .BY"
[2] "#' @importFrom data.table .EACHI"
[3] "#' @importFrom data.table .GRP"
[4] "#' @importFrom data.table .I"
[5] "#' @importFrom data.table .N"
[6] "#' @importFrom data.table .NGRP"
[7] "#' @importFrom data.table .SD"
[8] "#' @importFrom data.table :="
[9] "#' @importFrom data.table data.table"

# use_data_table() blocks use of Depends

Code
roxygen_ns_show()
Output
[1] "#' @importFrom data.table .BY"
[2] "#' @importFrom data.table .EACHI"
[3] "#' @importFrom data.table .GRP"
[4] "#' @importFrom data.table .I"
[5] "#' @importFrom data.table .N"
[6] "#' @importFrom data.table .NGRP"
[7] "#' @importFrom data.table .SD"
[8] "#' @importFrom data.table :="
[9] "#' @importFrom data.table data.table"

10 changes: 0 additions & 10 deletions tests/testthat/_snaps/pipe.md

This file was deleted.

6 changes: 1 addition & 5 deletions tests/testthat/_snaps/tibble.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@
use_tibble()
Message <message>
v Adding 'tibble' to Imports field in DESCRIPTION
* Copy and paste the following lines into 'R/mypackage-package.R':
## usethis namespace: start
#' @importFrom tibble tibble
## usethis namespace: end
NULL
v Adding '@importFrom tibble tibble' to 'R/mypackage-package.R'
* Document a returned tibble like so:
#' @return a [tibble][tibble::tibble-package]

25 changes: 23 additions & 2 deletions tests/testthat/test-data-table.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,32 @@ test_that("use_data_table() requires a package", {

test_that("use_data_table() Imports data.table", {
create_local_package()
use_package_doc()
with_mock(
check_installed = function(pkg) TRUE,
roxygen_update_ns = function(...) NULL,
check_functions_exist = function(...) TRUE,
use_data_table()
)

expect_match(desc::desc_get("Imports"), "data.table")
expect_snapshot(roxygen_ns_show())
})

test_that("use_data_table() blocks use of Depends", {
create_local_package()
use_package_doc()
desc::desc_set_dep("data.table", "Depends")
with_mock(
check_installed = function(pkg) TRUE,
roxygen_update_ns = function(...) NULL,
check_functions_exist = function(...) TRUE,
expect_warning(
use_data_table(),
"data.table should be in Imports or Suggests, not Depends"
)
)

expect_match(desc::desc_get("Imports"), "data.table")
datatable_doc <- read_utf8(proj_path("R", "utils-data-table.R"))
expect_match(datatable_doc, "#' @import data.table", all = FALSE)
expect_snapshot(roxygen_ns_show())
})
6 changes: 0 additions & 6 deletions tests/testthat/test-pipe.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,3 @@ test_that("use_pipe(export = FALSE) adds roxygen to package doc", {
expect_match(package_doc, "#' @importFrom magrittr %>%", all = FALSE)
})

test_that("use_pipe(export = FALSE) gives advice if no package doc", {
create_local_package()
withr::local_options(list(usethis.quiet = FALSE))
expect_snapshot(use_pipe(export = FALSE))
expect_match(desc::desc_get("Imports", proj_get()), "magrittr")
})
10 changes: 9 additions & 1 deletion tests/testthat/test-tibble.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ test_that("use_tibble() requires a package", {
test_that("use_tibble() Imports tibble", {
create_local_package(path_temp("mypackage"))
withr::local_options(list(usethis.quiet = FALSE))
expect_snapshot(use_tibble())
ui_silence(use_package_doc())

with_mock(
check_installed = function(pkg) TRUE,
roxygen_update_ns = function(...) NULL,
check_functions_exist = function(...) TRUE,
expect_snapshot(use_tibble())
)

expect_match(desc::desc_get("Imports", proj_get()), "tibble")
})

0 comments on commit c8c1b94

Please sign in to comment.