[英]Error in get(as.character(FUN), mode = "function", envir = envir) : object 'guide_legend' of mode 'function'
I'm building a package and I have encountered a problem when building a vignette.我正在构建一个包,并且在构建小插图时遇到了问题。 I get an error message when I try to knit the vignette, but when running function from console there are no errors.
当我尝试编织小插图时收到一条错误消息,但是从控制台运行函数时没有错误。
Example of input data输入数据示例
a <- c(1,1,2,2,3,3,4,4,5,5)
b <- c("T", "T", "N","N", "N","N", "T", "T", "N", "N")
c <- c("car","bike","car","bike","car","bike","car","bike","car","bike")
d <- c(10,12,15,19,25,29,38,47,66,90)
data <- data.frame(a,b,c,d)
colnames(data) <- c("xaxis", "rug", "type", "val")
Inside a package .r file this is a function I would like to call:在包 .r 文件中,这是一个我想调用的函数:
#' Test
#'
#' @param data test data
#'
#' @return
#' @importFrom ggplot2 ggplot geom_point geom_rug
#' @importFrom ggnewscale new_scale_color
#' @export
#'
test <- function(data){
ggplot(data, aes(x = xaxis, y = val, color = type, group = type)) + geom_point() +
new_scale_color() + geom_rug(aes(x = xaxis, color =rug), sides = "b")
}
I checked and installed the package and tried to call this test function in an RMarkdown document (vignette).我检查并安装了该包,并尝试在 RMarkdown 文档(小插图)中调用此测试函数。 This is the error i get:
这是我得到的错误:
Error in get(as.character(FUN), mode = "function", envir = envir) : object 'guide_legend' of mode 'function' was not found Calls: ... bump_aes_scales -> lapply -> FUN -> match.fun -> get Execution halted
get(as.character(FUN), mode = "function", envir = envir) 中的错误:找不到模式 'function' 的对象 'guide_legend' 调用:...bump_aes_scales -> lapply -> FUN -> 匹配。有趣 -> 停止执行
However, when I call this function in console I get the expected plot with no error messages:但是,当我在控制台中调用此函数时,我得到了没有错误消息的预期图:
Furthermore, when I delete new_scale_color() from the test function I get no errors and vignette is knitted perfectly.此外,当我从测试函数中删除 new_scale_color() 时,我没有收到任何错误,并且小插图编织得很好。 Does anybody have an idea what seems to be the problem here and how to solve it?
有没有人知道这里似乎是什么问题以及如何解决它?
The problem seems to be in the ggnewscale package, at this line, where it tries to create a call to ggplot::guide_legend
.问题似乎出在 ggnewscale 包中,在这一行,它试图创建对
ggplot::guide_legend
的调用。 This is done via match.fun
, which then throws an error at this line .这是通过
match.fun
完成的,然后在这一行抛出一个错误。 It may be because match.fun
only looks back through 2 layers of parent environments for the named function.可能是因为
match.fun
只回顾了命名函数的 2 层父环境。 You could try adding a non-exported, local, alias function to your own package prior to the problematic function:您可以尝试在出现问题的函数之前将非导出的本地别名函数添加到您自己的包中:
guide_legend <- function(...) ggplot2::guide_legend(...)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.