使用R 3.0.0 for Windows中的{diagram}的流程图

时间:2013-04-15 15:49:23

标签: r error-handling diagramming

我正在尝试使用图表包(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))

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

1 个答案:

答案 0 :(得分:2)

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

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()来确保包和它的nameSpace都不在。)

在按顺序加载库后,使用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期待对象派生自“Spatial”类(绝对不是简单的向量)。