在R包中重新定义一个函数 [英] Redefining a function in an R package

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

问题描述

我试图通过首先定义一个函数来修改和重新定义R package xcms中的一个函数(xcmsRaw)

  my.xcmsRaw <  - 函数(文件名,profstep = 1,profmethod =bin,
profparam = list(mzcorrf = 1),#PATCH - mzcorrf是m / z校正因子,例如长链碳氢化合物的0.99888 b $ b includeMSn = FALSE,mslevel = NULL,
scanrange = NULL){...}



然后键入

  unlockBinding(xcmsRaw,as.environment(package:xcms))$ b $ (xcmsRaw,as.environment(package:xcms))

然而,当我运行它时,它给了我错误

  get(as.character(FUN),mode =function,envir = envir)中的错误:
找不到模式'function'的对象'profBinM'

引起的它找不到profBinM函数,它是xcms包的文件xcms.c中定义的C代码函数。



关于如何解决此问题的任何想法? (我在Windows 7下工作,使用R 3.0.0版本)

解决方案

感谢乔希 - 对我来说,现在通过

  modifline ='if((profparam $ mzcorrf!= 1)& length(unique(rawdata $ mz  -  trunc (rawdata $ mz)))!= 1){rawdata $ mz = rawdata $ mz * profparam $ mzcorrf} else if(profparam $ mzcorrf!= 1){print(精确质量已经四舍五入到标称质量); profparam $ mzcorrf = 1}'
insertatline = 6
trace(xcmsRaw,tracer = modifline,at = c(insertatline))

在这里我找到了正确的代码行来插入修改过的代码:

  as.list (body(xcmsRaw))

为了抑制跟踪I的输出,然后定义了第二个函数


$ b $ pre $ xcmsRaw2 = function(...){sink(NUL); obj = xcmsRaw(...); sink(); return(obj)}

可以被调用,并且不会提供任何不必要的跟踪输出。 / p>

W尽管如此,仍然可以很好地通过assignInNamespace()来实现它,因为这样可以进行更广泛的编辑/重定义,还可以更改函数参数(这将是重新定义函数的常见原因,也就是说,额外的参数)...


I tried to modify and redefine a function (xcmsRaw) in R package xcms by first defining a function

my.xcmsRaw <- function(filename, profstep = 1, profmethod = "bin",
                    profparam = list(mzcorrf=1),    # PATCH - mzcorrf is the m/z correction factor, e.g. 0.99888 for long-chain hydrocarbons
                    includeMSn = FALSE, mslevel=NULL,
                    scanrange=NULL) { ... }

and then typing

unlockBinding("xcmsRaw", as.environment("package:xcms"))
assign("xcmsRaw", my.xcmsRaw, as.environment("package:xcms"))
lockBinding("xcmsRaw", as.environment("package:xcms"))

However, when I run it it gives me the error

Error in get(as.character(FUN), mode = "function", envir = envir) : 
  object 'profBinM' of mode 'function' was not found

caused by it not finding the profBinM function, which is a C code function defined in file xcms.c of the xcms package.

Any thoughts on how I could resolve this issue? (I am working under Windows 7, using R version 3.0.0)

解决方案

Thanks Josh - in my case I got it working now via

modifline='if ((profparam$mzcorrf!=1)&length(unique(rawdata$mz - trunc(rawdata$mz)))!=1) {rawdata$mz=rawdata$mz*profparam$mzcorrf} else if (profparam$mzcorrf!=1) {print("Exact masses were already rounded to nominal masses");profparam$mzcorrf=1}'
insertatline=6
trace(xcmsRaw, tracer=modifline,at=c(insertatline))

where I found the correct line to insert my modified code using

as.list(body(xcmsRaw))

To suppress the output of trace I then defined a second function

xcmsRaw2=function(...) {sink("NUL");obj=xcmsRaw(...);sink();return(obj) }

which can the be called and which does not provide any unnecessary tracing output.

Would still be nice to get it working via assignInNamespace() too though, as that would allow for more extensive edits/redefinitions and also for changes in the function arguments (which would be a common reason to redefine functions, that is, to take some extra argument)...

这篇关于在R包中重新定义一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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