在我的
summarytools
包,我已成功定义
print.by
方法。然而,即使我已经按照同样的步骤
print.list
,调度失败。
该方法似乎已注册,以及包中定义的其他两种打印方法:
grep("print(\\.summarytools$|\\.by$|\\.list$)", methods("print"), value = TRUE)
[1] "print.by" "print.list" "print.summarytools"
在命名空间中,我有:
S3method(print,by)
S3method(print,list)
S3method(print,summarytools)
例子
devtools::install_github("dcomtois/summarytools", ref = "dev-current")
library(summarytools)
list_obj <- lapply(tobacco[,c(1,3)], freq))
## $gender
## For best results printing list objects with summarytools, use view(x, method = 'pander')
## Frequencies
## tobacco$gender
## Type: Factor
##
## Freq % Valid % Valid Cum. % Total % Total Cum.
## ----------- ------ --------- -------------- --------- --------------
## F 489 50.00 50.00 48.90 48.90
## M 489 50.00 100.00 48.90 97.80
## <NA> 22 2.20 100.00
## Total 1000 100.00 100.00 100.00 100.00
##
## $age.gr
## Frequencies
## tobacco$age.gr
## Type: Factor
##
## Freq % Valid % Valid Cum. % Total % Total Cum.
## ----------- ------ --------- -------------- --------- --------------
## 18-34 258 26.46 26.46 25.80 25.80
## 35-50 241 24.72 51.18 24.10 49.90
## 51-70 317 32.51 83.69 31.70 81.60
## 71 + 159 16.31 100.00 15.90 97.50
## <NA> 25 2.50 100.00
## Total 1000 100.00 100.00 100.00 100.00
与…比较
summarytools:::print.list(list_obj)
## Frequencies
## tobacco$gender
## Type: Factor
##
## Freq % Valid % Valid Cum. % Total % Total Cum.
## ----------- ------ --------- -------------- --------- --------------
## F 489 50.00 50.00 48.90 48.90
## M 489 50.00 100.00 48.90 97.80
## <NA> 22 2.20 100.00
## Total 1000 100.00 100.00 100.00 100.00
##
## tobacco$age.gr
## Type: Factor
##
## Freq % Valid % Valid Cum. % Total % Total Cum.
## ----------- ------ --------- -------------- --------- --------------
## 18-34 258 26.46 26.46 25.80 25.80
## 35-50 241 24.72 51.18 24.10 49.90
## 51-70 317 32.51 83.69 31.70 81.60
## 71 + 159 16.31 100.00 15.90 97.50
## <NA> 25 2.50 100.00
## Total 1000 100.00 100.00 100.00 100.00
这是的内容
print.list.R
:
#' Print Method for Objects of Class \dQuote{list}.
#'
#' Displays a list comprised of summarytools objects created with \code{lapply}.
#'
#' @usage
#' \method{print}{list}(x, method = "pander", file = "",
#' append = FALSE, report.title = NA, table.classes = NA,
#' bootstrap.css = st_options('bootstrap.css'),
#' custom.css = st_options('custom.css'), silent = FALSE,
#' footnote = st_options('footnote'),
#' escape.pipe = st_options('escape.pipe'), \dots)
#'
#' @inheritParams print.summarytools
#' @method print list
#' @export
print.list <- function(x, method = "pander", file = "", append = FALSE,
report.title = NA, table.classes = NA,
bootstrap.css = st_options('bootstrap.css'),
custom.css = st_options('custom.css'),
silent = FALSE, footnote = st_options('footnote'),
escape.pipe = st_options('escape.pipe'), ...) {
if (inherits(x[[1]], "summarytools")) {
view(x, method = method, file = file, append = append,
report.title = report.title, table.classes = table.classes,
bootstrap.css = bootstrap.css, custom.css = custom.css,
silent = silent, footnote = footnote, escape.pipe = escape.pipe,
...)
} else {
base::print.default(x, ...)
}
}
我已经阅读了一些与通用函数及其方法有关的文档,但我无法确定问题所在,也看不到解决方案。我看着
setMethod()
函数和“signature”参数,但是由于函数很可能在没有参数的情况下被调用,所以我不知道这有什么帮助。
两者的一个区别是
通过印刷
存在于
base
包装,而
打印列表
没有。但是我不能确定这是否相关。
有关如何使用这种类型的函数定义的更多背景信息,请参见
this question I asked earlier
.
编辑
我试过其他一些不起作用的东西…
-
重新定义
print.default
而不是定义
打印列表
,如建议的那样
here
但它仍然不起作用。
-
在函数定义之后添加对setmethod的调用(
setMethod(f = "print", signature = "list", definition = print.list)
)仍然没有好的结果(我不太确定“签名”参数应该是什么。我发现有关它的文档相当混乱)。
我开始觉得我需要和罗克西根做点小扭结才能让它发挥作用…但什么扭曲,我不知道。
任何帮助都非常感谢。