【问题标题】:KableExtra does not work well with tablebyKableExtra 不适用于 tableby
【发布时间】:2020-08-10 11:02:12
【问题描述】:

我想做一个性别和疾病之间的列联表。当我将 R.markdown 用于 pdf 时。报告,我使用kableExtra 来自定义表格。当它们不是 data.frame 时,KableExtra 不能很好地制作表格。所以他们用tableby做了丑陋的桌子

有了这个data.frame,我得到了。

library(kableExtra)
library(arsenal)
set.seed(0)
Disease<-sample(c(rep("Name of the first category of the disease",20),
rep("Name of the Second category of the disease",32),
rep("Name of the third category of the disease",48),
rep("The category of those who do not belong to the first three categories",13)))
ID<-c(1:length(Disease))
Gender<-rbinom(length(Disease),1,0.55)
Gender<-factor(Gender,levels = c(0,1),labels = c("F","M"))

data<-data.frame(ID,Gender,Disease)

当我使用 R.markdown (pdf) 运行此分析的结果时,我得到了这种表格

有两个问题,渴了KableExtra::不处理字符&amp;nbsp;&amp;nbsp;&amp;nbsp; 其次,当我将tablebykableExtra 一起使用时,我无法自定义列宽,因为我想扩大包含变量名称的列,因为我确实在处理变量值名称很长的数据。但是如果我使用knitr::kable,则字符&amp;nbsp;&amp;nbsp;&amp;nbsp;被删除,但是表格没有按比例缩小,并且没有显示一部分。我认为knitr 有很多限制。

我该如何处理这个问题?或者是否有另一个函数可以在 R.markdown(pdf 格式)中使用 p.value 制作漂亮的列联表。

【问题讨论】:

  • 你有没有找到解决这个问题的方法?

标签: r knitr kable kableextra


【解决方案1】:

为避免在使用 knitr 时将 &amp;nbsp;&amp;nbsp;&amp;nbsp; 添加到您的输出中,请在块设置选项中使用 results='asin',例如:

{r results='asis'}

您可以使用 print 函数中的 width 选项来控制包含变量名称的列的宽度。从技术上讲,您不需要将摘要调用包装在 print 中,但这样做不会改变任何内容,并且您可以调整宽度设置。

所以,对于你的例子:

print(summary(tableby(Gender~Disease)), width = 20)

应该使它在呈现为 pdf 时更具可读性。您可以更改宽度,它将在您设置的限制处换行。

使用您示例中的代码和上述函数调用,表格在 knit 为 pdf 时如下所示:

【讨论】:

  • 我的代码块设置选项中有results="asis",但我的表格(输出为HTML)仍然在x 变量名称之前打印&amp;nbsp;&amp;nbsp;&amp;nbsp;。 :( 将 text = TRUE 添加到 summary() 消除了  s,但在适当的位置生成了一个连字符,它被呈现为列表中的公告点,这是最接近理想格式的,如上述答案所示。我还尝试了 summary()text = 参数中的其他选项,但没有任何效果。任何帮助将不胜感激!
  • 我在上面发布的解决方案适用于 pdf/latex,尽管我已经能够将 summary() 调用传递给 as.data.frame(text = "latex") %> % kable("latex") 修改 kable 输出。对于 html,我可以通过以下调用复制上述解决方案(使用 {r results = 'asis'}):print(summary(tableby(Gender~Disease)), width = 45)
猜你喜欢
  • 1970-01-01
  • 2020-03-31
  • 1970-01-01
  • 1970-01-01
  • 2014-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多