R - 调度到新方法时出现问题 print.list()

R - problems dispatching to new method print.list()

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

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

有关我如何开始使用此类函数定义的更多背景信息,请参见

编辑:我尝试了其他一些没有用的东西...

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

非常感谢任何帮助。

正如源代码中的注释中所解释的(有点神秘)(可用 here) and stated (more explicitly) in 、"auto-printing"(例如,与 (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