簡體   English   中英

編寫 R package 時如何導入“%>%”?

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

加載我正在寫的 R package 時遇到以下錯誤。

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

我不確定如何在我的 R package 中導入它。 這就是我的 function 設置

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

在 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). 這可以通過usethis::use_pipe()自動化(參見https://usethis.r-lib.org/reference/use_pipe.html )。 正如@user2554330 下面提到的,這個解決方案取決於roxygen2的使用。

如果您是roxygen2用戶,@Wil 會提供最佳解決方案。 如果沒有,那么正如@ArtemSokolov 在評論中所說,將此行添加到您的NAMESPACE文件中:

importFrom(magrittr,"%>%")

如果您還希望 package 的用戶能夠在沒有library(magrittr)調用或類似調用的情況下使用 pipe,請將此行添加到NAMESPACE

export("%>%")

您還需要確保您的DESCRIPTION文件在Imports:行中包含magrittr ,例如

Imports: magrittr

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM