3

在我的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()函数和“签名”参数,但由于函数很可能会在没有参数的情况下被调用,所以我看不出这会有什么帮助。

两者之间的一个区别是print.by存在于base包中,而print.list没有。但我无法确定这是否相关。

关于我如何使用这种类型的函数定义的更多背景信息可以在我之前提出的这个问题中找到。

编辑:我尝试了其他一些不起作用的东西......

  • 按照这里的建议,重新定义print.default而不是定义,但它仍然不起作用。print.list
  • 在函数定义()之后添加对 setMethod 的调用setMethod(f = "print", signature = "list", definition = print.list);仍然没有好的结果(我不太确定我得到了“签名”参数应该是什么。我发现关于它的文档相当混乱)。

我开始认为我可能需要对 Roxygen 进行一些改动才能使其正常工作……但是什么改动,我不知道。

非常感谢任何帮助。

4

1 回答 1

1

正如源代码(可在此处获得)中的注释中解释(有点神秘)以及在引用它们的此 Stack Overflow 答案中(更明确地)说明的那样,“自动打印”(例如,与 一起发生(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.00
print(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.00
于 2018-12-31T02:19:24.700 回答