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

eri*_*tta 5 error-handling r 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"’
Run Code Online (Sandbox Code Playgroud)

我还是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))
Run Code Online (Sandbox Code Playgroud)

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

r2e*_*ans 2

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

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

查找同名函数

然而,寻找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
Run Code Online (Sandbox Code Playgroud)

此输出搜索所有已安装(不一定已加载)的软件包。由此看来,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
Run Code Online (Sandbox Code Playgroud)

此消息告诉您,对的简单调用将使用来自而不是来自的coordinates()版本。(对于下面的每个代码块,我使用上面的方法来确保包及其名称空间都不存在。)spdiagramdetach()

使用该sp版本会产生与按顺序加载库后遇到的相同错误:diagram, sp:

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"'
Run Code Online (Sandbox Code Playgroud)

与您提供的内容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
# ...
Run Code Online (Sandbox Code Playgroud)

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

如有疑问,要明确

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

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

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