【问题标题】:htmlTable in Rmd - conversion to Word docxRmd 中的 htmlTable - 转换为 Word docx
【发布时间】:2017-03-13 04:03:09
【问题描述】:

我有以下 Rmd 文件,它生成一个 html 文件,然后我将其复制粘贴到一个 docx 文件中(供合作者使用)。以下是我想知道如何处理表格的事情,但我无法在小插曲here 中找到答案:

A.我想知道如何删除 Cgroup 1 和 Cgroup 2 之间插入 Word 中的空白列。

B.我想知道如何使用行名设置列的宽度(“第一行”,...)

C.如何更改字体和字体大小?我尝试关注this,但没有输出:word_document with htmlTable()

D.为了简化到 Word 的转换,有没有办法指定分页符?横向?

非常感谢!

---
title: "Example"
output: 
  Gmisc::docx_document:
    fig_caption: TRUE
    force_captions: TRUE
---

Results
=======

```{r, echo = FALSE}
library(htmlTable)
library(Gmisc)
library(knitr)
mx <-
  matrix(ncol=6, nrow=8)
rownames(mx) <- paste(c("1st", "2nd",
                        "3rd",
                        paste0(4:8, "th")),
                      "row")
colnames(mx) <- paste(c("1st", "2nd",
                        "3rd", 
                        paste0(4:6, "th")),
                      "hdr")

for (nr in 1:nrow(mx)){
  for (nc in 1:ncol(mx)){
    mx[nr, nc] <-
      paste0(nr, ":", nc)
  }
}

htmlTable(mx,
          cgroup = c("Cgroup 1", "Cgroup 2"),
          n.cgroup = c(2,4))

```

【问题讨论】:

    标签: ms-word html-table r-markdown


    【解决方案1】:

    行名称的样式似乎已关闭,现在已在版本 1.10.1 中修复,您可以使用 devtools 包下载:devtools::install_github("gforge/htmlTable", ref="develop")

    关于样式,该函数几乎允许您想象任何CSS-style。不幸的是,它需要复制粘贴到 Word 中,而这个功能并不是微软的最高优先级。您可以使用 css.cell 轻松调整示例以适应所需的更改:

    library(htmlTable)
    library(knitr)
    mx <-
      matrix(ncol=6, nrow=8)
    rownames(mx) <- paste(c("1st", "2nd",
                            "3rd",
                            paste0(4:8, "th")),
                          "row")
    colnames(mx) <- paste(c("1st", "2nd",
                            "3rd", 
                            paste0(4:6, "th")),
                          "hdr")
    
    for (nr in 1:nrow(mx)){
      for (nc in 1:ncol(mx)){
        mx[nr, nc] <-
          paste0(nr, ":", nc)
      }
    }
    
    css.cell = rep("font-size: 1.5em;", times = ncol(mx) + 1)
    css.cell[1] = "width: 4cm; font-size: 2em;"
    htmlTable(mx,
              css.cell=css.cell,
              css.cgroup = "color: red",
              css.table = "color: blue",
              cgroup = c("Cgroup 1", "Cgroup 2"),
              n.cgroup = c(2,4))
    

    没有办法删除 cgroups 生成的空列。这是桌子看起来不错的必要条件,也是有意识的设计选择。

    关于分页符,我怀疑是否有任何优雅的方法可以做到这一点。一个替代方案可能是ReporteRs 包。我自己没有使用过它,但它与 Word 更紧密地集成在一起,可能是一个解决方案。

    【讨论】:

    • 谢谢!非常有帮助。我能够更改字体和行宽。但是,行宽不再受到许多列的尊重,有没有办法解决这个问题?如果有帮助,我可以在单独的问题中提供一个示例?
    猜你喜欢
    • 1970-01-01
    • 2021-10-05
    • 1970-01-01
    • 1970-01-01
    • 2017-07-20
    • 1970-01-01
    • 1970-01-01
    • 2017-09-23
    相关资源
    最近更新 更多