Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change the pin_write() function signature, moving the dots #792

Merged
merged 8 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# pins (development version)

* Changed the function signature of `pin_write()` so arguments like `type` and `title` must be passed by name and not position (#792).

# pins 1.2.2

* Fixed how dots are checked in `pin_write()` to make user-facing messages more
Expand Down
6 changes: 5 additions & 1 deletion R/pin-read-write.R
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,19 @@ pin_read <- function(board, name, version = NULL, hash = NULL, ...) {
#' @export
pin_write <- function(board, x,
name = NULL,
...,
type = NULL,
title = NULL,
description = NULL,
metadata = NULL,
versioned = NULL,
tags = NULL,
...,
force_identical_write = FALSE) {
check_board(board, "pin_write", "pin")
dots <- list2(...)
if (!is_empty(dots) && is.null(names(dots[1]))) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the logic is quite right here as names will be NULL if all the elements are unnamed, but if there's one named element, it'll be "".

I don't know if ...names() is available in old enough R for us to use it, but you could also try something like this:

if (!missing(...) && !(is.null(...names())) || ...names()[[1]] == ""))

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like we do not yet have ...names() in oldrel-3 and oldrel-4.

cli::cli_abort('Arguments after the dots `...` must be named, like {.code type = "{dots[[1]]}"}.')
}

if (is.null(name)) {
name <- enexpr(x)
Expand Down
2 changes: 1 addition & 1 deletion man/pin_read.Rd

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

5 changes: 5 additions & 0 deletions tests/testthat/_snaps/pin-read-write.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
Condition
Error in `pin_write()`:
! `name` must be a string
Code
pin_write(board, mtcars, name = 1:10, "json")
Condition
Error in `pin_write()`:
! Arguments after the dots `...` must be named, like `type = "json"`.
Code
pin_write(board, mtcars, name = "mtcars", type = "froopy-loops")
Condition
Expand Down
3 changes: 2 additions & 1 deletion tests/testthat/test-pin-read-write.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ test_that("useful errors on bad inputs", {
expect_snapshot(error = TRUE, {
pin_write(mtcars)
pin_write(board, mtcars, name = 1:10)
pin_write(board, mtcars, name = 1:10, "json")
pin_write(board, mtcars, name = "mtcars", type = "froopy-loops")
pin_write(board, mtcars, name = "mtcars", metadata = 1)
})
Expand All @@ -65,7 +66,7 @@ test_that("pin_write() noisily generates name and type", {
test_that("user can supply metadata", {
board <- board_temp()

pin_write(board, 1:10, "x", metadata = list(name = "Susan"), desc = "A vector")
pin_write(board, 1:10, "x", metadata = list(name = "Susan"), description = "A vector")
meta <- pin_meta(board, "x")
expect_equal(meta$user, list(name = "Susan"))
expect_equal(meta$description, "A vector")
Expand Down
Loading