首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >坚持autoplot的S3方法定义

坚持autoplot的S3方法定义
EN

Stack Overflow用户
提问于 2018-04-18 05:50:18
回答 2查看 286关注 0票数 6

我一直在为autoplot定义S3方法。

我有以下内容(完整代码here):

代码语言:javascript
复制
#' 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
}

正如我现在所理解的,我应该能够运行,但它失败了:

代码语言:javascript
复制
> autoplot(test)
Error in autoplot(test) : could not find function "autoplot"

为什么它找不到这个函数?我有一个正确的@importFrom ggplot2 autoplot,而Roxygen产生了正确的NAMESPACE

DESCRIPTIONImports中有一个ggplot2

我不知道为什么它不工作,为什么我需要library(ggplot2)来使用它。

EN

回答 2

Stack Overflow用户

发布于 2018-04-18 07:42:08

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

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

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

如果不这样做,您的包就不知道来自ggplot2autoplot函数。

有一些解决方案可以解决这个问题:

  1. 使用Depends: ggplot2 (参见下面的链接,了解有关ImportsDepends的讨论,以及section 1.1.3 or writing R extensions]
  2. define a plot方法,该方法然后使用ggplot2::ggplot() autoplot.bigobenchmark调用各种ggplot2方法,但要求用户在使用前加载ggplot2(在实践中,<代码>C20中就是这样的一个示例。另请参阅?zoo::autoplot
  3. Export your own autoplot function,但如果用户随后加载ggplot2

,则这可能会导致冲突

以下是解决方案2的一个示例

代码语言:javascript
复制
#' 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的一个例子

代码语言:javascript
复制
#' 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中对ImportsDepends进行了更好的解释

票数 5
EN

Stack Overflow用户

发布于 2018-04-18 16:23:19

除了@SymbolixAU的响应之外,您还可以从ggplot2导入autoplot,然后像这样导出它,这样就不会与ggplot2冲突

代码语言:javascript
复制
#' 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
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49888022

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档