Change function from package

I want to edit a function from a package. I tried both "edit" and "fix", however it seems that my changes don't get saved. Lets say the function I want to changes is named "my_func", the package is called "your_package". The code I then ran was my_func <- fix(my_func). What am I doing wrong here?

Copy the code for the original function (the function definition) into a source file, maybe save it as my_func.R if you want to keep it.

Edit the code to make the changes you wish to make. Then re-assign the new code to the function name by running the function definition - I would do this by placing the cursor anywhere within the definition code (safest to have it at the first line I think) and then pressing Ctrl+Enter (in RStudio). This re-defines the function within your current global environment.

The package name thing seems irrelevant here - you can't permanently alter someone else's package directly, but you can copy and extract code from it and edit that (but check the licence of the package). Or you can clone their package and make your own version of it (on github this can be done by forking a repository branch).

Example of a function definition:

# returns the square of the input
my_func <- function(x) {
  x^2
}

I already tried the first approach and didn't work unfortunately. But thanks for the suggestion with forking the repository and changing my stuff there.

OK. We are able to help more if you give an example (simplified if necessary) of the code you have tried to run, and then any error messages or outputs.
It's hard to help with a problem if all the information we have is "it didn't work". Please post again with some code if you need further help.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.