简体   繁体   English

从.rmd编织到Word时如何格式化kable表(带书签)

[英]How to format kable table when knit from .rmd to Word (with bookdown)

I have read the bookdown book and still cannot figure this out. 我已经阅读了这本书 ,但仍然无法弄清楚这一点。 I am trying to create a Word report through bookdown . 我正在尝试通过bookdown创建Word报告。 I want to use kableExtra to add striping to my tables and also to bold my last table row. 我想使用kableExtra为我的表添加条带化,并加粗我的最后一个表行。 Can kableExtra be used when knitting to Word ? 编织到Word时可以使用kableExtra吗?

This is a subset of my code : 这是我的代码的一个子集:

library(dplyr)    
knitr::opts_chunk$set(echo = TRUE)
library(knitr)  # required for kable
library(kableExtra)  # required for kableExtra
options(knit.r.table.format = "markdown")

myRegion <- c("a", "b", "c")
Current_Perc_1 <- c(85.9, 90.8, 89.7)
Current_Perc_2 <- c(88.0, 91.0, 89.0)
tab_curr_est_2_times <- cbind(myRegion, Current_Perc_1, Current_Perc_2)
tab_curr_est_2_times <- as.data.frame(tab_curr_est_2_times, stringsAsFactors = FALSE)
tab_curr_est_2_times$Current_Perc_1 <- as.double(tab_curr_est_2_times$Current_Perc_1)
tab_curr_est_2_times$Current_Perc_2 <- as.double(tab_curr_est_2_times$Current_Perc_2)
tab_curr_est_2_times$curr_change_1_to_2 <- tab_curr_est_2_times$Current_Perc_2 - tab_curr_est_2_times$Current_Perc_1

tab_1_curr <- tab_curr_est_2_times
tab_1_curr[ nrow(tab_1_curr)+1 , ] <- NA
tab_1_curr$myRegion[ nrow(tab_1_curr) ] <- "BRITISH COLUMBIA"
tab_1_curr$Current_Perc_1[ nrow(tab_1_curr) ] <- 88.4
tab_1_curr$Current_Perc_2[ nrow(tab_1_curr) ] <- 89.3
tab_1_curr$curr_change_1_to_2[ nrow(tab_1_curr) ] <- 0.9

knitr::kable(tab_1_curr, digits = 1, align = "lccc", position = "c", 
         caption = "\\: my table caption here") %>%
  kable_styling("striped") %>%
  row_spec(nrow(tab_1_curr), bold = TRUE)

My bookdown settings are as follows: 我的预订设置如下:

--- 
title: "My Report"
author: "Me"
date: "`r Sys.Date()`"
site: "bookdown::bookdown_site"
output:
  bookdown::word_document2:
    fig_caption: true
documentclass: book
---

When I click on the Knit button in RStudio, I get this table: 当我点击RStudio中的Knit按钮时,我得到了这个表:

I want the last row to be bold and I want the table striped. 我希望最后一行是粗体,我希望表格条纹。 How do I do this ? 我该怎么做呢 ? (I also get the following error: "Currently generic markdown table using pandoc is not supported.") (我也收到以下错误:“目前不支持使用pandoc的通用降价表。”)

This was not possible but since pandoc V2 is out, you can do it with package flextable (>= 0.4.0) (and pandoc V2 ). 这是不可能的,但由于pandoc V2已经用完,你可以使用包flextable (>= 0.4.0) (和pandoc V2 )来实现。 Below the code you should add into a code chunk: 在代码下面,您应该添加到代码块中:

library(magrittr)
library(flextable)

tab_1_curr <- structure(list(myRegion = c("a", "b", "c", "BRITISH COLUMBIA"
 ), Current_Perc_1 = c(85.9, 90.8, 89.7, 88.4), Current_Perc_2 = c(88, 
 91, 89, 89.3), curr_change_1_to_2 = c(2.09999999999999, 0.200000000000003, 
 -0.700000000000003, 0.9)), .Names = c("myRegion", "Current_Perc_1", 
 "Current_Perc_2", "curr_change_1_to_2"), row.names = c(NA, 4L
 ), class = "data.frame")


regulartable(tab_1_curr) %>% 
  bold(i = ~ myRegion %in% "BRITISH COLUMBIA") %>% 
  theme_zebra() %>% 
  autofit()

The huxtable package is available. 可以使用huxtable包。 It includes similar table customization tools as kableExtra . 它包括与kableExtra类似的表自定义工具。 huxtable is designed to output to LaTeX/PDF and HTML (similar to kableExtra ). huxtable旨在输出到LaTeX / PDF和HTML(类似于kableExtra )。 However, huxtable also includes a as_flextable function to convert a huxtable object to a flextable object, which can be output to Word (as noted by David above). 但是, huxtable还包括一个as_flextable函数,用于将一个huxtable对象转换为一个flextable对象,该对象可以输出到Word(如上面David所述)。 After a lot of searching, it seems to me like huxtable is the only available package that can easily output to all of Word, HTML, and PDF with a single package. 经过大量的搜索,在我看来, huxtable是唯一可以使用单个包轻松输出到Word,HTML和PDF的可用包。

Pandoc Pandoc

The conversion to word is made via pandoc . 转换为单词是通过pandoc Currently pandoc only creates four type of tables, 目前pandoc只创建四种类型的表,

  • simple tables 简单的表格
  • multiline_tables multiline_tables
  • grid_tables grid_tables
  • pipe_tables pipe_tables

Some of those supported formats are demontrated in pander and in the pandoc manual p 35-39. 一些的那些支持的格式demontrated在奉迎并在pandoc手动 p 35-39。

So you cannot create the stripped table currently with pandoc. 因此,您无法使用pandoc创建当前剥离的表

You also have a good summary of how you can use tables in rmarkdown.rstudio . 您还可以很好地总结如何在rmarkdown.rstudio中使用表格。

Pandoc v2 潘多克v2

see the good news from David below 看下面大卫的好消息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM