diff --git a/NEWS.md b/NEWS.md index c0eda5f..184951c 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,10 @@ # tibbletime (development version) +* Bug fixes + + * `collapse_by()` no longer errors when there is a column + named `start_date` in the `tbl_time` object (#81). + ## tibbletime 0.1.2 * Features diff --git a/R/collapse_index.R b/R/collapse_index.R index 7a56c17..c0659e7 100644 --- a/R/collapse_index.R +++ b/R/collapse_index.R @@ -187,12 +187,14 @@ collapse_by <- function(.tbl_time, period = "yearly", start_date = NULL, side = index_quo <- get_index_quo(.tbl_time) index_char <- get_index_char(.tbl_time) + start_date <- rlang::enquo(start_date) + .tbl_time_collapsed <- dplyr::mutate( .data = .tbl_time, !! index_char := collapse_index( index = !! index_quo, period = period, - start_date = start_date, + start_date = !! start_date, side = side, clean = clean, ... diff --git a/tests/testthat/test_collapse_index.R b/tests/testthat/test_collapse_index.R index 885165a..46c03fe 100644 --- a/tests/testthat/test_collapse_index.R +++ b/tests/testthat/test_collapse_index.R @@ -63,3 +63,29 @@ test_that("day becomes DSTday for POSIXct to prevent DST boundary problems", { expect_equal(ret, test) }) + +test_that("can use `collapse_by()` when a column is named `start_date` (#81)", { + x <- data.frame( + start_date = as.Date("2017-12-01") + 0:2, + value = c(1, 2, 3) + ) + + x <- as_tbl_time(x, start_date) + + expect_equal( + collapse_by(x), + dplyr::mutate(x, start_date = collapse_index(start_date)) + ) + + expect_equal( + collapse_by(x, start_date = as.Date("2017-01-01"), side = "start", period = "2 days"), + dplyr::mutate(x, start_date = collapse_index(start_date, "2 days", start_date = as.Date("2017-01-01"), side = "start")) + ) + + expect_equal( + collapse_by(x, start_date = as.Date("2016-12-31"), side = "start", period = "2 days"), + dplyr::mutate(x, start_date = collapse_index(start_date, "2 days", start_date = as.Date("2016-12-31"), side = "start")) + ) +}) + +