首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >向新方法print.list()分派r-问题

向新方法print.list()分派r-问题
EN

Stack Overflow用户
提问于 2018-12-27 05:15:23
回答 1查看 331关注 0票数 3

在我的摘要工具包中,我成功地定义了print.by方法。然而,尽管我对print.list遵循了完全相同的步骤,但是调度失败了。

该方法似乎已与包中定义的其他两个打印方法一起注册:

代码语言:javascript
复制
grep("print(\\.summarytools$|\\.by$|\\.list$)", methods("print"), value = TRUE)
[1] "print.by"           "print.list"         "print.summarytools"

在命名空间中,我有:

代码语言:javascript
复制
S3method(print,by)
S3method(print,list)
S3method(print,summarytools)

示例

代码语言:javascript
复制
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

和..。

代码语言:javascript
复制
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的内容

代码语言:javascript
复制
#' 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)的调用;仍然没有好的结果(我不太确定我是否得到了“签名”参数应该是什么。我发现有关它的文档相当令人困惑)。

我开始觉得我需要用活性氧来做点小转折.但我不知道怎么回事。

任何帮助都很感激。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-12-31 02:19:24

正如源代码中的注释(可用的这里)和这个堆栈溢出回答引用了它们。中(更明确地)所解释的那样,“自动打印”(例如,使用(list_obj <- lapply(tobacco[,c(1,3)], freq)))只能在显式类上进行调度,因此不能处理列表。不过,它将与任何打印调用一起工作:

代码语言:javascript
复制
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
代码语言:javascript
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53940059

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档