Jim O.
Jim O.

Reputation: 1111

How to extract functions used in R package?

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.

Upvotes: 1

Views: 2220

Answers (2)

Mark
Mark

Reputation: 4537

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>

Upvotes: 1

dshkol
dshkol

Reputation: 1228

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.

Upvotes: 2

Related Questions