gpt4 book ai didi

R 包未加载 `Imports` 包

转载 作者:行者123 更新时间:2023-12-04 16:06:20 30 4
gpt4 key购买 nike

我正在开发一个包,该包的功能依赖于许多其他包中的 dplyr。正如 H. Wickham 在他的 R Packages 一书中所建议的那样,我在 Description 文件中的 Imports 下包含了所有必要的包。

Imports:
apaTables,
data.table,
dplyr,
magrittr,
plyr,
rlang,
sjstats,
stats

然后在函数体中使用namespace(函数的细节这里就不用说了,我想强调的是我用的是推荐的packagename::fun() Hadley 在他的书中推荐的格式):

#'
#' @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 "%>%"

我做错了什么?

附言如果您需要更多详细信息,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() 是一个不错的选择,但对运算符(operator)来说效果不佳。特别是对于管道 (%>%),使用例如相反,一个函数会破坏它的目的。

  • @importFrom 标签比 @import 更可取,因为它更狭窄和明确。这两个都会在调用 roxygen2::roxygenize() 时影响 NAMESPACE 文件。但是请注意,roxygen 不会混淆用户定义的 NAMESPACE 文件,因为通常情况下人们更愿意自己手动处理它(例如,当包提供 S3 类和/或方法时), 然后需要撤消 roxygen 的覆盖。删除现有的 NAMESPACE 文件会让 roxygen 重新创建它。 Roxygen 通常会在 NAMESPACE 文件中添加一行以识别它是否应该更新它:

    # Generated by roxygen2: do not edit by hand

  • DESCRIPTION 文件中的依赖项既没有被 roxygen 修改,也没有 roxygen 将它们添加到 NAMESPACE(请注意,这否则会导致完整的包导入,我们宁愿通过 @importFrom)。 DESCRIPTION 文件需要手动处理,确保 Imports: 部分涵盖通过 NAMESPACE 使用的所有包,即通过 @import@ importFrom,以及通过 packagename::fun()

关于R 包未加载 `Imports` 包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48668540/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com