简体   繁体   English

get(as.character(FUN), mode = "function", envir = envir) 中的错误:模式 'function' 的对象 'guide_legend'

[英]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:但是,当我在控制台中调用此函数时,我得到了没有错误消息的预期图:

Expected_output预期输出

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.

相关问题 get(as.character(FUN), mode = “function”, envir = envir) 中的错误: - Error in get(as.character(FUN), mode = “function”, envir = envir) : 获取错误(as.character(FUN),模式=“功能”,环境=环境) - Error in get(as.character(FUN), mode = “function”, envir = envir) R: get(as.character(FUN), mode = “function”, envir = envir) 中的错误: - R: Error in get(as.character(FUN), mode = “function”, envir = envir) : 从函数内部使用jags.parallel(R语言get(name,envir = envir)中的错误:未找到对象&#39;y&#39;) - Using jags.parallel from within a function (R language Error in get(name, envir = envir) : object 'y' not found) 运行函数时出错:eval(expr,envir,enclos)中的错误:找不到对象 - Error running a function: Error in eval(expr, envir, enclos) : object not found 错误:找不到模式 &#39;function&#39; 的对象 &#39;FUN&#39; - 即使更改了变量名 - Error: object 'FUN' of mode 'function' was not found - even with changed variable name 自定义函数导致返回ggplot对象时出错,并且eval(expr,envir,enclos)中出现错误:找不到对象&#39;MinVal&#39; - Custom function causing an error with returning a ggplot object and Error in eval(expr, envir, enclos) : object 'MinVal' not found R; 使用操纵函数并获取:(函数(x)中的错误:未使用的参数(envir = <environment> ) - R; Using manipulate function and get : Error in (function (x) : unused argument (envir = <environment>) ggplot2(有序条形图)功能:eval(expr,envir,enclos)错误:找不到对象 - Function with ggplot2 (ordered barplot): Error in eval(expr, envir, enclos): object not found 在R中编写函数,收到错误“eval中的错误(expr,envir,enclos):...” - wrote function in R, receiving error “Error in eval(expr, envir, enclos) : …”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM