From 85b84915e71f473d441ebf38e34029f290332d81 Mon Sep 17 00:00:00 2001 From: nikosbosse Date: Mon, 20 Nov 2023 14:04:22 +0100 Subject: [PATCH 1/2] make input of set_forecast_unit() a data.table --- DESCRIPTION | 2 +- NEWS.md | 6 ++++++ R/convenience-functions.R | 2 +- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 3e876aaf7..2fabe6a00 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: scoringutils Title: Utilities for Scoring and Assessing Predictions -Version: 1.2.1 +Version: 1.2.2 Language: en-GB Authors@R: c( person(given = "Nikos", diff --git a/NEWS.md b/NEWS.md index eb47b1cb4..4f35ea6d5 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,9 @@ +# scoringutils 1.2.2 + +## Bug fixes +- fixes a bug with `set_forecast_unit()` where the function only workded with a data.table, but not a data.frame as an input. + + # scoringutils 1.2.1 ## Package updates diff --git a/R/convenience-functions.R b/R/convenience-functions.R index f21aeefc8..41592d7d4 100644 --- a/R/convenience-functions.R +++ b/R/convenience-functions.R @@ -235,7 +235,7 @@ log_shift <- function(x, offset = 0, base = exp(1)) { #' ) set_forecast_unit <- function(data, forecast_unit) { - + data <- as.data.table(data) datacols <- colnames(data) missing <- forecast_unit[!(forecast_unit %in% datacols)] From a1bd8a93c09f9f13fbe7bd4d916b2f593aac9f37 Mon Sep 17 00:00:00 2001 From: nikosbosse Date: Sat, 25 Nov 2023 13:05:29 +0100 Subject: [PATCH 2/2] Add a unit test for an input that's not data.table --- tests/testthat/test-convenience-functions.R | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/testthat/test-convenience-functions.R b/tests/testthat/test-convenience-functions.R index ad7a40550..4fea72fa9 100644 --- a/tests/testthat/test-convenience-functions.R +++ b/tests/testthat/test-convenience-functions.R @@ -82,3 +82,20 @@ test_that("function get_forecast_unit() and set_forecast_unit() work together", expect_equal(fu_set, fu_get) }) + +test_that("set_forecast_unit() works on input that's not a data.table", { + df <- data.frame( + a = 1:2, + b = 2:3, + c = 3:4 + ) + expect_equal( + colnames(set_forecast_unit(df, c("a", "b"))), + c("a", "b") + ) + # apparently it also works on a matrix... good to know :) + expect_equal( + names(set_forecast_unit(as.matrix(df), "a")), + "a" + ) +})