定义自定义管道运算符时警告不明确 [英] Unclear warning when defining custom pipe operator

查看:31
本文介绍了定义自定义管道运算符时警告不明确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的过程中,我需要执行许多dplyr::inner_join.以为我可以按照此处的说明为其定义自定义管道运算符:

library(tidyverse)图书馆(rlang)df1 <- tibble(a = 1:10, b = 11:20)df2 <- tibble(a = 1:10, c = 21:30)`%J>%` <- 函数(lhs,rhs){内连接(lhs,rhs)}df1%J>%df2

这按预期工作,我得到:

<块引用>

加入,by="a"# 小费:10 x 3a b c<int><int><int>1 1 11 212 2 12 223 3 13 234 4 14 245 5 15 256 6 16 267 7 17 278 8 18 289 9 19 2910 10 20 30

但也有警告:

<块引用>

警告信息:`chr_along()` 从 rlang 0.2.0 开始被软弃用.此警告在每个会话中显示一次.

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

图书馆(tidyverse)df1 <- tibble(a = 1:10, b = 11:20)df2 <- tibble(a = 1:10, c = 21:30)`%J>%` <- 函数(lhs,rhs){内连接(lhs,rhs)}df1%J>%df2

显然,在这个例子中我根本不需要包含 library(rlang),但如果我这样做了 - 这是一个奇怪的警告.如果我确实想包含 library(rlang),它来自哪里以及如何避免它?

<块引用>

sessionInfo()R 版本 3.5.1 (2018-07-02)平台:x86_64-w64-mingw32/x64(64位)运行于:Windows 10 x64(内部版本 17134)矩阵产品:默认语言环境:[1] LC_COLLATE=English_Israel.1252 LC_CTYPE=English_Israel.1252 LC_MONETARY=English_Israel.1252 LC_NUMERIC=C LC_TIME=English_Israel.1252附带的基础包:[1] stats graphics grDevices utils datasets 方法基础其他附加包:[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 .1dyverse_1.2通过命名空间加载(而不是附加):[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_373.1[12] gtable_0.2.0lattice_0.20-35 pkgconfig_2.0.2 cli_1.0.1 rstudioapi_0.8haven_1.1.2 bindrcpp_0.2.2 withr_2.1.2 xml2_1.2.0 httr_1.3.1 hms_0.4.2.2[23] grid_3.5.1 tidyselect_0.2.4glue_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,问题应该会消失.

In my process I need to perform many dplyr::inner_joins. Thought I might define a custom pipe operator for it as explained here:

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

This works as expected and I get:

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

But then also a warning:

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

Plot thickens if I don't include library(rlang) at all (in a new session), in which case I get no warnings:

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

Obviously I don't have to include library(rlang) at all in this example, but if I did - this is one weird warning. Where is it coming from and how to avoid it if I did wanted to include 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

解决方案

From your description, I would say that If you load rlang as part of the tidyverse, (i.e. just load tidyverse), then R will use the verse's rlang which is automatically updated whithin the verse. If you load tidyverse first and then rlang, then R will use the last seen one, which is the one you loaded manually. Thus, If you did not update rlang manually then It will give the warning. The problem should go away If you manually update rlang.

这篇关于定义自定义管道运算符时警告不明确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆