gpt4 book ai didi

r - 坚持自动绘图 S3 方法的定义

转载 作者:行者123 更新时间:2023-12-02 00:08:47 26 4
gpt4 key购买 nike

我一直在为 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)才能使用它。

最佳答案

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

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

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

如果不这样做,您的包将无法识别 ggplot2 中的 autoplot 函数。

对此有一些解决方案:

  1. 使用Depends:ggplot2(有关ImportsDepends的讨论,请参阅下面的链接,以及section 1.1.3 or writing R extensions]
  2. 定义一个 plot 方法,然后调用各种 ggplot2::ggplot() 函数
  3. 继续使用 autoplot.bigobenchmark,但要求用户在使用前加载 ggplot2(实践中的示例位于 the zoo package 中。另请参阅 ?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)在 this SO answer 中对 ImportsDepends 给出了更好的解释。

关于r - 坚持自动绘图 S3 方法的定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49888022/

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