How do I implement a customized plot method for an S4 object in my R package using roxygen2?

Viewed 12

I am trying to implement a customized plot function for my R package (using RStudio), but how do I do this using roxygen2? So far, I have the following code in a file called plots_fn.R:

plot <- function(x, ...) UseMethod("plot")

@title Plotting function.
@description A function that produces plots using the model object.
@param obj A model object.
@returns A plot based on the model.
@export
plot.obj <- function(obj,...) {plot(obj@x, obj@y*obj@y)}

The object is created via a function called f1 that is defined in a file called fns.R (i.e. the function runs properly):

obj1 <- f1(x = c(1,2,3,4,5))

The following code runs successfully:

plot.obj(obj1)

However, when I try the following code

plot(obj1)

An error surfaces

Error in as.double(y) : cannot coerce type 'S4' to vector of type 'double'

Note that one of entries in the NAMESPACE file has the entry

S3method(plot,obj)

I want to call the function using plot(obj1). How do I fix this error?

0 Answers
Related