rroxygen2roxygen

Example for S3 method roxygen documentation not working?


When I use devtools::check() on my package, it produces an error suggesting my example doesn't work. When just running the function normally outside of the package it works just fine, so I'm wondering what the issue could be?

It produces error:

 Error in UseMethod("decision_lm") : 
    no applicable method for 'decision_lm' applied to an object of class "function"
  Calls: decision_lm
  Execution halted

This is my roxygen skeleton

#' Decision of linear regression test
#'
#' @description Gives the decision of retention or rejection for a linear
#' regression test
#'
#' @param fit1_lm list
#' @param ... other arguments
#'
#' @return characters
#'
#' @examples
#' fit1_lm <- fit_lm <- function(dat1) {
#' model <- lm(dat1$weight ~ dat1$height)
#' f1 <- summary(model)$coefficients[2,1]
#' f2 <- confint(model)[2,1]
#' f3 <- confint(model)[2,2]
#' f4 <- summary(model)$coefficients[2,3]
#' f5 <- df.residual(model)
#' f6 <- summary(model)$coefficients[2,4]
#' structure(
#'   list(beta_hat = f1, CI_Lower = f2, CI_Upper = f3, t.value = f4, df = f5, p.value = f6),
#'   class = "mytest") # Returns object of class mytest
#' }
#' fit1_lm(project2022)
#' decision_lm(fit1_lm)
#' @export
decision_lm <- function(fit1_lm, ...) {
  UseMethod("decision_lm")
}
#' @method decision_lm mytest
#' @export
decision_lm.mytest <- function(fit1_lm, ...) {
  if (fit1_lm[6] <= 0.05) {
    glue::glue("We reject the null hypothesis, as the p-value <= 0.05")
  } else if (fit1_lm[6] > 0.05) {
    glue::glue("We do not reject the null hypothesis, as the p-value > 0.05")
  }
}

Solution

  • The error says no applicable method for 'decision_lm' applied to an object of class "function", which tells you that the object you passed to decision_lm is a function, not an object for which you've defined a method.

    In fact, your example has decision_lm(fit1_lm), so it is trying to use the fit1_lm function as the argument, not the result of calling that function.

    If you change your example to something like

    #' fit <- fit1_lm(project2022)
    #' decision_lm(fit)
    

    it should work.