-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #98 from orichters/growthrate
add reportDuplicates() function
- Loading branch information
Showing
14 changed files
with
133 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
Type: Package | ||
Package: quitte | ||
Title: Bits and pieces of code to use with quitte-style data frames | ||
Version: 0.3136.0 | ||
Date: 2024-08-15 | ||
Version: 0.3137.1 | ||
Date: 2024-08-19 | ||
Authors@R: c( | ||
person("Michaja", "Pehl", , "[email protected]", role = c("aut", "cre")), | ||
person("Nico", "Bauer", , "[email protected]", role = "aut"), | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#' Checks for duplicates in quitte object | ||
#' | ||
#' Finds duplicates in a quitte object and warns about them per model and scenario. | ||
#' Also warn if variables are identical but value or units differ. | ||
#' | ||
#' @param mifdata object that can be converted with as.quitte | ||
#' @param action if set to 'warn', a warning with duplicate variables is raised | ||
#' @importFrom dplyr filter mutate select | ||
#' @return only the data that is duplicated. Has 0 rows if everything is fine | ||
#' @export | ||
reportDuplicates <- function(mifdata, action = "warn") { | ||
mifdata <- as.quitte(mifdata) | ||
duplicates <- mifdata %>% | ||
group_by(!!!syms(setdiff(colnames(mifdata), c("unit", "value")))) %>% | ||
filter(n() > 1) %>% | ||
ungroup() %>% | ||
droplevels() %>% | ||
quitteSort() | ||
if (nrow(duplicates) > 0 && isTRUE(action == "warn")) { | ||
warning(paste(c( | ||
"Duplicated data found for:", | ||
duplicates %>% | ||
distinct(.data$model, .data$scenario, .data$variable, .data$unit) %>% | ||
group_by(.data$model, .data$scenario) %>% | ||
summarise( | ||
text = paste0( | ||
unique(.data$model), " ", unique(.data$scenario), ": ", | ||
paste0(.data$variable, " (", .data$unit, ")", collapse = ", ")), | ||
.groups = 'drop') %>% | ||
pull('text')), | ||
collapse = '\n ')) | ||
} | ||
return(invisible(duplicates)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
test_that("report duplicate works", { | ||
# really identical | ||
d <- data.frame(variable = "Emi", unit = "Mt CO2/yr", period = c(2020, 2020), value = 1) | ||
expect_warning(dupl <- reportDuplicates(d), | ||
"Emi") | ||
expect_true(nrow(dupl) > 0) | ||
# difference in unit only | ||
d <- data.frame(variable = "Emi", unit = c("Mt CO2/yr", "kt CO2/yr"), period = 2020, value = 1) | ||
expect_warning(dupl <- reportDuplicates(d), | ||
"Emi") | ||
expect_true(nrow(dupl) > 0) | ||
# difference in value and unit | ||
d <- data.frame(variable = "Emi", unit = c("Mt CO2/yr", "kt CO2/yr"), period = 2020, value = c(1, 2)) | ||
expect_warning(dupl <- reportDuplicates(d), | ||
"Emi") | ||
expect_true(nrow(dupl) > 0) | ||
# differs by variant is acceptable | ||
d <- data.frame(variable = "Emi", unit = "Mt CO2/yr", period = 2020, | ||
variant = c('A', 'B'), value = 1) | ||
expect_no_warning(dupl <- reportDuplicates(d)) | ||
expect_true(nrow(dupl) == 0) | ||
# everything fine | ||
d <- data.frame(variable = "Emi", unit = "Mt CO2/yr", period = 2020, value = 1) | ||
expect_no_warning(dupl <- reportDuplicates(d)) | ||
expect_true(nrow(dupl) == 0) | ||
# everything fine with exampledata | ||
expect_no_warning(dupl <- reportDuplicates(quitte_example_data)) | ||
expect_true(nrow(dupl) == 0) | ||
expect_no_warning(dupl <- reportDuplicates(quitte_example_dataAR6)) | ||
expect_true(nrow(dupl) == 0) | ||
}) |