简体   繁体   中英

Why do I get an error when trying to use names_glue in tidyr::pivot_wider?

I get an unused argument error when trying to use the names_glue argument in tidyr::pivot_wider . Here's the example from the help page and my error:

library(tidyr)
us_rent_income %>%
  pivot_wider(
    names_from = variable,
    names_glue = "{variable}_{.value}",
    values_from = c(estimate, moe)
  )
Error in pivot_wider(., names_from = variable, names_glue = "{variable}_{.value}",  :
  unused argument (names_glue = "{variable}_{.value}")

sessionInfo()
R version 3.6.1 (2019-07-05)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: CentOS Linux 7 (Core)
...
attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
[1] tidyr_1.0.2

loaded via a namespace (and not attached):
 [1] tidyselect_1.0.0 compiler_3.6.1   magrittr_1.5     assertthat_0.2.1
 [5] R6_2.4.1         pillar_1.4.3     glue_1.4.1       dplyr_0.8.5
 [9] tibble_2.1.3     crayon_1.3.4     Rcpp_1.0.4       vctrs_0.2.4
[13] lifecycle_0.2.0  pkgconfig_2.0.3  rlang_0.4.5      purrr_0.3.3

I updated tidyr and glue .

I should note that without the names_glue argument, works just fine:

us_rent_income %>%
  pivot_wider(
    names_from = variable,
    names_sep = ".",
    values_from = c(estimate, moe)
  )

According to the changelog, it looks like pivot_wider() gained the names_glue argument in version 1.1.0. You're showing 1.0.2, right? Package 'tidyr' version 1.1.0 -also- Hadley's tidyr 1.1.0 blog announcement

This should solve your problem:

install.packages("tidyr")
library(tidyr)
us_rent_income %>%
  pivot_wider(
    names_from = variable,
    names_glue = "{variable}_{.value}",
    values_from = c(estimate, moe)
  )

Note, you may also have to update the vctrs package as well to version 0.3.0.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM