Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix undefined behaviour in cartesian() #81

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '615880016'
ValidationKey: '615930987'
AcceptedWarnings:
- 'Warning: package ''.*'' was built under R version'
- 'Warning: namespace ''.*'' is not available and has been replaced'
Expand Down
4 changes: 2 additions & 2 deletions CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ cff-version: 1.2.0
message: If you use this software, please cite it using the metadata from this file.
type: software
title: 'quitte: Bits and pieces of code to use with quitte-style data frames'
version: 0.3128.2
date-released: '2023-11-27'
version: 0.3128.3
date-released: '2023-11-28'
abstract: A collection of functions for easily dealing with quitte-style data frames,
doing multi-model comparisons and plots.
authors:
Expand Down
4 changes: 2 additions & 2 deletions DESCRIPTION
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.3128.2
Date: 2023-11-27
Version: 0.3128.3
Date: 2023-11-28
Authors@R: c(
person("Michaja", "Pehl", , "[email protected]", role = c("aut", "cre")),
person("Nico", "Bauer", , "[email protected]", role = "aut"),
Expand Down
29 changes: 9 additions & 20 deletions R/cartesian.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#' Generate cartesian product from to character vectors
#' Generate Cartesian product from character vectors
#'
#' @param ... objects that can be coerced to character vectors
#' @param sep a character string that will separate the elements of `...` in the
Expand All @@ -14,25 +14,14 @@
#'
#' @export
cartesian <- function(..., sep = '.') {
dots <- list(...)
if (0 == length(dots <- list(...)))
return(NULL)

L <- lengths(dots)
v <- as.character(dots[[1]])
for (i in dots[-1])
v <- paste(rep(v, each = length(i)),
rep(as.character(i), times = length(v)),
sep = sep)

apply(
X = sapply(
X = seq_along(dots),
FUN = function(i) {
# lengths of dots elements before i (excluding)
times_slice <- L[seq_len(i - 1)]
# lengths of dot elements after i (excluding)
each_slice <- L[setdiff(seq_along(dots), seq_len(i))]

rep(rep(dots[[i]], times = prod(times_slice)),
each = prod(each_slice))
}
),
MARGIN = 1,
FUN = paste,
collapse = sep
)
return(v)
}
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Bits and pieces of code to use with quitte-style data frames

R package **quitte**, version **0.3128.2**
R package **quitte**, version **0.3128.3**

[![CRAN status](https://www.r-pkg.org/badges/version/quitte)](https://cran.r-project.org/package=quitte) [![R build status](https://github.com/pik-piam/quitte/workflows/check/badge.svg)](https://github.com/pik-piam/quitte/actions) [![codecov](https://codecov.io/gh/pik-piam/quitte/branch/master/graph/badge.svg)](https://app.codecov.io/gh/pik-piam/quitte) [![r-universe](https://pik-piam.r-universe.dev/badges/quitte)](https://pik-piam.r-universe.dev/builds)

Expand Down Expand Up @@ -47,7 +47,7 @@ In case of questions / problems please contact Michaja Pehl <michaja.pehl@pik-po

To cite package **quitte** in publications use:

Pehl M, Bauer N, Hilaire J, Levesque A, Luderer G, Schultes A, Dietrich J, Richters O (2023). _quitte: Bits and pieces of code to use with quitte-style data frames_. R package version 0.3128.2, <https://github.com/pik-piam/quitte>.
Pehl M, Bauer N, Hilaire J, Levesque A, Luderer G, Schultes A, Dietrich J, Richters O (2023). _quitte: Bits and pieces of code to use with quitte-style data frames_. R package version 0.3128.3, <https://github.com/pik-piam/quitte>.

A BibTeX entry for LaTeX users is

Expand All @@ -56,7 +56,7 @@ A BibTeX entry for LaTeX users is
title = {quitte: Bits and pieces of code to use with quitte-style data frames},
author = {Michaja Pehl and Nico Bauer and Jérôme Hilaire and Antoine Levesque and Gunnar Luderer and Anselm Schultes and Jan Philipp Dietrich and Oliver Richters},
year = {2023},
note = {R package version 0.3128.2},
note = {R package version 0.3128.3},
url = {https://github.com/pik-piam/quitte},
}
```
8 changes: 7 additions & 1 deletion tests/testthat/test-cartesian.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
test_that(
desc = 'cartesian() generates correct combinations',
desc = 'cartesian(...) generates correct combinations',
code = {
expect_equal(
object = cartesian(1:2),
Expand All @@ -18,3 +18,9 @@ test_that(
'2.3.7', '2.3.8', '2.3.9', '2.4.7', '2.4.8', '2.4.9',
'2.5.7', '2.5.8', '2.5.9', '2.6.7', '2.6.8', '2.6.9'))
})

test_that(
desc = 'cartesian() returns NULL',
code = {
expect_null(object = cartesian())
})