在我的摘要工具包中,我成功地定义了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()函数和“签名”参数,但是由于函数很可能在没有参数的情况下被调用,所以我看不出这会有什么帮助。
两者的一个不同之处在于,print.by存在于base包中,而print.list不存在,但我无法确定这是否相关。
关于我如何使用这种函数定义的更多背景信息,可以在我刚才问的这个问题中找到。
编辑:我尝试过其他一些不起作用的东西.
print.default,而不是像建议的那样定义print.list,但是它仍然不能工作。setMethod(f = "print", signature = "list", definition = print.list))之后添加对setMethod(f = "print", signature = "list", definition = print.list)的调用;仍然没有好的结果(我不太确定我是否得到了“签名”参数应该是什么。我发现有关它的文档相当令人困惑)。我开始觉得我需要用活性氧来做点小转折.但我不知道怎么回事。
任何帮助都很感激。
发布于 2018-12-31 02:19:24
正如源代码中的注释(可用的这里)和这个堆栈溢出回答引用了它们。中(更明确地)所解释的那样,“自动打印”(例如,使用(list_obj <- lapply(tobacco[,c(1,3)], freq)))只能在显式类上进行调度,因此不能处理列表。不过,它将与任何打印调用一起工作:
devtools::install_github("dcomtois/summarytools", ref = "dev-current", quiet = TRUE)
library(summarytools)
# For best results, consider updating pander to its most recent version. You can do so
# by using devtools::install_github('rapporter/pander')
list_obj <- lapply(tobacco[,c(1,3)], freq)
list_obj # will not work since it uses auto-printing
# $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.00print(list_obj) # will work
# 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.00https://stackoverflow.com/questions/53940059
复制相似问题