首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何修补R包中的S4方法?

如何修补R包中的S4方法?
EN

Stack Overflow用户
提问于 2010-05-14 23:29:08
回答 2查看 349关注 0票数 0

如果你在一个包中发现了一个bug,通常可以用fixInNamespace来修补这个问题,比如fixInNamespace("mean.default", "base")

但是对于S4方法,我不确定该怎么做。我正在查看的方法在gWidgetstcltk包中。您可以使用以下命令查看源代码

代码语言:javascript
复制
getMethod(".svalue", c("gTabletcltk", "guiWidgetsToolkittcltk"))

我找不到使用fixInNamespace的方法。

代码语言:javascript
复制
fixInNamespace(".svalue", "gWidgetstcltk")

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

我以为setMethod可能会成功,但是

代码语言:javascript
复制
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"

有什么想法吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-05-14 23:34:44

老式的获取源代码、应用更改和重建的方法如何?

票数 1
EN

Stack Overflow用户

发布于 2021-11-24 05:49:55

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

代码语言:javascript
复制
.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")
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2835375

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档