How to add new slot to already existing class?

2.4k Views Asked by At

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?

1

There are 1 best solutions below

0
On

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.