【问题标题】:Flow chart using {diagram} in R 3.0.0 for Windows在 R 3.0.0 for Windows 中使用 {diagram} 的流程图
【发布时间】:2013-04-15 15:49:22
【问题描述】:

我正在尝试使用图表包 (v 1.6) 在 R 中重新制作流程图。我能够使用这个确切的脚本(我从图表文档中的示例修改)制作图表,但是一旦我将 R 更新到 3.0.0,坐标函数就会给我一个错误。这是一个例子:

library(graphics)
library(diagram)

par(mar = c(1, 1, 1, 1))
openplotmat()
elpos<-coordinates(c(1,1,2,4))

Error in (function (classes, fdef, mtable)  : unable to find an inherited method for function ‘coordinates’ for signature ‘"numeric"’

我对 R 和代码等还是很陌生,所以当我运行 traceback() 时,我真的不明白它在告诉我什么:

3: stop(gettextf("unable to find an inherited method for function %s for signature %s", 
   sQuote(fdef@generic), sQuote(cnames)), domain = NA)
2: (function (classes, fdef, mtable) 
  {
   methods <- .findInheritedMethods(classes, fdef, mtable)
   if (length(methods) == 1L) 
       return(methods[[1L]])
   else if (length(methods) == 0L) {
       cnames <- paste0("\"", sapply(classes, as.character), 
           "\"", collapse = ", ")
       stop(gettextf("unable to find an inherited method for function %s for signature %s", 
           sQuote(fdef@generic), sQuote(cnames)), domain = NA)
   }
   else stop("Internal error in finding inherited methods; didn't return a unique method", 
       domain = NA)
  })(list("numeric"), function (obj, ...) 
  standardGeneric("coordinates"), <environment>)
1: coordinates(c(1, 1, 2, 4))

大多数情况下,我不知道为什么坐标()在更新后不起作用。任何对此的见解,以及可能的回溯翻译将是一个巨大的帮助。谢谢!

【问题讨论】:

  • 您是否从源代码重建包?在这里查看:stackoverflow.com/questions/15973594/…
  • @TARehman 为什么要构建? 3.0.0 二进制文件位于 CRAN 上。请确保您更新了所有库,尤其是 diagram,然后重试。
  • @TARehman,我从未尝试过重建包,但我卸载并重新安装了图表包(一切都已更新 - 我先尝试过)。显然,坐标()被另一个包掩盖了,所以我把它扔了,重新安装后,一切正常。非常感谢您的回复!

标签: r error-handling diagramming


【解决方案1】:

我不能像问题一样回答这个问题。最初,我无法重现您的错误:

library(diagram)
openplotmat()
(elpos1 <- diagram::coordinates(c(1,1,2,4)))
#       [,1]  [,2]
# [1,] 0.500 0.875
# [2,] 0.500 0.625
# ...

查找同名函数

然而,寻找 coordinates 函数的其他实例发现了一些东西:

help.search('coordinates', fields='name')
# Help files with name matching 'coordinates' using fuzzy matching:
# 
# diagram::coordinates                  coordinates of elements on a plot
# sp::coordinates-methods               retrieve (or set) spatial coordinates
# sp::coordinates                       sets spatial coordinates to create spatial data, or retrieves spatial
#                                       coordinates
# sp::coordnames                        retrieve or assign coordinate names for classes in sp

此输出搜索所有已安装(不一定已加载)的包。由此看来,sp 也有一个。在您的用例中使用它的版本会产生错误。

包加载顺序(或屏蔽函数)

加载包的顺序很重要,因为后面加载的函数会屏蔽之前加载的包中的同名函数。具体来说:

# ensure we have neither package loaded
detach(package:diagram, unload=TRUE) # ignore errors if not loaded
detach(package:sp, unload=TRUE)      # ignore errors if not loaded
library(diagram)
library(sp)
# Attaching package: 'sp'
# 
# The following object is masked from 'package:diagram':
# 
#     coordinates

此消息告诉您,对coordinates() 的简单调用将使用来自sp 的版本,而不是来自diagram 的版本。 (对于下面的每个代码块,我都使用上面的detach() 来确保包和它的命名空间都不存在。)

使用sp 版本会产生相同的错误,在按顺序加载库后:diagramsp

library(diagram)
library(sp)
# Attaching package: 'sp'
# 
# The following object is masked from 'package:diagram':
# 
#     coordinates
(elpos <- coordinates(c(1,1,2,4)))
# Error in (function (classes, fdef, mtable)  : 
# unable to find an inherited method for function 'coordinates' for signature '"numeric"'

traceback() 与您提供的相同。

颠倒加载顺序有效:

library(sp)
library(diagram)
# Attaching package: 'diagram'
# 
# The following object is masked from 'package:sp':
# 
#     coordinates
(elpos <- coordinates(c(1,1,2,4)))
#       [,1]  [,2]
# [1,] 0.500 0.875
# [2,] 0.500 0.625
# ...

请注意,警告现在告诉您sp::coordinates() 现在已被屏蔽。

当有疑问时,要明确

如果对调用哪个版本有任何疑问,我们总是可以强制使用我们打算使用的版本:

(elpos <- diagram::coordinates(c(1,1,2,4)))
#       [,1]  [,2]
# [1,] 0.500 0.875
# [2,] 0.500 0.625
# ...

我觉得将其作为答案发布有点不妥,因为我正在解决您的问题,而不一定是陈述的问题。如果您仍然需要寻找traceback() 的结果,请继续提示您的答案。不过,在这项工作中,我找不到.findInheritedMethods(),但是当diagram::coordinates 期望一个向量来指定每行中的元素数量,或者带有元素位置的2列矩阵,或者'NULL ',而sp::coordinates 期望对象派生自“空间”类(这绝对不是一个简单的向量)。

【讨论】:

  • 哇,谢谢!这是一个非常有用的解释。它很好地回答了这个问题,谢谢。 :)
猜你喜欢
  • 2021-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-28
  • 2017-11-01
相关资源
最近更新 更多