首页 文章

dplyr . 和_no全局变量的可见绑定'.' _在包检查中注意

提问于
浏览
5

在dplyr中,曾经可以编写像使用 ' . '引用管道中的数据

x <- data.frame(x = 2:4)
y <- data.frame(y = 1:3)

y %>% dplyr::bind_cols(x,.)

但是当在函数中使用它并运行包时,检查它会为全局变量' . '生成无可见的绑定 .

处理NOTE的最佳做法是什么?

3 回答

  • 3

    现在最好的做法是使用quosures . 这个其他SO帖子有一个很好的总结:How to evaluate a constructed string with non-standard evaluation using dplyr?

    在实践中,我刚刚将 . = NULL 包含在我的函数顶部 .

    EDIT

    正如@MrFlick所指出的那样,quosures赢得了't actually help in this case. You can feasibly use quosures to define column names etc. in a way that would allow you to avoid notes about non-standard evaluation in package functions (I haven' t,但是's on my to-do list for at least one of my packages), but you can'实际上使用了这个策略来使用 . 将值传递给指定的参数或位置 .

    值得指出的是,至少有some overhead with using pipes . 可能最佳做法是在包函数中根本不使用管道,这解决了使用 . 的问题 . 对于使用 dplyr 命令的NSE的其余部分,您可以使用quosures .

  • 1

    似乎最佳做法是使用 .data 而不是 . 然后使用 rlang 包中的import .data . 来自programming with dplyr vignette

    如果此函数在一个包中,使用.data也会阻止R CMD检查提供有关未定义全局变量的注释(假设您还使用@importFrom rlang .data导入了rlang :: . data) .

    不幸的是,这对于 dplyr::bind_cols 的原始问题不起作用,但它适用于 dplyr::mutatedplyr::do .

  • -1

    您发布的代码正在为我运行:

    > x <- data.frame(x = 2:4)
    > y <- data.frame(y = 1:3)
    > y %>% dplyr::bind_cols(x, .)
      x y
    1 2 1
    2 3 2
    3 4 3
    

    话虽如此,这里有两个产生类似但不同的输出的替代方案:

    > y %>% dplyr::bind_cols(x)
      y x
    1 1 2
    2 2 3
    3 3 4
    

    这里R看到 y 作为 x 的前一个参数,因为 dplyr::bind_cols 函数期望任意数量的数据帧作为它的第一个参数,因此看到你正在管道它并假设它应该是第一个 .

    > y %>% dplyr::bind_cols(x, { . })
      y x y1
    1 1 2  1
    2 2 3  2
    3 3 4  3
    

    . 不起作用的情况下,您也可以将管道变量称为 { . } . 但是,你可以在这里看到magrittr仍然将 y 作为第一个参数,并在我指定 { . } 的地方再次给出它 .

    因此,我认为magrittr将假设您的管道变量将在第一个可用的机会插入下一个函数,并且您可以再次启动它,或者如果它无法找到该变量的用途 { . } .

相关问题