Tae-Sung Shin
Tae-Sung Shin

Reputation: 20643

How to inherit a package or a class in R?

I am using a package where I want to add an additional option to its main function and change the content in the main function accordingly. Other than that, I would like to use the package as it is right now.

What is the best way to do this? Any references would be appreciated.

Upvotes: 1

Views: 186

Answers (2)

Tae-Sung Shin
Tae-Sung Shin

Reputation: 20643

This is what I did.

  1. Create another function copied from the main function of the given package.
  2. Change the function as you want.
  3. If you need to use internal function of the package (or class), use <package name>::: in front of the function name.
  4. Make your result inherited from the class you want.

For example, class(results) <- c("rpart","nnet")

Upvotes: 0

cbeleites
cbeleites

Reputation: 14093

If you think your change is needed by the rest of the world, too:

  1. Contact the maintainer (packageDescription ("pkgName") $ Maintainer),
  2. explain your change and
  3. attach patches for code and documentation to the email?

If only you yourself need the change / want to try around locally:

  • ? fix
  • see also ? assignInNamespace
  • get the source (packages on CRAN should be FOSS, but better read the license), make your chagne & install that package locally.

  • if you need to insert a bit of code (like an extra output statement): ? trace

Upvotes: 2

Related Questions