简体   繁体   English

编写 R package 时如何导入“%>%”?

[英]How do I import “%>%” when writing an R package?

I'm running into the following error when loading an R package I'm writing.加载我正在写的 R package 时遇到以下错误。

Error in nations %>% rvest::html_nodes(".x") %>% rvest::html_nodes(".y") %>%  : 
  could not find function "%>%"

I'm not sure how to import this in my R package.我不确定如何在我的 R package 中导入它。 This is how I have my function setup这就是我的 function 设置

 nations_url_odd<-nations %>%
    rvest::html_nodes('.x') %>%
    rvest::html_nodes('.y') %>%
    rvest::html_nodes('a')

Create a reexports.R file in your package with the following lines:在 package 中使用以下行创建reexports.R文件:

#' @importFrom magrittr %>%
#' @export
magrittr::`%>%`

This will make the pipe available to your package and also reexport it to users of your package, so when they load or attach your package the pipe will be available to them (they won't have to also load magrittr). This will make the pipe available to your package and also reexport it to users of your package, so when they load or attach your package the pipe will be available to them (they won't have to also load magrittr). This can be automated with usethis::use_pipe() (see https://usethis.r-lib.org/reference/use_pipe.html ).这可以通过usethis::use_pipe()自动化(参见https://usethis.r-lib.org/reference/use_pipe.html )。 As @user2554330 mentions below, this solution depends on the use of roxygen2 .正如@user2554330 下面提到的,这个解决方案取决于roxygen2的使用。

@Wil gives the best solution if you're a roxygen2 user.如果您是roxygen2用户,@Wil 会提供最佳解决方案。 If not, then as @ArtemSokolov said in a comment, add this line to your NAMESPACE file:如果没有,那么正如@ArtemSokolov 在评论中所说,将此行添加到您的NAMESPACE文件中:

importFrom(magrittr,"%>%")

If you also want users of your package to be able to use the pipe without a library(magrittr) call or similar, also add this line to NAMESPACE :如果您还希望 package 的用户能够在没有library(magrittr)调用或类似调用的情况下使用 pipe,请将此行添加到NAMESPACE

export("%>%")

You'll also need to make sure your DESCRIPTION file includes magrittr on the Imports: line, eg您还需要确保您的DESCRIPTION文件在Imports:行中包含magrittr ,例如

Imports: magrittr

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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