Rmarkdown - 打印对象列表而不显示那些讨厌的 [[indices]]

Dom*_*ois 2 knitr r-markdown htmltools summarytools

这会有点罗嗦,因为如果没有适当的上下文,我不知道如何提出我的问题。

没有过多介绍细节,在我的summarytools包中,我by()通过使用包装函数来处理对象print.summarytools,即view(). 此view()函数可以识别通过by或创建的对象lapply(),并将其组件分派到包的print(),以及与标题和脚注相关的适当参数,等等(第一个元素不会接收与第二个或最后一个相同的参数)。

链接到包开发页面:

该软件包提供了两种呈现内容的方式:通过 rmarkdownpander和使用 RStudio 的htmltools. 就rmarkdown/pander而言,我取得了令人满意的结果。对于 rmarkdown 文档中的 HTML 渲染,情况并非如此。

例如:

library(devtools)
install_github("dcomtois/summarytools", ref = "dev-current")
library(summarytools)

# call the descr() function through by() to get stats by gender
groupstats <- by(data = exams, INDICES = exams$gender, FUN = descr)

# Use the view() function to neatly display results
view(groupstats, method = 'render')
Run Code Online (Sandbox Code Playgroud)

此示例可作为github 上Gist 使用

编织后的结果:

呈现的 html 内容

我尝试了几件事,都不满意。由于 summarytool 的 print 方法返回的对象属于 class shinytag,(也许)理想的解决方案是将所有这些组合到一个 class 列表中shinytag,但我不知道用htmltools. 在我看来,精心挑选列表元素会带来麻烦,因为那里有很多列表嵌套。

我试过了lapply(groupstats, print, method = 'render'),但后来[[n]]我没有$names出现,而是出现了。

所以我的问题是:我怎样才能摆脱[[n]]输出中的's ?

包源代码 对于包的源代码的初步认识夹头,看到summarytool的DEV-当前分支R/view.R文件,行〜78 -116。

use*_*330 6

如果您有一个list()将打印为 HTML 的项目,您可以使用将它们放在一起htmltools::tagList()并且列表的索引不会显示。例如,

library(htmltools)
thelist <- list(a, b)
Run Code Online (Sandbox Code Playgroud)

可能会像您的示例中那样显示[1][2],但是

tagList(thelist)
Run Code Online (Sandbox Code Playgroud)

或者

tagList(a, b)
Run Code Online (Sandbox Code Playgroud)

将只显示这两个项目。