Skip to content

Commit f6a2b5b

Browse files
author
Stefan Fleck
committed
rotate_rds: the on_change_only argument now also accepts a list() of
paramters to be passed on to `all.equal.data.table` when comparing `data.tables`
1 parent 6116d1a commit f6a2b5b

5 files changed

Lines changed: 73 additions & 10 deletions

File tree

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
* `rotate()`, `backup()` and co. no longer fail on filenames that
44
contain special regex characters (such as `*` or `+`)
55
* `rotate()`, `backup()` and co. now work with hidden files
6+
* `rotate_rds`: the `on_change_only` argument now also accepts a `list()` of
7+
paramters to be passed on to `all.equal.data.table` when comparing `data.tables`
68

79

810
# rotor 0.3.5

R/rotate_rds.R

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
#' silently not rotate the file, while `rotate_rds_date()` will throw an
1111
#' error.
1212
#'
13-
#' @param on_change_only `logical` scalar. Rotate only if `object` is different
14-
#' from the object saved in `file`.
13+
#' @param on_change_only `logical` scalaror a `list`. Rotate only if `object`
14+
#' is different from the object saved in `file`. If a `list`, arguments
15+
#' that will be passed on to `data.table::all.equal` (only when both obects
16+
#' are `data.tables`)
1517
#'
1618
#' @inheritParams base::saveRDS
1719
#' @inheritDotParams rotate
@@ -132,16 +134,18 @@ rotate_rds_internal <- function(
132134
fun
133135
){
134136
assert(is_scalar_character(file))
135-
assert(is_scalar_bool(on_change_only))
137+
assert(is_scalar_bool(on_change_only) || is.list(on_change_only))
136138

137139
if (file.exists(file)){
138-
if (on_change_only){
140+
if (isTRUE(on_change_only) || is.list(on_change_only)){
139141
comp <- readRDS(file)
142+
if (is.list(on_change_only)){
143+
extra_args <- on_change_only
144+
} else {
145+
extra_args <- list()
146+
}
140147

141-
if (
142-
identical(object, comp) ||
143-
(inherits(object, "data.table") && inherits(comp, "data.table") && assert_namespace("data.table") && isTRUE(all.equal(object, comp)))
144-
){
148+
if (objects_are_equal(object, comp, extra_args)){
145149
message(ObjectHasNotChangedMessage("not rotating: object has not changed"))
146150
return(invisible(file))
147151
}
@@ -160,3 +164,23 @@ rotate_rds_internal <- function(
160164

161165
invisible(file)
162166
}
167+
168+
169+
170+
171+
objects_are_equal <- function(
172+
x,
173+
y,
174+
extra_args = NULL
175+
){
176+
if (identical(x, y)){
177+
return(TRUE)
178+
}
179+
180+
if (inherits(x, "data.table") && inherits(y, "data.table")){
181+
assert_namespace("data.table")
182+
return(isTRUE(do.call(all.equal, c(list(x, y), extra_args))))
183+
}
184+
185+
FALSE
186+
}

man/rotate.Rd

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/rotate_rds.Rd

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test_rotate_rds.R

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,34 @@ test_that("rotate_rds_date on_change_only", {
124124
expect_message(rotate_rds_date(dt, tf, on_change_only = TRUE), class = "ObjectHasNotChangedMessage")
125125
prune_backups(tf, 0)
126126
})
127+
128+
129+
130+
test_that("rotate_rds `on_change_only` works with arguments list", {
131+
dir.create(td, recursive = TRUE)
132+
on.exit(unlink(td, recursive = TRUE))
133+
134+
dt1 <- data.table::as.data.table(iris)
135+
dt2 <- dt1[rev(seq_len(nrow(dt1))), ]
136+
tf <- file.path(td, "testfile.rds")
137+
138+
expect_silent(rotate_rds(dt1, tf, on_change_only = TRUE))
139+
expect_message(rotate_rds(dt1, tf, on_change_only = TRUE), class = "ObjectHasNotChangedMessage")
140+
expect_message(rotate_rds(dt2, tf, on_change_only = list(ignore.row.order = TRUE)), class = "ObjectHasNotChangedMessage")
141+
expect_message(rotate_rds(dt1, tf, on_change_only = TRUE), class = "ObjectHasNotChangedMessage")
142+
expect_silent(rotate_rds(dt2, tf, on_change_only = TRUE))
143+
144+
expect_identical(n_backups(tf), 1L)
145+
prune_backups(tf, 0)
146+
})
147+
148+
149+
150+
151+
test_that("objects_are_equal ", {
152+
x <- data.table::data.table(a = 1:3)
153+
y <- data.table::data.table(a = 3:1)
154+
155+
expect_false(objects_are_equal(x, y, extra_args = list()))
156+
expect_true(objects_are_equal(x, y, extra_args = list(ignore.row.order = TRUE)))
157+
})

0 commit comments

Comments
 (0)