You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

S3方法roxygen文档示例失效:devtools::check报错求助

问题原因分析

错误的核心是示例代码未将fit1_lm(project2022)的运行结果赋值给变量,导致调用decision_lm(fit1_lm)时传入的是函数本身,而非预期的mytest类对象。

具体细节:

  • 你在示例中定义fit1_lm为函数后,执行fit1_lm(project2022)仅运行了函数,但未把返回的mytest类对象存回变量。此时fit1_lm仍然是一个function类的对象。
  • 你的decision_lm仅针对mytest类实现了方法,UseMethod找不到匹配function类的方法,因此抛出错误。

修正后的示例代码

fit1_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")
}
# 将函数运行结果赋值给变量
result_fit <- fit1_lm(project2022)
# 传入结果对象而非函数本身
decision_lm(result_fit)

额外优化建议

  • 原示例中fit1_lm <- fit_lm <- function(...)的双重赋值无必要,易造成混淆,建议只保留一个变量名。
  • decision_lm.mytest中用fit1_lm[6]访问p值不够直观,建议改用fit1_lm$p.value提升代码可读性。

内容的提问来源于stack exchange,提问作者Caitlin Luo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 08:56:07