更改包的内部功能 [英] Change internal function of a package

查看:10
本文介绍了更改包的内部功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一些背景:

我必须经常使用 HMR 包中的函数 HMR.不幸的是,这个功能很慢.(HMR本质上是一个拟合函数,设计得尽可能的健壮,这也是效率不高的原因之一.)HMR函数调用函数HMR::.HMR.fit1,进行实际拟合.使用 Rprof 我知道关于效率的主要问题是使用 lsfit,它被调用了很多.因此,我修改了.HMR.fit1的代码,直接调用lsfit使用的C函数,没有lsfit的所有开销,应该导致显着的速度增益.

I have to use function HMR from package HMR a lot. Unfortunately, this function is very slow. (HMR is essentially a fitting function, which is designed to be as robust as possible, which is one reason for the lack of efficiency.) Function HMR calls function HMR::.HMR.fit1, which does the actual fitting. Using Rprof I know that the main problem regarding efficiency is the use of lsfit, which gets called a lot. Therefore, I modified the code of .HMR.fit1 to call the C function used by lsfit directly without all the overhead of lsfit, which should result in a substantial speed gain.

现在我想用我修改的函数替换 HMR::.HMR.fit1 并测试 HMR 是否给出相同的结果以及我获得了多少速度.

Now I would like to substitute HMR::.HMR.fit1 with my modified function and test HMR if it gives the same results and how much speed I gain.

我尝试过这样做:

mod.fun <- function(<many args>) {
 <a lot of code>
}
environment(mod.fun) <- environment(.HMR.fit1)
.HMR.fit1 <- mod.fun 

但是,HMR::.HMR.fit1 并没有因此而改变,显然 HMR::HMR 没有使用我修改后的拟合函数.有没有办法在不从源代码构建包的情况下实现我想要的,由于我的(Windows)计算机上的用户权限限制,我无法做到这一点?

However, HMR::.HMR.fit1 is not changed by doing this and apparently HMR::HMR does not use my modified fitting function. Is there a way to achieve what I want without building the package from source, which I cannot do due to user rights restrictions on my (windows) computer?

目前我的解决方案是复制HMR::HMR的代码,希望有更方便的解决方案.

Right now, my solution would be to copy the code of HMR::HMR, but I hope there is a more convenient solution.

推荐答案

试试

?assignInNamespace

HMR 包中的 .HMR.fit1 替换为您的版本.

to replace .HMR.fit1 in the HMR package with your version.

可能重复:

如何覆盖包命名空间中的不可见函数?

这篇关于更改包的内部功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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