我一直在为autoplot定义S3方法。
我有以下内容(完整代码here):
#' Autoplot for bigobenchmark object
#'
#' @importFrom ggplot2 autoplot
#'
#' @param object
#'
#' @return A ggplot2 plot
#' @export
#'
#' @examples
#' # Create plot for benchmarks
#' library(ggplot2)
#' bench <- bigobenchmark(1:n, for(i in 1:n) for(i in 1:n) 1:n, args=seq(from=1, to=100, length.out = 50))
#' autoplot(bench)
autoplot.bigobenchmark <- function(object) {
plt <- ggplot2::ggplot(data = object$benchmarks, ggplot2::aes(x=arg, y=mean, colour=expr))
plt <- plt + ggplot2::geom_line()
plt <- plt + ggplot2::geom_pointrange(aes(ymin=min, ymax=max))
plt
}正如我现在所理解的,我应该能够运行,但它失败了:
> autoplot(test)
Error in autoplot(test) : could not find function "autoplot"为什么它找不到这个函数?我有一个正确的@importFrom ggplot2 autoplot,而Roxygen产生了正确的NAMESPACE。
在DESCRIPTION的Imports中有一个ggplot2。
我不知道为什么它不工作,为什么我需要library(ggplot2)来使用它。
发布于 2018-04-18 07:42:08
当您导入一个包时,它是“通过名称空间加载(而不是附加的)”(引用自sessionInfo())。
当您想要使用导入包中的函数时,通常使用结构ggplot2::ggplot()调用它,就像您所做的那样。
因此,要使用autoplot,您仍然需要使用ggplot2::autoplot()。
如果不这样做,您的包就不知道来自ggplot2的autoplot函数。
有一些解决方案可以解决这个问题:
Depends: ggplot2 (参见下面的链接,了解有关Imports与Depends的讨论,以及section 1.1.3 or writing R extensions]plot方法,该方法然后使用ggplot2::ggplot() autoplot.bigobenchmark调用各种ggplot2方法,但要求用户在使用前加载ggplot2(在实践中,<代码>C20中就是这样的一个示例。另请参阅?zoo::autoplotautoplot function,但如果用户随后加载ggplot2,则这可能会导致冲突
以下是解决方案2的一个示例
#' plot for bigobenchmark object
#'
#' @importFrom ggplot2 autoplot
#'
#' @param object
#'
#' @return A ggplot2 plot
#' @export
#'
#' @examples
#' # Create plot for benchmarks
#' library(ggplot2)
#' bench <- bigobenchmark(1:n, for(i in 1:n) for(i in 1:n) 1:n, args=seq(from=1, to=100, length.out = 50))
#' plot(bench)
#'
#' @author Andrew Prokhorenkov
plot.bigobenchmark <- function(object) {
plt <- ggplot2::ggplot(data = object$benchmarks, ggplot2::aes(x=arg, y=mean, colour=expr))
plt <- plt + ggplot2::geom_line()
plt <- plt + ggplot2::geom_pointrange(ggplot2::aes(ymin=min, ymax=max))
plt
}下面是解决方案4的一个例子
#' Autoplot for bigobenchmark object
#'
#' @importFrom ggplot2 autoplot
#'
#' @param object
#'
#' @return A ggplot2 plot
#' @export
#'
#' @examples
#' # Create plot for benchmarks
#' library(ggplot2)
#' bench <- bigobenchmark(1:n, for(i in 1:n) for(i in 1:n) 1:n, args=seq(from=1, to=100, length.out = 50))
#' autoplot(bench)
#'
#' @author Andrew Prokhorenkov
autoplot <- function(object) UseMethod("autoplot")
#' @export
autoplot.bigobenchmark <- function(object) {
plt <- ggplot2::ggplot(data = object$benchmarks, ggplot2::aes(x=arg, y=mean, colour=expr))
plt <- plt + ggplot2::geom_line()
plt <- plt + ggplot2::geom_pointrange(ggplot2::aes(ymin=min, ymax=max))
plt
}Josh O‘’Brian和majom (引用哈德利的话)在this SO answer中对Imports和Depends进行了更好的解释
发布于 2018-04-18 16:23:19
除了@SymbolixAU的响应之外,您还可以从ggplot2导入autoplot,然后像这样导出它,这样就不会与ggplot2冲突
#' bigobenchmark exported operators and S3 methods
#'
#' The following functions are imported and then re-exported
#' from the bigobenchmark package to avoid loading them.
#'
#' @importFrom ggplot2 autoplot
#' @name autoplot
#' @export
NULLhttps://stackoverflow.com/questions/49888022
复制相似问题