0

如果您在包中发现错误,通常可以使用 修补问题fixInNamespace,例如fixInNamespace("mean.default", "base").

对于 S4 方法,我不知道该怎么做。我正在查看的方法在gWidgetstcltk包中。你可以看到源代码

getMethod(".svalue", c("gTabletcltk", "guiWidgetsToolkittcltk"))

我找不到fixInNamespace.

fixInNamespace(".svalue", "gWidgetstcltk")

Error in get(subx, envir = ns, inherits = FALSE) : 
  object '.svalue' not found

我想setMethod可能会成功,但是

setMethod(".svalue", c("gTabletcltk", "guiWidgetsToolkittcltk"),
  definition = function (obj, toolkit, index = NULL, drop = NULL, ...) 
  {
      widget = getWidget(obj)
      sel <- unlist(strsplit(tclvalue(tcl(widget, "selection")), 
          " "))
      if (length(sel) == 0) {
          return(NA)
      }
      theChildren <- .allChildren(widget)
      indices <- sapply(sel, function(i) match(i, theChildren))
      inds <- which(visible(obj))[indices]
      if (!is.null(index) && index == TRUE) {
          return(inds)
      }
      if (missing(drop) || is.null(drop)) 
          drop = TRUE
      chosencol <- tag(obj, "chosencol")
      if (drop) 
          return(obj[inds, chosencol, drop = drop])
      else return(obj[inds, ])
  },
  where = "package:gWidgetstcltk"  
)

Error in setMethod(".svalue", c("gTabletcltk", "guiWidgetsToolkittcltk"),  : 
  the environment "gWidgetstcltk" is locked; cannot assign methods for function ".svalue"

有任何想法吗?

4

2 回答 2

1

获取源、应用更改和重建的老式方法怎么样?

于 2010-05-14T15:34:44.487 回答
0

您可以先获取泛型,然后在全局环境中通过 setMethod 修复泛型,然后将其分配回该命名空间

.svalue <- gWidgetstcltk:::.svalue
setMethod(".svalue", c("gTabletcltk", "guiWidgetsToolkittcltk"),
  definition = function (obj, toolkit, index = NULL, drop = NULL, ...) 
  {
      widget = getWidget(obj)
      sel <- unlist(strsplit(tclvalue(tcl(widget, "selection")), 
          " "))
      if (length(sel) == 0) {
          return(NA)
      }
      theChildren <- .allChildren(widget)
      indices <- sapply(sel, function(i) match(i, theChildren))
      inds <- which(visible(obj))[indices]
      if (!is.null(index) && index == TRUE) {
          return(inds)
      }
      if (missing(drop) || is.null(drop)) 
          drop = TRUE
      chosencol <- tag(obj, "chosencol")
      if (drop) 
          return(obj[inds, chosencol, drop = drop])
      else return(obj[inds, ])
  }#,
  #where = "package:gWidgetstcltk"  
)
assignInNamespace(".svalue", .svalue, ns = "gWidgetstcltk")
于 2021-11-24T05:49:55.260 回答