【问题标题】:Kable not displaying tables properlyKable 无法正确显示表格
【发布时间】:2019-02-25 15:36:25
【问题描述】:

我一直在尝试在文档中显示表格。当我使用kable 时,docx 中的输出不会显示为表格。我可以使用pander 正确生成表格,但这不是最佳选择,因为我将无法生成标题。

--- 
title: "My Title"
output:
  bookdown::word_document2:
    fig_caption: yes
    reference_docx: G:/My Drive/Projects/R15_Pipeline/R15Dir/styles/Brain_template.docx
  bookdown::pdf_document2:
    toc: no
link-citations: no
site: bookdown::bookdown_site
bibliography: G:/My Drive/ZoteroRPlugin/Report_references.bib
csl: G:/My Drive/ZoteroRPlugin/styles-master/dependent/brain.csl
---


```{r setup, include=FALSE, cache=FALSE, echo=FALSE}
#create a table
smoke <- matrix(c(51,43,22,92,28,21,68,22,9),ncol=3,byrow=TRUE)
colnames(smoke) <- c("High","Low","Middle")
rownames(smoke) <- c("current","former","never")
smoke <- as.data.frame(smoke)
```

Here is an example using pander where the table is displayed properly but no caption. For example Table \@ref(tab:table1)

```{r table1, echo = FALSE}
pander(smoke, booktabs=T, caption = "Descriptives per Group")
```

Here is an example using kable. Table \@ref(tab:table2) which does not work

```{r table2, echo = FALSE}
knitr::kable(smoke, format="markdown", caption = "Descriptives per Group")
```

【问题讨论】:

  • 您的示例不可重现(我们没有您的 G: 驱动器)。当我删除这些行时,我不明白你的描述。所以也许你需要更新你的包,或者你 G 上的那些文件中可能有一些东西:驱动器搞砸了。
  • 嗯,好的,谢谢,今晚晚些时候我会解决这个问题。感谢您的意见
  • 感谢您的帮助。我能够找到错误的根源。虽然我不确定它为什么会发生。在我的代码中,我使用以下代码加载数据

标签: r-markdown knitr pandoc kable


【解决方案1】:

感谢您的帮助。我能够找到错误的根源。虽然我不确定它为什么会发生。在我的代码中,我使用以下代码加载数据

pacman::p_load(apastats,kableExtra, grid, captioner, citr)
#devtools::install_github("haozhu233/kableExtra", force = T)
load("data/data.RData") #load the data from createTablePlots.R
load("data/data2.RData") #load the data from newStatsSigTables.R
load("data/tables.RData") #load the data from createTablePlots.R
load("data/TrialErrorsTables.RData") #load the data from newStatsSigTable.R
load("data/statsProj2.RData")

此代码将导致错误,如屏幕截图所示。经过一些试验和错误后,这个错误似乎与kableExtra 包冲突。当我从行中删除这个包时,我能够得到正确的输出。我已在 KableExtra 的 GitHub 上将此报告为错误

【讨论】:

  • 只是为了跟进另一个用户遇到了同样的问题,可以在GitHub上找到
猜你喜欢
  • 2021-06-06
  • 2011-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多