定义自定义管道运算符时出现不明确的警告

2024-01-10

在我的过程中我需要执行很多dplyr::inner_joins。我想我可以为它定义一个自定义管道运算符,如所解释的here https://stackoverflow.com/questions/47475923/custom-pipe-to-silence-warnings:

library(tidyverse)
library(rlang)

df1 <- tibble(a = 1:10, b = 11:20)
df2 <- tibble(a = 1:10, c = 21:30)

`%J>%` <- function(lhs, rhs){
  inner_join(lhs, rhs)
}

df1 %J>% df2

这按预期工作,我得到:

Joining, by = "a"
# A tibble: 10 x 3
       a     b     c
   <int> <int> <int>
 1     1    11    21
 2     2    12    22
 3     3    13    23
 4     4    14    24
 5     5    15    25
 6     6    16    26
 7     7    17    27
 8     8    18    28
 9     9    19    29
10    10    20    30

但也有一个警告:

Warning message:
`chr_along()` is soft-deprecated as of rlang 0.2.0.
This warning is displayed once per session.

如果我不包括的话,情节就会变厚library(rlang)根本没有(在新会话中),在这种情况下我不会收到任何警告:

library(tidyverse)

df1 <- tibble(a = 1:10, b = 11:20)
df2 <- tibble(a = 1:10, c = 21:30)

`%J>%` <- function(lhs, rhs){
  inner_join(lhs, rhs)
}

df1 %J>% df2

显然我不必包括library(rlang)在这个例子中,但如果我这样做了——这就是一个奇怪的警告。它来自哪里以及如果我确实想包括的话如何避免它library(rlang)?

sessionInfo()
R version 3.5.1 (2018-07-02)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 17134)

Matrix products: default

locale:
[1] LC_COLLATE=English_Israel.1252  LC_CTYPE=English_Israel.1252    LC_MONETARY=English_Israel.1252 LC_NUMERIC=C                    LC_TIME=English_Israel.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] rlang_0.3.0.1   forcats_0.3.0   stringr_1.3.1   dplyr_0.7.6     purrr_0.2.5     readr_1.1.1     tidyr_0.8.1     tibble_1.4.2    ggplot2_3.1.0   tidyverse_1.2.1

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.19     cellranger_1.1.0 pillar_1.3.0     compiler_3.5.1   plyr_1.8.4       bindr_0.1.1      tools_3.5.1      packrat_0.4.9-3  jsonlite_1.5     lubridate_1.7.4  nlme_3.1-137    
[12] gtable_0.2.0     lattice_0.20-35  pkgconfig_2.0.2  cli_1.0.1        rstudioapi_0.8   haven_1.1.2      bindrcpp_0.2.2   withr_2.1.2      xml2_1.2.0       httr_1.3.1       hms_0.4.2       
[23] grid_3.5.1       tidyselect_0.2.4 glue_1.3.0       R6_2.2.2         fansi_0.3.0      readxl_1.1.0     modelr_0.1.2     magrittr_1.5     backports_1.1.2  scales_1.0.0     rvest_0.3.2     
[34] assertthat_0.2.0 colorspace_1.3-2 utf8_1.1.4       stringi_1.2.4    lazyeval_0.2.1   munsell_0.5.0    broom_0.5.0      crayon_1.3.4

根据你的描述,我会说如果你加载rlang作为tidyverse,(即只需加载tidyverse),那么 R 将使用这节经文的rlang这会在经文中自动更新。如果你加载tidyverse首先然后rlang,那么 R 将使用最后一次看到的,即您手动加载的。因此,如果您没有更新rlang手动然后它会发出警告。 如果您手动更新,问题应该会消失rlang.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

定义自定义管道运算符时出现不明确的警告 的相关文章

随机推荐