@@ -278,41 +278,22 @@ next_after.Date <- function(x) x + 1L
278
278
# ' x <- df %>% as_epi_archive(other_keys = "county")
279
279
# '
280
280
new_epi_archive <- function (
281
- x ,
281
+ data_table ,
282
282
geo_type ,
283
283
time_type ,
284
284
other_keys ,
285
285
clobberable_versions_start ,
286
286
versions_end ) {
287
- assert_data_frame( x )
287
+ assert_class( data_table , " data.table " )
288
288
assert_string(geo_type )
289
289
assert_string(time_type )
290
290
assert_character(other_keys , any.missing = FALSE )
291
291
if (any(c(" geo_value" , " time_value" , " version" ) %in% other_keys )) {
292
292
cli_abort(" `other_keys` cannot contain \" geo_value\" , \" time_value\" , or \" version\" ." )
293
293
}
294
- validate_version_bound(clobberable_versions_start , x , na_ok = TRUE )
295
- validate_version_bound(versions_end , x , na_ok = FALSE )
296
-
297
- key_vars <- c(" geo_value" , " time_value" , other_keys , " version" )
298
- if (! all(key_vars %in% names(x ))) {
299
- # Give a more tailored error message than as.data.table would:
300
- cli_abort(c(
301
- " `x` is missing the following expected columns:
302
- {format_varnames(setdiff(key_vars, names(x)))}." ,
303
- " >" = " You might need to `dplyr::rename()` beforehand
304
- or use `as_epi_archive()`'s renaming feature." ,
305
- " >" = if (! all(other_keys %in% names(x ))) {
306
- " Check also for typos in `other_keys`."
307
- }
308
- ))
309
- }
310
-
311
- # Create the data table; if x was an un-keyed data.table itself,
312
- # then the call to as.data.table() will fail to set keys, so we
313
- # need to check this, then do it manually if needed
314
- data_table <- as.data.table(x , key = key_vars )
315
- if (! identical(key_vars , key(data_table ))) setkeyv(data_table , cols = key_vars )
294
+ assert_true(identical(key(data_table ), c(" geo_value" , " time_value" , other_keys , " version" )))
295
+ validate_version_bound(clobberable_versions_start , data_table , na_ok = TRUE )
296
+ validate_version_bound(versions_end , data_table , na_ok = FALSE )
316
297
317
298
structure(
318
299
list (
@@ -523,11 +504,32 @@ as_epi_archive <- function(
523
504
.versions_end = max_version_with_row_in(x ), ... ,
524
505
versions_end = .versions_end ) {
525
506
assert_data_frame(x )
507
+ # Convert first to data.frame to guard against data.table#6859 and potentially
508
+ # other things epiprocess#618:
509
+ x_already_copied <- identical(class(x ), c(" data.table" , " data.frame" ))
510
+ x <- as.data.frame(x )
526
511
x <- rename(x , ... )
527
- x <- guess_column_name(x , " time_value" , time_column_names())
528
512
x <- guess_column_name(x , " geo_value" , geo_column_names())
513
+ if (! all(other_keys %in% names(x ))) {
514
+ # Give a more tailored error message than as.data.table would:
515
+ cli_abort(c(
516
+ " `x` is missing the following expected columns:
517
+ {format_varnames(setdiff(other_keys, names(x)))}." ,
518
+ " >" = " You might need to `dplyr::rename()` beforehand
519
+ or using `as_epi_archive()`'s renaming feature."
520
+ ))
521
+ }
522
+ x <- guess_column_name(x , " time_value" , time_column_names())
529
523
x <- guess_column_name(x , " version" , version_column_names())
530
524
525
+ # Convert to data.table:
526
+ key_vars <- c(" geo_value" , " time_value" , other_keys , " version" )
527
+ if (x_already_copied ) {
528
+ setDT(x , key = key_vars )
529
+ } else {
530
+ x <- as.data.table(x , key = key_vars )
531
+ }
532
+
531
533
if (lifecycle :: is_present(geo_type )) {
532
534
cli_warn(" epi_archive constructor argument `geo_type` is now ignored. Consider removing." )
533
535
}
@@ -548,11 +550,10 @@ as_epi_archive <- function(
548
550
cli_abort(' `compactify` must be `TRUE`, `FALSE`, or `"message"`' )
549
551
}
550
552
551
- data_table <- result $ DT
552
- key_vars <- key(data_table )
553
+ data_table <- result $ DT # probably just `x`, but take no chances
553
554
554
555
nrow_before_compactify <- nrow(data_table )
555
- # Runs compactify on data frame
556
+ # Runs compactify on data_table
556
557
if (identical(compactify , TRUE ) || identical(compactify , " message" )) {
557
558
compactified <- apply_compactify(data_table , key_vars , compactify_abs_tol )
558
559
} else {
0 commit comments