Tomas
Tomas

Reputation: 59597

How to add new slot to already existing class?

I have variable mod of class unmarkedFitPCount from package unmarked and I need to add a new attribute to that class:

mod@new_attr <- 1

I get an error:

‘new_attr’ is not a slot in class “unmarkedFitPCount”

I need to add this new attribute without creating a new derived class, because I need all those functions to work on this object. This is supposed to be just a very lightweight temporary hack. How can I do that?

Upvotes: 5

Views: 2578

Answers (1)

timothyjgraham
timothyjgraham

Reputation: 1232

What about: attributes(mod)$new_attr <- 1.

The core function attributes accesses an object's attributes. In this example you create a new attribute new_attr and assign it the value of 1.

You can then access the newly created attribute via attributes(mod)$new_attr.

Upvotes: 7

Related Questions