poissonconsulting / term

An R package to manipulate the names of parameters terms
https://poissonconsulting.github.io/term/
Other
10 stars 1 forks source link

What is the best way to deal with `levels.term()` not supported? #40

Closed joethorley closed 4 years ago

joethorley commented 4 years ago
rbind(data.frame(term = term::term(x = 1)), data.frame(term = term::term(x = 1)))
#> Error: `levels.term()` not supported.

Created on 2020-02-04 by the reprex package (v0.3.0)

joethorley commented 4 years ago

If I define level.term I end up with a factor whether or not I've set stringsAsFactors = FALSE

levels.term <- function(x) unique(as.character(x))
x <- rbind(data.frame(term = term::term(x = 1)), data.frame(term = term::term(x = 1)), stringsAsFactors = FALSE)
class(x$term)
#> [1] "factor"
krlmlr commented 4 years ago

This is not what I see:

rbind(
  data.frame(term = term::term(x = 1)),
  data.frame(term = term::term(x = 1))
)
#>   term
#> 1    x
#> 2    x

Created on 2020-02-06 by the reprex package (v0.3.0)

Could you please post a reprex?

krlmlr commented 4 years ago

Rerunning your reprex with session info:

rbind(data.frame(term = term::term(x = 1)), data.frame(term = term::term(x = 1)))
#>   term
#> 1    x
#> 2    x

Created on 2020-02-06 by the reprex package (v0.3.0)

Session info ``` r devtools::session_info() #> ─ Session info ─────────────────────────────────────────────────────────────── #> setting value #> version R version 3.6.2 (2019-12-12) #> os Ubuntu 18.04.3 LTS #> system x86_64, linux-gnu #> ui X11 #> language (EN) #> collate en_US.UTF-8 #> ctype en_US.UTF-8 #> tz Europe/Zurich #> date 2020-02-06 #> #> ─ Packages ─────────────────────────────────────────────────────────────────── #> package * version date lib source #> assertthat 0.2.1 2019-03-21 [2] CRAN (R 3.6.2) #> backports 1.1.5 2019-11-30 [1] Github (mllg/backports@8ad3061) #> callr 3.4.1 2020-01-24 [2] CRAN (R 3.6.2) #> chk 0.3.1.9000 2020-02-06 [1] local #> cli 2.0.1 2020-01-08 [2] CRAN (R 3.6.2) #> crayon 1.3.4 2017-09-16 [2] CRAN (R 3.6.2) #> desc 1.2.0 2018-05-01 [2] CRAN (R 3.6.2) #> devtools 2.2.1 2019-09-24 [2] CRAN (R 3.6.2) #> digest 0.6.23 2019-11-23 [2] CRAN (R 3.6.2) #> ellipsis 0.3.0 2019-09-20 [2] CRAN (R 3.6.2) #> evaluate 0.14 2019-05-28 [2] CRAN (R 3.6.2) #> extras 0.0.0.9008 2020-02-06 [1] local #> fansi 0.4.1 2020-01-12 [1] Github (brodieG/fansi@7ccb892) #> fs 1.3.1 2019-05-06 [2] CRAN (R 3.6.2) #> glue 1.3.1.9000 2020-02-02 [1] Github (tidyverse/glue@680bb60) #> highr 0.8 2019-03-20 [2] CRAN (R 3.6.2) #> htmltools 0.4.0 2019-10-04 [2] CRAN (R 3.6.2) #> knitr 1.27.2 2020-02-02 [1] Github (yihui/knitr@ab191b0) #> lifecycle 0.1.0.9000 2020-01-02 [1] local #> magrittr 1.5.0.9000 2019-11-30 [1] Github (tidyverse/magrittr@496ff02) #> memoise 1.1.0 2017-04-21 [2] CRAN (R 3.6.2) #> pkgbuild 1.0.6 2019-10-09 [2] CRAN (R 3.6.2) #> pkgload 1.0.2 2018-10-29 [2] CRAN (R 3.6.2) #> prettyunits 1.1.1 2020-01-24 [1] CRAN (R 3.6.2) #> processx 3.4.1 2019-07-18 [2] CRAN (R 3.6.2) #> ps 1.3.0 2018-12-21 [2] CRAN (R 3.6.2) #> R6 2.4.1 2019-11-12 [2] CRAN (R 3.6.2) #> Rcpp 1.0.3.5 2019-12-19 [1] local #> remotes 2.1.0.9000 2019-11-30 [1] local #> reprex 0.3.0 2019-05-16 [2] CRAN (R 3.6.2) #> rlang 0.4.4 2020-01-28 [1] CRAN (R 3.6.2) #> rmarkdown 2.1 2020-01-20 [2] CRAN (R 3.6.2) #> rprojroot 1.3.2.9000 2019-11-30 [1] Github (r-lib/rprojroot@4e4f70c) #> sessioninfo 1.1.1 2018-11-05 [2] CRAN (R 3.6.2) #> stringi 1.4.5 2020-01-11 [2] CRAN (R 3.6.2) #> stringr 1.4.0 2019-02-10 [2] CRAN (R 3.6.2) #> term 0.1.0.9005 2020-02-06 [1] local #> testthat 2.3.1 2019-11-30 [1] Github (r-lib/testthat@3633dfe) #> universals 0.0.0.9004 2020-02-06 [1] local #> usethis 1.5.1.9000 2020-02-02 [1] local #> vctrs 0.2.99.9005 2020-02-06 [1] local #> withr 2.1.2 2018-03-15 [2] CRAN (R 3.6.2) #> xfun 0.12 2020-01-13 [2] CRAN (R 3.6.2) #> yaml 2.2.1 2020-02-01 [1] CRAN (R 3.6.2) #> #> [1] /home/kirill/R-dev #> [2] /home/kirill/R/x86_64-pc-linux-gnu-library/3.6 #> [3] /usr/local/lib/R/site-library #> [4] /usr/lib/R/site-library #> [5] /usr/lib/R/library ```

Maybe the dev version of vctrs does the magic?

joethorley commented 4 years ago

It works because I had added

#' @export
levels.term <- function(x) unique(as.character(x))

If you go to the broken-rbind-demo branch you will get

rbind(
  data.frame(term = term::term(x = 1)),
  data.frame(term = term::term(x = 1))
)
#> Error: `levels.term()` not supported.

Created on 2020-02-06 by the reprex package (v0.3.0)

with

Backtrace:
    █
 1. └─base::rbind(data.frame(term = term::term(x = 1)), data.frame(term = term::term(x = 1)))
 2.   └─base::rbind(deparse.level, ...)
 3.     ├─[ base::levels(...) ]
 4.     └─vctrs:::levels.vctrs_vctr(xj)
 5.       └─vctrs:::stop_unsupported(x, "levels")
 6.         └─vctrs:::stop_vctrs(...)

The hack is a pain because rbind turns the term vector into a factor

dput(rbind(
  data.frame(term = term::term(x = 1)),
  data.frame(term = term::term(x = 1))
))
#> structure(list(term = structure(c(1L, 1L), .Label = "x", class = "factor")), row.names = c(NA, 
#> -2L), class = "data.frame")

Created on 2020-02-06 by the reprex package (v0.3.0)

I get the same result with the development branch of vctrs.