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

dm_paste(options = "tables") fails for factor col with too many levels #1510

Closed
TSchiefer opened this issue Sep 12, 2022 · 1 comment · Fixed by #1511
Closed

dm_paste(options = "tables") fails for factor col with too many levels #1510

TSchiefer opened this issue Sep 12, 2022 · 1 comment · Fixed by #1511

Comments

@TSchiefer
Copy link
Member

The problem is, that deparse_line() is using deparse() which has a maximum cutoff-width of 500 (this max is being used). If the result of deparse exceeds this limit, a vector of length > 1 is produced.
deparse_line() is being used in a map_chr() call on a table, so if the result for one of the columns is a vector of length > 1, an error is thrown.

suppressMessages(devtools::load_all("~/git/cynkra/dm"))
suppressPackageStartupMessages({
  library(dplyr)
  library(tibble)
})

factor_levels <- expand.grid(letters, as.character(1:5)) %>% transmute(x = paste0(Var1, Var2)) %>% pull()
factor_levels
#>   [1] "a1" "b1" "c1" "d1" "e1" "f1" "g1" "h1" "i1" "j1" "k1" "l1" "m1" "n1" "o1"
#>  [16] "p1" "q1" "r1" "s1" "t1" "u1" "v1" "w1" "x1" "y1" "z1" "a2" "b2" "c2" "d2"
#>  [31] "e2" "f2" "g2" "h2" "i2" "j2" "k2" "l2" "m2" "n2" "o2" "p2" "q2" "r2" "s2"
#>  [46] "t2" "u2" "v2" "w2" "x2" "y2" "z2" "a3" "b3" "c3" "d3" "e3" "f3" "g3" "h3"
#>  [61] "i3" "j3" "k3" "l3" "m3" "n3" "o3" "p3" "q3" "r3" "s3" "t3" "u3" "v3" "w3"
#>  [76] "x3" "y3" "z3" "a4" "b4" "c4" "d4" "e4" "f4" "g4" "h4" "i4" "j4" "k4" "l4"
#>  [91] "m4" "n4" "o4" "p4" "q4" "r4" "s4" "t4" "u4" "v4" "w4" "x4" "y4" "z4" "a5"
#> [106] "b5" "c5" "d5" "e5" "f5" "g5" "h5" "i5" "j5" "k5" "l5" "m5" "n5" "o5" "p5"
#> [121] "q5" "r5" "s5" "t5" "u5" "v5" "w5" "x5" "y5" "z5"

# `deparse_line()` is used in a map_chr() call which needs a length 1 vector as a result for each element
deparse_line(factor(levels = factor_levels))
#> [1] "structure(integer(0), class = \"factor\", levels = c(\"a1\", \"b1\", \"c1\", \"d1\", \"e1\", \"f1\", \"g1\", \"h1\", \"i1\", \"j1\", \"k1\", \"l1\", \"m1\", \"n1\", \"o1\", \"p1\", \"q1\", \"r1\", \"s1\", \"t1\", \"u1\", \"v1\", \"w1\", \"x1\", \"y1\", \"z1\", \"a2\", \"b2\", \"c2\", \"d2\", \"e2\", \"f2\", \"g2\", \"h2\", \"i2\", \"j2\", \"k2\", \"l2\", \"m2\", \"n2\", \"o2\", \"p2\", \"q2\", \"r2\", \"s2\", \"t2\", \"u2\", \"v2\", \"w2\", \"x2\", \"y2\", \"z2\", \"a3\", \"b3\", \"c3\", \"d3\", \"e3\", \"f3\", \"g3\", \"h3\", \"i3\", \"j3\", \"k3\", \"l3\", \"m3\", \"n3\", \"o3\", \"p3\", \"q3\", \"r3\", \"s3\", \"t3\", \"u3\", \"v3\", \"w3\", "
#> [2] "\"x3\", \"y3\", \"z3\", \"a4\", \"b4\", \"c4\", \"d4\", \"e4\", \"f4\", \"g4\", \"h4\", \"i4\", \"j4\", \"k4\", \"l4\", \"m4\", \"n4\", \"o4\", \"p4\", \"q4\", \"r4\", \"s4\", \"t4\", \"u4\", \"v4\", \"w4\", \"x4\", \"y4\", \"z4\", \"a5\", \"b5\", \"c5\", \"d5\", \"e5\", \"f5\", \"g5\", \"h5\", \"i5\", \"j5\", \"k5\", \"l5\", \"m5\", \"n5\", \"o5\", \"p5\", \"q5\", \"r5\", \"s5\", \"t5\", \"u5\", \"v5\", \"w5\", \"x5\", \"y5\", \"z5\"))"

dm(t1 = tibble(a = factor(levels = factor_levels))) %>% 
  dm_paste(options = "tables")
#> Error in `stop_bad_type()`:
#> ! Result 1 must be a single string, not a character vector of length 2

Created on 2022-09-12 by the reprex package (v2.0.1)

A possible solution could be to use map() instead of map_chr() and flatten the elements of the resulting list before working further with them.

@github-actions
Copy link
Contributor

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue and link to this old issue if necessary.

@github-actions github-actions bot locked and limited conversation to collaborators Sep 21, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging a pull request may close this issue.

1 participant