R Change Base Package Function
R Change Base Package Function
I had an error with installing R packages (unable to move temporary file
), but it can be solved by editing utils:::unpackPkgZip
to increase the time between the unzipping, i.e.
unable to move temporary file
utils:::unpackPkgZip
trace(utils:::unpackPkgZip, edit = TRUE)
and on line 142 I increase
Sys.sleep(0.5)
to 2.5. My question is if there is a way to do this automatically on startup, or to just change the function altogether, so I do not have to repeat this every session.
I tried to put
unlockBinding("unpackPkgZip", getNamespace("utils"))
utils::assignInNamespace("unpackPkgZip", new_fct, "utils", getNamespace("utils"))
lockBinding("unpackPkgZip", getNamespace("utils"))
where new_fct
is the function with changed value, in my .Rprofile`s .First()
function, but then I get
new_fct
.First()
Error in utils::assignInNamespace("unpackPkgZip", function(pkg, pkgname, :
locked binding of ‘unpackPkgZip’ cannot be changed
I think it is because .First()
executes before .First.sys()
.First()
.First.sys()
Edit: I just realized that in assignInNamespace
itself, it states
assignInNamespace
if (ns_name %in% tools:::.get_standard_package_names()$base)
stop("locked binding of ", sQuote(x), " cannot be changed",
domain = NA)
So I suppose it is not possible.
1 Answer
1
You don't need to modify the function itself,
you can let trace execute code at specified lines with its at
parameter.
Its documentation states:
at
When the at
argument is supplied, it can be a vector of integers referring to the substeps of the body of the function (this only works if the body of the function is enclosed in ...). In this case tracer is not called on entry, but instead just before evaluating each of the steps listed in at. (Hint: you don't want to try to count the steps in the printed version of a function; instead, look at as.list(body(f)) to get the numbers associated with the steps in function f.)
at
Take cat
as an example:
cat
as.list(body(cat))
[[1]]
`{`
[[2]]
if (is.character(file)) if (file == "") file <- stdout() else if (substring(file,
1L, 1L) == "|")
file <- pipe(substring(file, 2L), "w")
on.exit(close(file))
else
file <- file(file, ifelse(append, "a", "w"))
on.exit(close(file))
[[3]]
.Internal(cat(list(...), file, sep, fill, labels, append))
The default behavior:
system.time(cat("I'm fastn"))
I'm fast
user system elapsed
0 0 0
Now with injected code:
trace(cat, quote(Sys.sleep(2)), at=3L, print=FALSE)
system.time(cat("now I'm slown"))
now I'm slow
user system elapsed
0.008 0.000 2.001
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Comments
Post a Comment