tidyverse / magrittr

Improve the readability of R code with the pipe
https://magrittr.tidyverse.org
Other
957 stars 157 forks source link

Error in split_chain for latest magrittr version #238

Closed aaron-horowitz closed 2 years ago

aaron-horowitz commented 3 years ago

Hi folks,

I had originally thought this issue was specific to just me so I didn't file an issue report, but I see based on this twitter thread I am not alone.

The error appears to occur with the latest version of magrittr and an internal function magrittr:::split_chain. If I force install v1.5 via devtools::install_github('tidyverse/magrittr@v1.5') the issue is resolved.

I'm struggling to come up with a reprex for this, but if I try to use a pipe on something as simple as below in the context of an existing function within a package (where the package accesses the pipe via %>% = magrittr::%>%` in a utils.R file):

c("a","b","c") %>% purrr::map(print)
Error in split_chain(match.call(), env = env) : 
  could not find function "split_chain"

While browsing within that function, the %>% is clearly referencing the old version of the pipe:

function (lhs, rhs) 
{
    parent <- parent.frame()
    env <- new.env(parent = parent)
    chain_parts <- split_chain(match.call(), env = env)
    pipes <- chain_parts[["pipes"]]
    rhss <- chain_parts[["rhss"]]
    lhs <- chain_parts[["lhs"]]
    env[["_function_list"]] <- lapply(1:length(rhss), function(i) wrap_function(rhss[[i]], 
        pipes[[i]], parent))
    env[["_fseq"]] <- `class<-`(eval(quote(function(value) freduce(value, 
        `_function_list`)), env, env), c("fseq", "function"))
    env[["freduce"]] <- freduce
    if (is_placeholder(lhs)) {
        env[["_fseq"]]
    }
    else {
        env[["_lhs"]] <- eval(lhs, parent, parent)
        result <- withVisible(eval(quote(`_fseq`(`_lhs`)), env, 
            env))
        if (is_compound_pipe(pipes[[1L]])) {
            eval(call("<-", lhs, result[["value"]]), parent, 
                parent)
        }
        else {
            if (result[["visible"]]) 
                result[["value"]]
            else invisible(result[["value"]])
        }
    }
}
<bytecode: 0x562bfab03748>
<environment: 0x562bfab045f0>

While outside of debug / the package itself, assigning %>% = magrittr::%>% produces the new version version of the pipe correctly.

Session info in details:

─ Session info ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── setting value version R version 3.6.1 (2019-07-05) os Ubuntu 18.04.5 LTS system x86_64, linux-gnu ui RStudio language (EN) collate en_US.UTF-8 ctype en_US.UTF-8 tz America/New_York date 2020-12-12 ─ Packages ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── package * version date lib source assertthat 0.2.1 2019-03-21 [1] CRAN (R 3.6.1) backports 1.1.8 2020-06-17 [1] CRAN (R 3.6.1) cli 2.2.0 2020-11-20 [1] CRAN (R 3.6.1) clisymbols 1.2.0 2017-05-21 [1] CRAN (R 3.6.1) crayon 1.3.4 2017-09-16 [1] CRAN (R 3.6.1) digest 0.6.25 2020-02-23 [1] CRAN (R 3.6.1) dplyr 1.0.2 2020-08-18 [1] CRAN (R 3.6.1) ellipsis 0.3.1 2020-05-15 [1] CRAN (R 3.6.1) evaluate 0.14 2019-05-28 [1] CRAN (R 3.6.1) fansi 0.4.1 2020-01-08 [1] CRAN (R 3.6.1) fcuk * 0.1.3.1 2020-01-11 [1] Github (ThinkR-open/fcuk@49e4e27) fs 1.5.0 2020-07-31 [1] CRAN (R 3.6.1) generics 0.1.0 2020-10-31 [1] CRAN (R 3.6.1) glue 1.4.1 2020-05-13 [1] CRAN (R 3.6.1) htmltools 0.5.0 2020-06-16 [1] CRAN (R 3.6.1) httr 1.4.2 2020-07-20 [1] CRAN (R 3.6.1) jsonlite 1.7.0 2020-06-25 [1] CRAN (R 3.6.1) knitr 1.29 2020-06-23 [1] CRAN (R 3.6.1) lifecycle 0.2.0 2020-03-06 [1] CRAN (R 3.6.1) magrittr 2.0.1 2020-11-17 [1] CRAN (R 3.6.1) memuse 4.0-0 2017-11-10 [1] CRAN (R 3.6.1) paletti 0.1.0 2019-09-20 [1] Github (EdwinTh/paletti@27e9a27) pillar 1.4.6 2020-07-10 [1] CRAN (R 3.6.1) pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 3.6.1) prompt 1.0.0 2020-01-11 [1] Github (gaborcsardi/prompt@b332c42) purrr 0.3.4 2020-04-17 [1] CRAN (R 3.6.1) R6 2.4.1 2019-11-12 [1] CRAN (R 3.6.1) rlang 0.4.9 2020-11-26 [1] CRAN (R 3.6.1) rmarkdown 2.3 2020-06-18 [1] CRAN (R 3.6.1) rstudioapi 0.13 2020-11-12 [1] CRAN (R 3.6.1) sessioninfo 1.1.1 2018-11-05 [1] CRAN (R 3.6.1) stringdist 0.9.5.5 2019-10-21 [1] CRAN (R 3.6.1) stringi 1.4.6 2020-02-17 [1] CRAN (R 3.6.1) stringr 1.4.0 2019-02-10 [1] CRAN (R 3.6.1) styler 1.3.2 2020-02-23 [1] CRAN (R 3.6.1) tibble 3.0.4 2020-10-12 [1] CRAN (R 3.6.1) tidyselect 1.1.0 2020-05-11 [1] CRAN (R 3.6.1) vctrs 0.3.2 2020-07-15 [1] CRAN (R 3.6.1) whoami 1.3.0 2019-03-19 [1] CRAN (R 3.6.1) withr 2.2.0 2020-04-20 [1] CRAN (R 3.6.1) xfun 0.15 2020-06-21 [1] CRAN (R 3.6.1)

I'm hoping someone else who's having this issue can provide a cleaner reprex.

aaron-horowitz commented 3 years ago

https://twitter.com/Andrew___Baker/status/1337874195783413760?s=20

hadley commented 3 years ago

Could you please make a reprex with the reprex package, just to make sure there's nothing else going on in your session?

And are you restarting your R session after installing magrittr?

alexeyza commented 3 years ago

Having the same issue as @aaron-horowitz. Not sure I can give a better reprex atm (sorry).

I did restart R-Studio after installing the package, same issue.

Until it is fixed, there are two workarounds for anyone encountering this now: Revert to older magrittr version with:

devtools::install_version("magrittr", version = "1.5.0", repos = "http://cran.us.r-project.org")

or, Manually assign %>%:

`%>%` <- magrittr::`%>%\`
session info

R version 4.0.2 (2020-06-22) Platform: x86_64-pc-linux-gnu (64-bit) Running under: Ubuntu 20.10 Matrix products: default BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.9.0 LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.9.0 locale: [1] LC_CTYPE=en_IL.UTF-8 LC_NUMERIC=C LC_TIME=en_IL.UTF-8 LC_COLLATE=en_IL.UTF-8 [5] LC_MONETARY=en_IL.UTF-8 LC_MESSAGES=en_IL.UTF-8 LC_PAPER=en_IL.UTF-8 LC_NAME=en_IL.UTF-8 [9] LC_ADDRESS=en_IL.UTF-8 LC_TELEPHONE=en_IL.UTF-8 LC_MEASUREMENT=en_IL.UTF-8 LC_IDENTIFICATION=en_IL.UTF-8 attached base packages: [1] grid stats graphics grDevices utils datasets methods base other attached packages: [1] shinydashboardPlus_0.7.5 shinyWidgets_0.5.4 shinydashboard_0.7.1 openxlsx_4.2.3 [5] httr_1.4.2 jsonlite_1.7.2 e1071_1.7-4 DMwR_0.4.1 [9] AUC_0.3.0 caret_6.0-86 lattice_0.20-41 glmnet_4.0-2 [13] Matrix_1.2-18 lsa_0.73.2 text2vec_0.6 SnowballC_0.7.0 [17] qdap_2.4.3 RColorBrewer_1.1-2 qdapTools_1.3.5 qdapRegex_0.7.2 [21] qdapDictionaries_1.0.7 tm_0.7-8 NLP_0.2-1 d3Tree_0.2.2 [25] shinyPagerUI_0.2.0 tidytext_0.2.6 highcharter_0.8.2 vip_0.2.2 [29] ggthemes_4.2.0 pool_0.1.5 odbc_1.3.0 htmlTable_2.1.0 [33] compareDF_2.3.0 DT_0.16 forcats_0.5.0 stringr_1.4.0 [37] dplyr_1.0.2 purrr_0.3.4 readr_1.4.0 tidyr_1.1.2 [41] tibble_3.0.4 tidyverse_1.3.0 rsample_0.0.8 caTools_1.18.0 [45] wesanderson_0.3.6 anytime_0.3.9 waffle_0.7.0 ggplot2_3.3.2 [49] config_0.3 shiny_1.5.0 loaded via a namespace (and not attached): [1] reticulate_1.18 tidyselect_1.1.0 htmlwidgets_1.5.3 pROC_1.16.2 devtools_2.3.2 [6] munsell_0.5.0 codetools_0.2-16 chron_2.3-56 future_1.21.0 withr_2.3.0 [11] colorspace_2.0-0 knitr_1.30 rstudioapi_0.13 ROCR_1.0-11 stats4_4.0.2 [16] tensorflow_2.2.0 rJava_0.9-13 Rttf2pt1_1.3.8 TTR_0.24.2 listenv_0.8.0 [21] slam_0.1-48 lgr_0.4.1 openNLPdata_1.5.3-4 bit64_4.0.5 rprojroot_2.0.2 [26] parallelly_1.22.0 vctrs_0.3.5 generics_0.1.0 ipred_0.9-9 xfun_0.19 [31] float_0.2-4 rlist_0.4.6.1 R6_2.5.0 bitops_1.0-6 assertthat_0.2.1 [36] promises_1.1.1 scales_1.1.1 nnet_7.3-14 gtable_0.3.0 globals_0.14.0 [41] processx_3.4.5 timeDate_3043.102 rlang_0.4.9 splines_4.0.2 extrafontdb_1.0 [46] ModelMetrics_1.2.2.2 wordcloud_2.6 broom_0.7.2 checkmate_2.0.0 abind_1.4-5 [51] yaml_2.2.1 reshape2_1.4.4 modelr_0.1.8 crosstalk_1.1.0.1 backports_1.2.1 [56] rsconnect_0.8.16 httpuv_1.5.4 quantmod_0.4.18 tokenizers_0.2.1 extrafont_0.17 [61] lava_1.6.8.1 tools_4.0.2 usethis_2.0.0 ellipsis_0.3.1 sessioninfo_1.1.1 [66] Rcpp_1.0.5 plyr_1.8.6 base64enc_0.1-3 RCurl_1.98-1.2 ps_1.5.0 [71] prettyunits_1.1.1 rpart_4.1-15 openNLP_0.2-7 zoo_1.8-8 haven_2.3.1 [76] fs_1.5.0 furrr_0.2.1 magrittr_2.0.1 data.table_1.13.4 reprex_0.3.0 [81] RBERT_0.1.11 mlapi_0.1.0 whisker_0.4 pkgload_1.1.0 hms_0.5.3 [86] mime_0.9 xtable_1.8-4 RhpcBLASctl_0.20-137 XML_3.99-0.5 readxl_1.3.1 [91] gridExtra_2.3 shape_1.4.5 tfruns_1.4 testthat_3.0.0 compiler_4.0.2 [96] crayon_1.3.4 gender_0.5.4 htmltools_0.5.0 later_1.1.0.1 venneuler_1.1-0 [101] lubridate_1.7.9.2 DBI_1.1.0 dbplyr_2.0.0 MASS_7.3-52 cli_2.2.0 [106] gower_0.2.2 parallel_4.0.2 igraph_1.2.6 pkgconfig_2.0.3 rsparse_0.4.0 [111] recipes_0.1.15 xml2_1.3.2 foreach_1.5.1 prodlim_2019.11.13 rvest_0.3.6 [116] janeaustenr_0.1.5 callr_3.5.1 digest_0.6.27 cellranger_1.1.0 curl_4.3 [121] nlme_3.1-149 lifecycle_0.2.0 desc_1.2.0 fansi_0.4.1 pillar_1.4.7 [126] fastmap_1.0.1 plotrix_3.7-8 pkgbuild_1.1.0 survival_3.2-3 glue_1.4.2 [131] xts_0.12.1 remotes_2.2.0 zip_2.1.1 iterators_1.0.13 bit_4.0.4 [136] class_7.3-17 stringi_1.5.3 blob_1.2.1 memoise_1.1.0

hadley commented 3 years ago

@alexeyza can you please use https://reprex.tidyverse.org to produce a reprex? Or at least include the code that is giving you the error? I would like to figure out the root cause of the issue before recommending fixes.

alexeyza commented 3 years ago

While creating a reprex, I noticed that it is probably happening due to masking (by qdap in my case). I only noticed this masking now. I wonder why wasn't this an issue in magrittr 1.5.0. Loading qdap first and then tidyverse seems to not have this issue.

I'll need to figure out a way to maintain order of loading packages (when involving a multi-module Shiny app), and make sure qdap is loaded before tidyverse. At the moment, using global.R to guarantee qadp is loaded early on.

I wonder if this is the same for @aaron-horowitz

Here's the reprex:

iris %>% group_by(Species) %>% mutate(avg = mean(Sepal.Length))
#> Error in iris %>% group_by(Species) %>% mutate(avg = mean(Sepal.Length)): could not find function "%>%"

library(tidyverse)
iris %>% group_by(Species) %>% mutate(avg = mean(Sepal.Length))
#> # A tibble: 150 x 6
#> # Groups:   Species [3]
#>    Sepal.Length Sepal.Width Petal.Length Petal.Width Species   avg
#>           <dbl>       <dbl>        <dbl>       <dbl> <fct>   <dbl>
#>  1          5.1         3.5          1.4         0.2 setosa   5.01
#>  2          4.9         3            1.4         0.2 setosa   5.01
#>  3          4.7         3.2          1.3         0.2 setosa   5.01
#>  4          4.6         3.1          1.5         0.2 setosa   5.01
#>  5          5           3.6          1.4         0.2 setosa   5.01
#>  6          5.4         3.9          1.7         0.4 setosa   5.01
#>  7          4.6         3.4          1.4         0.3 setosa   5.01
#>  8          5           3.4          1.5         0.2 setosa   5.01
#>  9          4.4         2.9          1.4         0.2 setosa   5.01
#> 10          4.9         3.1          1.5         0.1 setosa   5.01
#> # … with 140 more rows

library(qdap)
#> Loading required package: qdapDictionaries
#> Loading required package: qdapRegex
#> 
#> Attaching package: 'qdapRegex'
#> The following object is masked from 'package:dplyr':
#> 
#>     explain
#> The following object is masked from 'package:ggplot2':
#> 
#>     %+%
#> Loading required package: qdapTools
#> 
#> Attaching package: 'qdapTools'
#> The following object is masked from 'package:dplyr':
#> 
#>     id
#> Loading required package: RColorBrewer
#> 
#> Attaching package: 'qdap'
#> The following object is masked from 'package:forcats':
#> 
#>     %>%
#> The following object is masked from 'package:stringr':
#> 
#>     %>%
#> The following object is masked from 'package:dplyr':
#> 
#>     %>%
#> The following object is masked from 'package:purrr':
#> 
#>     %>%
#> The following object is masked from 'package:tidyr':
#> 
#>     %>%
#> The following object is masked from 'package:tibble':
#> 
#>     %>%
#> The following objects are masked from 'package:base':
#> 
#>     Filter, proportions
iris %>% group_by(Species) %>% mutate(avg = mean(Sepal.Length))
#> Error in split_chain(match.call(), env = env): could not find function "split_chain"

Created on 2020-12-13 by the reprex package (v0.3.0)

hadley commented 3 years ago

You’ll notice that there are a bunch of warning about masking %>% — the root cause is probably that qdap copied in the source of %>% rather than re-exporting it.

aaron-horowitz commented 3 years ago

I see this as the way qdap uses the pipe. What is appropriate way to re-export %>%?

https://github.com/trinker/qdap/blob/master/R/chaining.R

On Sun, Dec 13, 2020 at 10:43 AM Hadley Wickham notifications@github.com wrote:

You’ll notice that there are a bunch of warning about masking %>% — the root cause is probably that qdap copied in the source of %>% rather than re-exporting it.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/tidyverse/magrittr/issues/238#issuecomment-744050446, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA2OABRE5LBFHT3KVEIPRIDSUUDO7ANCNFSM4UYXYCDA .

aaron-horowitz commented 3 years ago

Could you please make a reprex with the reprex package, just to make sure there's nothing else going on in your session?

And are you restarting your R session after installing magrittr?

Yes re restarting R session after installing magrittr. I couldn't figure out how to make a reprex with the reprex package because I can only get the error to occur when I'm inside a package function (internal package, happy to share with you Hadley but it's not pretty enough to open source). I don't re-export the pipe in the package, I just use it internally.

lionel- commented 3 years ago

Hi everyone,

jennybc commented 3 years ago

Here are some general resources that seem relevant to what went wrong here:

aaron-horowitz commented 3 years ago

Thank you! I suspect other package others made the <- assignment incorrectly, so I hope this thread is a useful reference when it comes up for others.