What ways are there to edit a function in R?
Or take a look at the debugging function trace()
. It is probably not exactly what you are looking for but it lets you play around with the changes and it has the nice feature that you can always go back to your original function with untrace()
.trace()
is part of the base
package and comes with a nice and thorough help page.
Start by calling as.list (body(foo))
to see all the lines of your code.
as.list(body(foo))
[[1]]
`{`
[[2]]
line1 <- x
[[3]]
line2 <- 0
[[4]]
line3 <- line1 + line2
[[5]]
return(line3)
Then you simply define what to add to your function and where to place it by defining the arguments in trace()
.
trace (foo, quote(line2 <- 2), at=4)
foo (2)
[1] 4
I said in the beginning that trace()
might not be exactly what you are looking for since you didn't really change your third line of code and instead simply reassigned the value to the object line2
in the following, inserted line of code. It gets clearer if you print out the code of your now traced function
body (foo)
{
line1 <- x
line2 <- 0
{
.doTrace(line2 <- 2, "step 4")
line3 <- line1 + line2
}
return(line3)
}
Modify package function
I finally found a solution that should work in all situations!
environment(customGenomePlot) <- asNamespace('snapCGH')
assignInNamespace("genomePlot", customGenomePlot, ns = "snapCGH")
The call to environment()
assures that the function will be able to call other hidden functions from the package.
The call to assignInNamespace()
assures that other functions from the package will call your updated version of the function.
It is possible that in certain situation, you need only one of these, but in general you need both. I struggled to find this general solution, found many other which are not working in some cases, like this (need opposite order), or this (misses the second part), or this (throws the error "cannot add bindings to a locked environment").
Customize existing function in R
find the location in the function that you want to change
as.list(body(psych::polychoric))
Change the function
trace(psych::polychoric, quote(nvalues > 10), at=11)
Check to see that you changed what you want to change
trace(psych::polychoric, edit=TRUE)
Set the function back to original
untrace(psych::polychoric)
-----
Seems like fix
may be easier for you to implement for this task
fix(polychoric)
opens a pane that you can change the code in - change and hit save.
This will make the function local to your global environment you can check this by looking at the original function trace(polychoric, edit = T)
will show nvalues > 10
, and trace(psych::polychoric, edit = T)
will show nvalues > 8
. The next time you reload psych
you will be using the original function. Bit of a manual hack - but hopefully works for this one off situation.
How to edit a function with a specific environment defined in R
As you are just creating a new function in the GlobalEnvironment
it doesn't know where to find functions that are not exported from the pacakge of the original function. You just need to tell it explicitly where to find them using the :::
operator. So e.g.
cv.glm2 <-
function (data, glmfit, cost = function(y, yhat) mean((y - yhat)^2),
K = n)
{
# // Some other code here // #
# Tell it which package the unexported function resides in
s <- boot:::sample0(rep(1L:K, f), n)
}
Editing a function from a package in R?
In the beginning of the package there is a line
data(sysdata, envir=environment())
See here: https://github.com/cran/referenceIntervals/tree/master/data/sysdata.rda
I suspect that "nonparRanks" is defined there as I don't see it defined anywhere else. So perhaps you could download that file, write your own function, then run that same line before running your function and it may work.
EDIT:
Download the file then run:
load("C:/sysdata.rda")
With your path to the file and then your function will work.
How to modify pre-existing function in local environment in R
If New
is the new function copied from camtrapR then use
environment(New) <- asNamespace("camtrapR")
to ensure that the function calls in its body are looked up in the correct places.
Related Topics
How to Format a Number as Percentage in R
Pass Arguments to Dplyr Functions
Combine Two or More Columns in a Dataframe into a New Column With a New Name
How to Number/Label Data-Table by Group-Number from Group_By
Remove Columns from Dataframe Where All Values Are Na
How to Save Plots That Are Made in a Shiny App
Call Apply-Like Function on Each Row of Dataframe With Multiple Arguments from Each Row
Ignore Outliers in Ggplot2 Boxplot
Cumulatively Paste (Concatenate) Values Grouped by Another Variable
Ggplot2 Change Axis Limits For Each Individual Facet Panel
Incomplete Final Line' Warning When Trying to Read a .Csv File into R
How to Replace Na With Mean by Group/Subset
What Is the Width Argument in Position_Dodge
Multiple Plots in For Loop Ignoring Par
Why Is Rbindlist "Better" Than Rbind