从 .rmd 编织到 Word 时如何格式化 ka​​ble 表(使用 bookdown) [英] How to format kable table when knit from .rmd to Word (with bookdown)

查看:48
本文介绍了从 .rmd 编织到 Word 时如何格式化 ka​​ble 表(使用 bookdown)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了 和 pandoc 手册第 35-39 页.

因此您无法创建当前使用 pandoc 的剥离表.

您还很好地总结了如何使用 rmarkdown.rstudio 中的表格.

Pandoc v2

看看下面大卫的好消息

I have read the bookdown book and still cannot figure this out. I am trying to create a Word report through bookdown. I want to use kableExtra to add striping to my tables and also to bold my last table row. Can kableExtra be used when knitting to Word ?

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:

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

The conversion to word is made via pandoc. Currently pandoc only creates four type of tables,

  • simple tables
  • multiline_tables
  • grid_tables
  • pipe_tables

Some of those supported formats are demontrated in pander and in the pandoc manual p 35-39.

So you cannot create the stripped table currently with pandoc.

You also have a good summary of how you can use tables in rmarkdown.rstudio.

Pandoc v2

see the good news from David below

这篇关于从 .rmd 编织到 Word 时如何格式化 ka​​ble 表(使用 bookdown)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆