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 "%>%"
我做错了什么?
P.S. 如果需要更多细节, 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
%>%
。我建议在这种情况下添加@importFrom标签。 - RolandAScmagrittr
添加到“导入”中,这样做不会自动解决问题吗? 无论如何,我确实在函数中添加了#' @importFrom magrittr %>%
调用,但仍然出现相同的错误。 只有当我调用library(dplyr)
时,错误才会消失,但对于包内R脚本中的函数来说,这是一个大忌。所以不确定该怎么做才能摆脱这个错误。 - Indrajeet PatilRstudio
中打开有问题的R包项目并运行roxygenize()
,该函数就可以工作。但是,如果我退出并重新进入该项目,然后在没有先运行roxygenize()
的情况下使用该包中的函数,它会再次出现相同的错误。我猜我错过了一些关键功能,这些功能涉及到roxygen2
在包环境中应该如何操作。 - Indrajeet Patil