My package (let's call it A) depends on another package B. I need to modify a function f
in B that has a bug that is causing my package to fail. The problem is that f
is an unexported function.
If f
was exported, I could use the technique described in this post to R-help:
The few times I want to patch a function like this, I use:
unlockBinding(name, env);
assignInNamespace(name, value, ns=pkgName, envir=env);
assign(name, value, envir=env);
lockBinding(name, env);
But because f
is unexported, this doesn't work.
Simple example to illustrate the problem:
# rf is an exported function from the stats package; this works
foo <- function(x) x
unlockBinding("rf", as.environment("package:stats"))
assignInNamespace("rf", foo, ns="stats", pos="package:stats")
assign("rf", bar, pos="package:stats")
lockBinding("rf", as.environment("package:stats"))
rf(42)
# 42
# C_rf is an unexported object that rf() uses; this fails
bar <- function(x) x + 1
unlockBinding("C_rf", as.environment("package:stats"))
assignInNamespace("C_rf", bar, ns="stats", pos="package:stats")
assign("C_rf", bar, pos="package:stats")
lockBinding("C_rf", as.environment("package:stats"))
# Error in unlockBinding("C_rf", as.environment("package:stats")) :
# no binding for "C_rf"
Is it possible to modify f
?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…