1

I have inadvertently over-ridden the package I created with a different function, saved it, and closed R Studio. Now, my R package contains an unintended function.

Thankfully, I did not install the package, so I still have the old package contents stored in my computer.

Is there a way to extract the function from the installed package? It's one long function. Not more than one function.

And, no, I do not have a backup, at least not the updated version.

4
  • 6
    I am not sure I understand what happened. However if you have an old version of the package, you can load the package and then just type the function name and its code will be sent to the terminal. You could then copy and paste that code. Commented Aug 31, 2017 at 20:14
  • What @JohnPaul commented is better but functionBody() also works (most of the times). Commented Aug 31, 2017 at 20:41
  • You might be automatically loading everything in the .RData file when you start a new R session. Try rm(name_of_function) and see if you can use the package version afterwards. Commented Aug 31, 2017 at 20:58
  • Johns method did the trick! Commented Aug 31, 2017 at 21:10

2 Answers 2

2

View(package::function)

Where package is the package you mentioned you had installed and function is the function you're looking to to inspect.

The important thing is to forego the parenthesis where you would normally have the function argument. This will open the function code for inspection.

Sign up to request clarification or add additional context in comments.

Comments

1

You can view the structure of a function by typing it's name in the console.

> sum
function (..., na.rm = FALSE)  .Primitive("sum")

To get the function from a package, you can use the :: operator

> dplyr::coalesce
function (x, ...) 
{
    values <- list(...)
    for (i in seq_along(values)) {
        x <- replace_with(x, is.na(x), values[[i]], paste0("Vector ", 
            i))
    }
    x
}
<environment: namespace:dplyr>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.