[英]R package not loading `Imports` packages
我正在開發一個包,該包的功能依賴於許多其他包中的dplyr
。 正如 H. Wickham 在他的R Packages書中所建議的那樣,我在Description
文件中的Imports
下包含了所有必要的包。
Imports:
apaTables,
data.table,
dplyr,
magrittr,
plyr,
rlang,
sjstats,
stats
然后在函數體中使用namespace
(這里不需要函數的細節;我想強調的是,我使用的是 Hadley 在他的書中推薦的推薦packagename::fun()
格式):
#'
#' @title Confidence intervals for Partial Eta Squared
#' @name partialeta_sq_ci
#' @author Indrajeet Patil
#'
#' @param lm_object stats::lm linear model object
#' @param conf.level Level of confidence for the confidence interval
#' @importFrom magrittr %>%
#' @export
partialeta_sq_ci <- function(lm_object, conf.level = 0.95) {
# get the linear model object and turn it into a matrix and turn row names into a variable called "effect"
# compute partial eta-squared for each effect
# add additional columns containing data and formula that was used to create these effects
x <-
dplyr::left_join(
# details from the anova results
x = data.table::setDT(x = as.data.frame(as.matrix(
stats::anova(object = lm_object)
)),
keep.rownames = "effect"),
# other information about the results (data and formula used, etc.)
y = data.table::setDT(x = as.data.frame(
cbind(
"effsize" = sjstats::eta_sq(
model = stats::anova(object = lm_object),
partial = TRUE
),
"data" = as.character(lm_object$call[3]),
"formula" = as.character(lm_object$call[2])
)
),
keep.rownames = "effect"),
# merge the two preceding pieces of information by the common element of Effect
by = "effect"
)
# create a new column for residual degrees of freedom
x$df2 <- x$Df[x$effect == "Residuals"]
# remove sum of squares columns since they will not be useful
x <-
x %>%
dplyr::select(.data = .,
-c(base::grep(pattern = "Sq", x = names(x))))
# remove NAs, which would remove the row containing Residuals (redundant at this point)
x <- na.omit(x)
# rename to something more meaningful and tidy
x <- plyr::rename(x = x,
replace = c("Df" = "df1",
"F value" = "F.value"))
# rearrange the columns
x <-
x[, c("F.value",
"df1",
"df2",
"effect",
"effsize",
"Pr(>F)",
"data",
"formula")]
# convert the effect into a factor
x$effect <- as.factor(x$effect)
# for each type of effect, compute partial eta-squared confidence intervals, which would return a list
ci_df <-
plyr::dlply(
.data = x,
.variables = .(effect),
.fun = function(data)
apaTables::get.ci.partial.eta.squared(
F.value = data$F.value,
df1 = data$df1,
df2 = data$df2,
conf.level = conf.level
)
)
# get elements from the effect size confidence intervals list into a neat dataframe
ci_df <-
plyr::ldply(
.data = ci_df,
.fun = function(x)
cbind("LL" = x[[1]],
"UL" = x[[2]])
)
# merge the dataframe containing effect sizes with the dataframe containing rest of the information
effsize_ci <- base::merge(x = x,
y = ci_df,
by = "effect")
# returning the final dataframe
return(effsize_ci)
}
但是當我構建包並使用該函數時,它給了我以下錯誤-
Error in x %>% dplyr::select(.data = ., -c(base::grep(pattern = "Sq", :
could not find function "%>%"
我究竟做錯了什么?
PS 如果您需要更多詳細信息,請訪問 GitHub 存儲庫: https : //github.com/IndrajeetPatil/ipmisc 相關功能: https : //github.com/IndrajeetPatil/ipmisc/blob/master/R/partialeta_sq_ci.R描述文件: https://github.com/IndrajeetPatil/ipmisc/blob/master/DESCRIPTION
總結一下,這里有幾個問題:
使用packagename::fun()
是一個不錯的選擇,但不適用於操作員。 尤其是對於管道( %>%
),使用例如函數代替它會破壞它的目的。
@importFrom
標簽比@import
更可取,因為它更窄更明確。 這兩者都會在調用roxygen2::roxygenize()
時影響 NAMESPACE 文件。 但是請注意, roxygen 不會與用戶定義的 NAMESPACE 文件混淆,因為通常情況下人們寧願自己手動處理它(例如,當包提供 S3 類和/或方法時),並且被 roxygen 覆蓋然后需要撤消。 刪除現有的 NAMESPACE 文件會讓 roxygen 重新創建它。 Roxygen 通常會在 NAMESPACE 文件中添加一行以識別是否應該更新它:
# 由 roxygen2 生成:請勿手動編輯
描述文件中的依賴項既不會被 roxygen 修改,也不會被 roxygen 添加到 NAMESPACE(請注意,這會導致完整的包導入,我們寧願通過@importFrom
避免@importFrom
)。 描述文件需要手動處理,確保Imports:部分涵蓋通過 NAMESPACE 使用的所有包,即通過@import
和@importFrom
,以及通過packagename::fun()
我在我的DESCRIPTION
文件中使用以下內容解決了這個問題:
Depends: tidyverse, rlang
這個負載magrittr
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.