New eamples and tests using add_padding()

This commit is contained in:
Andreas Gammelgaard Damsbo 2023-09-05 14:11:00 -07:00
parent 8c65c5e13b
commit ec38ffa51b
3 changed files with 27 additions and 20 deletions

View File

@ -14,7 +14,7 @@
#' tail(chunks_of_n(seq_len(100),7),3)
#' tail(chunks_of_n(seq_len(100),7,even=TRUE),3)
#' ds <- data.frame(nm=paste0("Sub",
#' sprintf("%03s", rownames(stRoke::talos))),stRoke::talos)
#' add_padding(rownames(stRoke::talos))),stRoke::talos)
#' head(chunks_of_n(ds,7,pattern="Sub[0-9]{3}",label="grp"),2)
#' ## Please notice that no sorting is performed. This is on purpose to preserve
#' ## original sorting. If sorting is intended, try something like this:

View File

@ -28,7 +28,7 @@ Split to chunks of size n
tail(chunks_of_n(seq_len(100),7),3)
tail(chunks_of_n(seq_len(100),7,even=TRUE),3)
ds <- data.frame(nm=paste0("Sub",
sprintf("\%03s", rownames(stRoke::talos))),stRoke::talos)
add_padding(rownames(stRoke::talos))),stRoke::talos)
head(chunks_of_n(ds,7,pattern="Sub[0-9]{3}",label="grp"),2)
## Please notice that no sorting is performed. This is on purpose to preserve
## original sorting. If sorting is intended, try something like this:

View File

@ -6,22 +6,29 @@ test_that("chunks_of_n returns correct", {
use.names = FALSE), c(6, 6, 6, 6, 6))
# This is the example from the function, but I believe it fails in GitHub testing
# ds <- data.frame(nm = paste0("Sub",
# sprintf("%03s", rownames(stRoke::talos))),
# stRoke::talos)
ds <- data.frame(nm = paste0("Sub",rownames(stRoke::talos)),
ds <- data.frame(nm = paste0("Sub",
add_padding(rownames(stRoke::talos))),
stRoke::talos)
# ds <- data.frame(nm = paste0("Sub",rownames(stRoke::talos)),
# stRoke::talos)
expect_equal(head(names(chunks_of_n(ds, 7,
pattern = "Sub([0-9]+)", label = "grp")),
1),"grp-Sub38-Sub11")
pattern = "Sub[0-9]{3}", label = "grp")),
1),"grp-Sub038-Sub011")
expect_equal(
ds[order(as.numeric(rownames(stRoke::talos))), ] |>
ds[order(ds$nm),] |>
chunks_of_n(7, pattern = "Sub([0-9]+)", label = "grp") |>
head(1) |> names(),
"grp-Sub1-Sub20"
"grp-Sub001-Sub020"
)
expect_equal(
ds[order(ds$nm),] |>
chunks_of_n(7, pattern = "Sub[0-9]{3}", label = "grp") |>
head(1) |> names(),
"grp-Sub001-Sub020"
)
## Errors
@ -36,23 +43,23 @@ test_that("n_chunks returns correct", {
expect_equal(lengths(n_chunks(seq_len(30), 7, even = TRUE),
use.names = FALSE), rep(5,6))
# This is the example from the function, but I believe it fails in GitHub testing
# ds <- data.frame(nm = paste0("Sub",
# sprintf("%03s", rownames(stRoke::talos))),
# stRoke::talos)
ds <- data.frame(nm = paste0("Sub",rownames(stRoke::talos)),
## This is the example from the function, but I believe it fails in GitHub testing
ds <- data.frame(nm = paste0("Sub",
add_padding(rownames(stRoke::talos))),
stRoke::talos)
# ds <- data.frame(nm = paste0("Sub",rownames(stRoke::talos)),
# stRoke::talos)
expect_equal(head(names(n_chunks(ds, 7,
pattern = "Sub([0-9]+)", label = "grp")),
1),"grp-Sub38-Sub603")
1),"grp-Sub038-Sub603")
expect_equal(
ds[order(as.numeric(rownames(stRoke::talos))), ] |>
ds[order(ds$nm), ] |>
n_chunks(7, pattern = "Sub([0-9]+)", label = "grp") |>
head(1) |> names(),
"grp-Sub1-Sub72"
"grp-Sub001-Sub072"
)
## Errors