问题描述
我正在尝试构建一个严重依赖 ggplot2 的数据可视化包,但有一些自定义快捷方式可以解决我面临的一些日常问题。
我可以使用 ggplot_add
函数为来自脚本的自定义类扩展 +
的功能,但是当我将这些脚本添加到包时,ggplot_add
不再有效。
下面我粘贴了一个 minrep,要复制第一个需要创建一个包(我正在使用 RStudio),我称之为 SOExa。 该项目包含以下文件:
.Rbuildignore
^.*\.Rproj$
^\.Rproj\.user$
描述
Package: SOExa
Type: Package
Title: An minrep for a problem I'm having
Version: 0.1.0
Author: Col Bates
Maintainer: The package maintainer <yourself@somewhere.net>
Description: I want to use ggplot2's ggplot_add from inside another package,i.e. this one.
It seems that when I do I get an error.
License: GPLv2
Encoding: UTF-8
Imports:
dplyr,magrittr,tidyr,glue,ggplot2
LazyData: true
RoxygenNote: 7.1.1
我的项目文件 SOExa.Rproj
R/designed_by.R
#' the function to add a 'designed by' to the plot
#' as a designed_by class
#'@export
designed_by<-function(x){
return(new_designed_by(x))
}
#' generic constructor.
#' @export
new_designed_by<-function(x){
x <- list('designed_by' = x)
class(x) <- 'designed_by'
return(x)
}
#' generic print for designed_by
#' @export
print.designed_by <- function(x){
print(paste('Designed by:',format(x)))
}
#' defines the addition of an designed_by object for
#' @export
ggplot_add.designed_by <- function(object,plot,objectname){
plot$designed_by <- object$designed_by
plot
}
ggplot_add <- function(x){
UseMethod("ggplot_add")
}
devtools::document()
命名空间
# Generated by roxygen2: do not edit by hand
S3method(ggplot_add,designed_by)
S3method(print,designed_by)
export(designed_by)
export(new_designed_by)
在此之后我安装并加载库:
devtools::install()
library(SOExa)
然后创建一个空图:
p <- ggplot2::ggplot()
以下将导致错误:
p <- p + designed_by('Col Bates')
我得到的错误是:
# Error: Can't add `designed_by("Col Bates")` to a ggplot object.
# Run `rlang::last_error()` to see where the error occurred.
请按照以下步骤操作:
rlang::last_error()
哪个返回
# <error/rlang_error>
# Can't add `designed_by("Col Bates")` to a ggplot object.
# Backtrace:
# 1. ggplot2:::`+.gg`(p,designed_by("Col Bates"))
# 2. ggplot2:::add_ggplot(e1,e2,e2name)
# 4. ggplot2:::ggplot_add.default(object,p,objectname)
# Run `rlang::last_trace()` to see the full context.
运行
rlang::last_trace()
我明白
<error/rlang_error>
Can't add `designed_by("Col Bates")` to a ggplot object.
Backtrace:
x
1. \-ggplot2:::`+.gg`(p,designed_by("Col Bates"))
2. \-ggplot2:::add_ggplot(e1,e2name)
3. +-ggplot2::ggplot_add(object,objectname)
4. \-ggplot2:::ggplot_add.default(object,objectname)
由此我可以推断调用 UseMethod('ggplot_add')
的 ggplot2::ggplot_add() 已决定应用函数 ggplot_add.default
,并且没有识别我的类 designed_by
。
顺便说一句,使用 print()
函数确实可以从库中使用。
print(designed_by('Col Bates'))
source('./R/designed_by.R')
p <- p + designed_by('Col Bates')
它确实按照我期望的方式工作。
深入研究,我可以看到类 ggplot_add
上的泛型 designed_by
的来源是我的包。
sloop::s3_methods_generic("ggplot_add")
## A tibble: 1 x 4
# generic class visible source
# <chr> <chr> <lgl> <chr>
# 1 ggplot_add designed_by TRUE SOExa
而对于 ggplot 类,它是“注册的 S3method”
> sloop::s3_methods_generic("ggplot_add")
## A tibble: 14 x 4
# generic class visible source
# <chr> <chr> <lgl> <chr>
# 1 ggplot_add by FALSE registered S3method
# 2 ggplot_add Coord FALSE registered S3method
# 3 ggplot_add data.frame FALSE registered S3method
# 4 ggplot_add default FALSE registered S3method
# 5 ggplot_add Facet FALSE registered S3method
# ...
我查看了 ggplot2 源代码,但无法真正弄清楚它是如何工作的。我也一直在阅读 https://adv-r.hadley.nz/s3.html,但没有看到任何关于使用适用于其他库中的类的 S3methods 的内容。
如果可以将调用打包到我的自定义包中,或者我是否总是需要依赖采购,那就太好了。
谢谢。
解决方法
这是一个让我很头疼的常见问题。您需要确保您的包可以访问 ggplot2
的 ggplot_add
通用函数。您可以通过以下两种方式之一执行此操作。
您需要在包中的某处包含以下行:
#' @import ggplot2
这将使所有 ggplot2 函数可用,或者如果您只使用该函数,请执行以下操作:
#' @importFrom ggplot2 ggplot_add
这有点不直观,但仅仅因为 DESCRIPTION
文件说它导入了一个包,并不意味着您的包的命名空间将能够看到该包的功能。您始终可以通过查看 NAMESPACE
文件来检查这一点。当您包含上述建议时,您应该会在 NAMESPACE
文件的底部看到以下行之一
import(ggplot2)
importFrom(ggplot2,ggplot_add)
,
如果您将 ggplot2
添加到 Depends
而不是 Imports
在 DESCRIPTION
文件中,您的设置应该可以工作。例如,如果我创建一个包含以下文件的新包:
.Rbuildignore
^.*\.Rproj$
^\.Rproj\.user$
描述
Package: SOExa
Type: Package
Title: An minrep for a problem I'm having
Version: 0.1.0
Author: Col Bates
Maintainer: The package maintainer <yourself@somewhere.net>
Description: I want to use ggplot2's ggplot_add from inside another package,i.e. this one.
It seems that when I do I get an error.
License: GPLv2
Encoding: UTF-8
Depends: ggplot2
Imports:
dplyr,magrittr,tidyr,glue
LazyData: true
RoxygenNote: 7.1.1
designed_by.R
#' the function to add a 'designed by' to the plot
#' as a designed_by class
#'@export
designed_by<-function(x){
return(new_designed_by(x))
}
#' generic constructor.
#' @export
new_designed_by<-function(x){
x <- list('designed_by' = x)
class(x) <- 'designed_by'
return(x)
}
#' generic print for designed_by
#' @export
print.designed_by <- function(x){
print(paste('Designed by:',format(x)))
}
#' defines the addition of an designed_by object for
#' @export
ggplot_add.designed_by <- function(object,plot,objectname){
plot$designed_by <- object$designed_by
plot
}
然后运行 devtools::document()
给我:
命名空间
# Generated by roxygen2: do not edit by hand
S3method(ggplot_add,designed_by)
S3method(print,designed_by)
export(designed_by)
export(new_designed_by)
所以在我执行 devtools::install()
后,我得到以下输出:
library(SOExa)
#> Loading required package: ggplot2
p <- ggplot(data = NULL,aes(x = 1:10,y = 1:10)) + geom_point()
p <- p + designed_by('Col Bates')
p
p$designed_by
#> [1] "Col Bates"