替换R中内置函数的定义? [英] Replace definition of built-in function in R?

查看:294
本文介绍了替换R中内置函数的定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

'sparcl'包在标准'stat'包中使用'kmeans'功能。我想让它使用我自己的kmeans ++实现。



执行此操作的一种方法是编辑sparcl程序包本身的代码。我宁愿避免这种情况,因为它会很麻烦,因为我不确定我将如何在R中安装编辑后的代码。



不幸的是, superassignment operator<< - 不起作用:

 > kmeans<<  -   - 函数(x)打印(hi!)
错误:无法更改'kmeans'的锁定绑定的值

既不是assign:

  assign(kmeans,function x){print(HI THERE!); return(FALSE)},pos =package:sparcl)
错误assign(is.null,function(x){:
不能添加绑定到一个锁定的环境

那么编译包的代码是唯一的方法吗?



谢谢!

解决方案

关于进一步思考(以及重读您的问题后) ,这里有一个简单的解决方案,可以为你工作。



您需要做的就是分配编辑版本的 kmeans()到全局环境中的符号 kmeans 。换句话说,在命令行执行以下操作:

  kmeans<  - 函数(...)绘图(rnorm(99),col =red)#但使用您自己的编辑

##然后从KMea运行一个例子nsSparseCluster来查看它的工作原理。
库(sparcl)
x< - 矩阵(rnorm(50×300),ncol = 300)
x [1:25,1:50]< -x [1:25, (x,TRUE,TRUE)
KMeansSparseCluster.permute(x,K = 2,wbounds = seq(3,9,len = 15),nperms = 5)

因为 KMeansSparseCluster() (并且调用 package:sparcl 中的任何其他函数),查找 kmeans first namespace:sparcl then 位于 imports:sparcl then namespace:base 中,然后在 .GlobalEnv 中,它会找到您重新定义的 kmeans ,然后到达 package:stats 中的一个。为了让自己看看,试试这个:

  parent.env(asNamespace(sparcl))
parent。 env(parent.env(asNamespace(sparcl)))
parent.env(parent.env(parent.env(asNamespace(sparcl))))
##等,也包装上述任何一个环境调用ls()
##来查看'em

很好,使用 kmeans()的stats包中的函数不会被您的版本中断,因为它们会找到 kmeans 在它们自己的命名空间中,在符号搜索进入全球环境之前。


The 'sparcl' package uses the 'kmeans' function in the standard 'stat' package. I want to make it use my own implementation of kmeans++ instead.

One way to do this would be to edit the code in the sparcl package itself. I'd prefer to avoid this, both because it would be messy and because I'm not sure how I would then install that edited code in R.

Unfortunately, the superassignment operator "<<-" doesn't work:

> kmeans <<- function(x) print("hi!")
Error: cannot change value of locked binding for 'kmeans'

neither does "assign":

assign("kmeans",function(x) {print("HI THERE!"); return(FALSE)},pos="package:sparcl")
Error in assign("is.null", function(x) { : 
  cannot add bindings to a locked environment

So is editing the package code the only way?

Thanks!

解决方案

On further thought (and after re-reading your question), here's a simple solution that should work for you.

All you need to do is to assign your edited version of kmeans() to the symbol kmeans in the global environment. In other words, at the command line do this:

kmeans <- function(...) plot(rnorm(99), col="red") # but using your own edits

## Then run an example from ?KMeansSparseCluster to see that it works.
library(sparcl)
x <- matrix(rnorm(50*300),ncol=300)
x[1:25,1:50] <- x[1:25,1:50]+1
x <- scale(x, TRUE, TRUE)
KMeansSparseCluster.permute(x,K=2,wbounds=seq(3,9,len=15),nperms=5)

This works because KMeansSparseCluster() (and calls to any other functions in package:sparcl) look for kmeans first in namespace:sparcl, then in imports:sparcl, then in namespace:base, and then in .GlobalEnv, where it'll find your redefined kmeans before it gets to the one in package:stats. To have a look yourself, try this:

parent.env(asNamespace("sparcl"))
parent.env(parent.env(asNamespace("sparcl")))
parent.env(parent.env(parent.env(asNamespace("sparcl"))))
## etc., also wrapping any of the environments above in calls to ls() 
## to see what's in 'em

Nicely, functions from the stats package that use kmeans() won't be disrupted by your version, because they will find kmeans in their own namespace, before the symbol-search ever gets to the global environment.

这篇关于替换R中内置函数的定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆