6

我坚持为autoplot.

我有以下(完整的代码在这里):

#' 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 autoplotRoxygen 产生正确NAMESPACE的 .

里面有一个ggplot2ImportsDESCRIPTION

我不知道为什么它不起作用以及为什么我需要library(ggplot2)使用它。

4

2 回答 2

5

当您导入一个包时,它是“通过命名空间加载的(而不是附加的)”(引用自sessionInfo())。

当您想使用导入包中的函数时,您通常使用结构来调用它ggplot2::ggplot(),就像您所做的那样。

因此,要使用autoplot您仍然需要使用ggplot2::autoplot().

如果你不这样做,你的包不知道autoplot来自ggplot2.

有几个解决方案:

  1. 使用(有关vsDepends: ggplot2的讨论,请参见下面的链接,以及第 1.1.3 节或编写 R 扩展]ImportsDepends
  2. 定义一个plot方法,然后调用各种ggplot2::ggplot()函数
  3. 继续autoplot.bigobenchmark,但要求用户ggplot2在使用前加载(实践中的一个例子是在zoo 包中。另请参阅?zoo::autoplot
  4. 导出您自己的autoplot函数,但如果用户稍后加载 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(引用 Hadley)在这个 SO 答案中给出了对Importsvs的更好解释Depends

于 2018-04-17T23:42:07.793 回答
4

除了@SymbolixAU 的回复,你可以像这样导入autoplot导出ggplot2,不会有冲突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
NULL
于 2018-04-18T08:23:19.093 回答