Skip to content

Commit 03843f8

Browse files
committed
Fix: Allows roll_elements() to work on non-numeric vectors/columns
1 parent 3d55a21 commit 03843f8

File tree

4 files changed

+173
-1
lines changed

4 files changed

+173
-1
lines changed

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11

2+
# rearrr 0.3.5.9000
3+
4+
* Fix: `roll_elements()` now works on non-numeric vectors/columns.
5+
26
# rearrr 0.3.5
37

48
* Replaces use of `dplyr::all_equal()` with `all.equal()`.

R/apply_coordinate_fn.R

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,16 @@ apply_coordinate_fn_ <- function(dim_vectors,
1313
dim_var_name,
1414
grp_id,
1515
allow_len_one = FALSE,
16+
allow_non_numerics = FALSE,
1617
extra_args = NULL) {
1718

1819
# Check arguments ####
1920
assert_collection <- checkmate::makeAssertCollection()
20-
checkmate::assert_list(dim_vectors, types = "numeric", any.missing = FALSE, min.len = 1, add = assert_collection)
21+
if (isTRUE(allow_non_numerics)){
22+
checkmate::assert_list(dim_vectors, any.missing = FALSE, min.len = 1, add = assert_collection)
23+
} else {
24+
checkmate::assert_list(dim_vectors, types = "numeric", any.missing = FALSE, min.len = 1, add = assert_collection)
25+
}
2126
checkmate::assert_numeric(coordinates, any.missing = FALSE, null.ok = TRUE, min.len = 1, add = assert_collection)
2227
checkmate::assert_function(fn, null.ok = TRUE, add = assert_collection)
2328
checkmate::assert_number(num_dims, lower = 1, add = assert_collection)

R/roll_elements.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ roll_elements_rearranger_method_ <- function(data,
231231
dim_var_name = "cols",
232232
grp_id = grp_id,
233233
allow_len_one = TRUE,
234+
allow_non_numerics = TRUE,
234235
extra_args = n_fn_args
235236
)
236237

tests/testthat/test_roll_elements.R

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,3 +1216,165 @@ test_that("fuzz testing roll_elements()", {
12161216

12171217

12181218
})
1219+
1220+
test_that("prev error cases in roll_elements()", {
1221+
1222+
1223+
## Testing 'roll_elements(as.character(1:10), n = 2)' ####
1224+
## Initially generated by xpectr
1225+
xpectr::set_test_seed(42)
1226+
# Assigning output
1227+
output_19148 <- roll_elements(as.character(1:10), n = 2)
1228+
# Testing class
1229+
expect_equal(
1230+
class(output_19148),
1231+
c("tbl_df", "tbl", "data.frame"),
1232+
fixed = TRUE)
1233+
# Testing column values
1234+
expect_equal(
1235+
output_19148[["Value"]],
1236+
c("3", "4", "5", "6", "7", "8", "9", "10", "1", "2"),
1237+
fixed = TRUE)
1238+
# Testing column names
1239+
expect_equal(
1240+
names(output_19148),
1241+
c("Value", ".n"),
1242+
fixed = TRUE)
1243+
# Testing column classes
1244+
expect_equal(
1245+
xpectr::element_classes(output_19148),
1246+
c("character", "list"),
1247+
fixed = TRUE)
1248+
# Testing column types
1249+
expect_equal(
1250+
xpectr::element_types(output_19148),
1251+
c("character", "list"),
1252+
fixed = TRUE)
1253+
# Testing dimensions
1254+
expect_equal(
1255+
dim(output_19148),
1256+
c(10L, 2L))
1257+
# Testing group keys
1258+
expect_equal(
1259+
colnames(dplyr::group_keys(output_19148)),
1260+
character(0),
1261+
fixed = TRUE)
1262+
## Finished testing 'roll_elements(as.character(1:10), n = 2)' ####
1263+
1264+
# Set seed
1265+
xpectr::set_test_seed(42)
1266+
1267+
df <- data.frame(
1268+
"x" = as.character(1:20),
1269+
"y" = 1:20,
1270+
"g" = rep(1:4, each = 5),
1271+
stringsAsFactors = FALSE
1272+
)
1273+
1274+
1275+
## Testing 'roll_elements(df, n = 1, cols = "x")' ####
1276+
## Initially generated by xpectr
1277+
xpectr::set_test_seed(42)
1278+
# Assigning output
1279+
output_19148 <- roll_elements(df, n = 1, cols = "x")
1280+
# Testing class
1281+
expect_equal(
1282+
class(output_19148),
1283+
c("tbl_df", "tbl", "data.frame"),
1284+
fixed = TRUE)
1285+
# Testing column values
1286+
expect_equal(
1287+
output_19148[["y"]],
1288+
c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
1289+
19, 20),
1290+
tolerance = 1e-4)
1291+
expect_equal(
1292+
output_19148[["g"]],
1293+
c(1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4),
1294+
tolerance = 1e-4)
1295+
expect_equal(
1296+
output_19148[["x"]],
1297+
c("2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13",
1298+
"14", "15", "16", "17", "18", "19", "20", "1"),
1299+
fixed = TRUE)
1300+
# Testing column names
1301+
expect_equal(
1302+
names(output_19148),
1303+
c("y", "g", "x", ".n"),
1304+
fixed = TRUE)
1305+
# Testing column classes
1306+
expect_equal(
1307+
xpectr::element_classes(output_19148),
1308+
c("integer", "integer", "character", "list"),
1309+
fixed = TRUE)
1310+
# Testing column types
1311+
expect_equal(
1312+
xpectr::element_types(output_19148),
1313+
c("integer", "integer", "character", "list"),
1314+
fixed = TRUE)
1315+
# Testing dimensions
1316+
expect_equal(
1317+
dim(output_19148),
1318+
c(20L, 4L))
1319+
# Testing group keys
1320+
expect_equal(
1321+
colnames(dplyr::group_keys(output_19148)),
1322+
character(0),
1323+
fixed = TRUE)
1324+
## Finished testing 'roll_elements(df, n = 1, cols = "x")' ####
1325+
1326+
1327+
1328+
## Testing 'roll_elements(dplyr::group_by(df, g), n = 1,...' ####
1329+
## Initially generated by xpectr
1330+
xpectr::set_test_seed(42)
1331+
# Assigning output
1332+
output_19148 <- roll_elements(dplyr::group_by(df, g), n = 1, cols = "x")
1333+
# Testing class
1334+
expect_equal(
1335+
class(output_19148),
1336+
c("tbl_df", "tbl", "data.frame"),
1337+
fixed = TRUE)
1338+
# Testing column values
1339+
expect_equal(
1340+
output_19148[["y"]],
1341+
c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
1342+
19, 20),
1343+
tolerance = 1e-4)
1344+
expect_equal(
1345+
output_19148[["g"]],
1346+
c(1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4),
1347+
tolerance = 1e-4)
1348+
expect_equal(
1349+
output_19148[["x"]],
1350+
c("2", "3", "4", "5", "1", "7", "8", "9", "10", "6", "12", "13",
1351+
"14", "15", "11", "17", "18", "19", "20", "16"),
1352+
fixed = TRUE)
1353+
# Testing column names
1354+
expect_equal(
1355+
names(output_19148),
1356+
c("y", "g", "x", ".n"),
1357+
fixed = TRUE)
1358+
# Testing column classes
1359+
expect_equal(
1360+
xpectr::element_classes(output_19148),
1361+
c("integer", "integer", "character", "list"),
1362+
fixed = TRUE)
1363+
# Testing column types
1364+
expect_equal(
1365+
xpectr::element_types(output_19148),
1366+
c("integer", "integer", "character", "list"),
1367+
fixed = TRUE)
1368+
# Testing dimensions
1369+
expect_equal(
1370+
dim(output_19148),
1371+
c(20L, 4L))
1372+
# Testing group keys
1373+
expect_equal(
1374+
colnames(dplyr::group_keys(output_19148)),
1375+
character(0),
1376+
fixed = TRUE)
1377+
## Finished testing 'roll_elements(dplyr::group_by(df, g), n = 1,...' ####
1378+
1379+
1380+
})

0 commit comments

Comments
 (0)