r-markdown - Bookdown/Huskydown 中的表格,具有多种功能(引文、标题、URL、PNG 图……)
问题描述
我想在 R 降价文档(Bookdown/Huskydown)中包含一个表格,它应该满足以下要求。理想情况下,该表格适用于多种输出格式,例如 LaTex/PDF 和 HTML。
要求:
- 台面宽度:固定
- 单元格宽度:固定
- 垂直对齐:单元格内容与顶部对齐
- 文本格式:如粗体或斜体(最好是支持 md 格式,这样代码与输出无关)并允许在较长文本中换行
- 引文:应该呈现
- URL:作为 HTML 和 LaTex/PDF 中的可点击链接
- 数字:包括
- 本地存储的数字,无论是在
- 降价方式
![](Rlogo.png)
或 - 编织方式
knitr::include_graphics("Rlogo.png")
- 降价方式
- 直接来自网络的数字
- 本地存储的数字,无论是在
- 表的标题
- 字幕文本格式:字幕还应允许文本格式
- 脚注:在表格中包含脚注
- 表格编号:表格应该被编号
- 参考表格:在文档中是需要的
关于不同方法的说明
- 固定单元格宽度:在 Markdown 中,表头中“-”的数量确定单元格宽度
- 换行符:
- 乳胶:
\\linebreak
- 所有其他人:
<br/>
- 乳胶:
- 参考
- 乳胶:添加
\label{foo}
=>\ref{foo}
(\@ref(foo)
) - 降价:添加
Table: (\#tab:md-table) Caption
==>\@ref(tab:md-table)
)
- 乳胶:添加
对不同方法的评论
- Markdown:在 Markdown 中轻松编码表格
- Kable & kableExtra:表格的通用 R 降价编码,但垂直文本对齐模糊且 PDF 中不包含图形
- Pander:实现最多,但没有垂直对齐和脚注
- Huxtable:最有前途,但 PDF 中不包含数字
解决方案
这不是为上表提供 MWE 的答案
```{r}
# create some random text
library(stringi)
some_text <- stri_rand_lipsum(1)
some_text <- substr(some_text, 1, 75)
# create dataframe with some stuff
figpath <- "figure/"
df <- data.frame(
Citation = c("@R-base", "@R-bookdown"),
Textfield = c("**Formatted** string<br/> -- _Everyone_ needs H^2^O", some_text),
URL = c("[R-url](https://www.r-project.org/)", "[bookdown](https://bookdown.org/)"),
fig_local_md = c(
paste0("![](", figpath, "Rlogo.png){ width=10% height=5% }"),
paste0("![](", figpath, "bookdownlogo.png){ height='36px' width='36px' }")
)#,
# not working:
# fig_local_knitr = c("knitr::include_graphics('figure/Rlogo.png')", "knitr::include_graphics('figure/bookdownlogo.png')")
)
# only include if output format is HTML, else pander throws error
if (knitr::is_html_output()) {
df$fig_web <- c("![](https://www.picgifs.com/glitter-gifs/a/arrows/picgifs-arrows-110130.gif)")
output_format <- "html"
}
if (knitr::is_latex_output()) {
output_format <- "latex"
}
```
降价
Table: markdown table: markdown styling works in HTML (*italics*, **bold**), LaTex styling in PDF (\\textbf{bold})
| Image | Description |
| :--------------------------------------------------------- | :----------------------------------------------------------- |
| ![](figure/Rlogo.png){ width=10% height=5% } | **Image description** [@R-base] <br/>Lorem ipsum dolor sit amet, ... [R-url](https://www.r-project.org/) |
| ![](figure/bookdownlogo.png){ height='36px' width='36px' } | **Image description** [@R-bookdown] <br/>Lorem ipsum dolor sit amet, ... [bookdown](https://bookdown.org/) |
卡布尔表
```{r kable-table, echo=FALSE, out.width='90%', fig.align = "center", results='asis'}
library(knitr)
kable(df,
caption = "kable table: markdown styling works in HTML (*italics*, **bold**), LaTex styling in PDF (\\textbf{bold})",
caption.short = "md styling works in HTML (*italics*, **bold**), LaTex styling in PDF (\\textbf{bold})"
)
```
kable额外表
```{r kableExtra-table, echo=FALSE, out.width='90%', fig.align = "center", results='asis'}
library(kableExtra)
# http://haozhu233.github.io/kableExtra/awesome_table_in_pdf.pdf
kable(
df,
caption = "kableExtra table: markdown styling works in HTML (*italics*, **bold**), LaTex styling in PDF (\\textbf{bold})",
output_format, booktabs = T, # output_format = latex, html (specify above)
# align = "l",
valign = "top"
) %>%
kable_styling(full_width = F,
latex_options = c(#"striped",
"hold_position", # stop table floating
"repeat_header") # for long tables
) %>%
column_spec(1, bold = T, border_right = T, width = "30em") %>%
column_spec(2, width = "50em") %>%
column_spec(3, width = "5em") %>%
column_spec(4, width = "10em") %>%
column_spec(5, width = "10em") %>%
footnote(general = "Here is a general comments of the table. ",
number = c("Footnote 1; ", "Footnote 2; "),
alphabet = c("Footnote A; ", "Footnote B; "),
symbol = c("Footnote Symbol 1; ", "Footnote Symbol 2"),
general_title = "General: ", number_title = "Type I: ",
alphabet_title = "Type II: ", symbol_title = "Type III: ",
footnote_as_chunk = T, title_format = c("italic", "underline")
)
```
潘德表
```{r pander-table, echo=FALSE, out.width='90%', fig.align = "center", results='asis'}
library(pander)
# https://cran.r-project.org/web/packages/pander/vignettes/pandoc_table.html
pander(
df,
caption = "pander table: markdown styling works in HTML and PDF (*italics*, **bold**), LaTex styling in PDF (\\textbf{bold})",
# style = "multiline", # simple
split.table = Inf, # default = 80 characters; Inf = turn off table splitting
split.cells = c(15, 50, 5, 5, 5), # default = 30
# split.cells = c("25%", "50%", "5%", "10%", "10%"), # no difference
justify = "left"
)
```
表
```{r huxtable-table, echo=FALSE, out.width='90%', fig.align = "center", results='asis'}
library(dplyr)
library(huxtable)
# https://hughjonesd.github.io/huxtable/
hux <- as_hux(df) %>%
# huxtable::add_rownames(colname = '') %>%
huxtable::add_colnames() %>%
set_top_border(1, everywhere, 1) %>%
set_bottom_border(1, everywhere, 1) %>%
set_bottom_border(final(), everywhere, 1) %>%
set_bold(1, everywhere, TRUE) %>% # bold headlines
set_italic(-1, 1, TRUE) %>% # italics in first column (except the first row)
set_valign("top") %>%
set_width(1) %>%
set_col_width(c(0.10,0.45,0.05,0.10,0.10)) %>%
set_wrap(TRUE) %>%
set_position('left') %>% # fix table alignment (default is center)
add_footnote("Sample Footnote") %>%
set_font_size(4)
table_caption <- 'huxtable table: markdown styling works in HTML (*italics*, **bold**), LaTex styling in PDF (\\textbf{bold})'
# Print table conditional on output type
if (knitr::is_html_output()) {
caption(hux) <- paste0('(#tab:huxtable-table-explicit) ', table_caption)
print_html(hux) # output table html friendly (requires in chunk options "results='asis'")
}
if (knitr::is_latex_output()) {
caption(hux) <- paste0('(\\#tab:huxtable-table-explicit) ', table_caption)
hux # if using chunk option "results='asis'" simply output the table with "hux", i.e. do not use print_latex(hux)
}
```
参考表格
对于不同的表类型,工作方式不同
为 LoT 添加简短标题
最后为图表添加一个简短的标题并没有真正按预期工作 (ref:huxtable-table-caption) huxtable-table 标题 (ref:huxtable-table-scaption) huxtable-table 短标题
```{r huxtable-table, echo=FALSE, out.width='90%', fig.align = "center", fig.cap='(ref:huxtable-table-caption)', fig.scap='(ref:huxtable-table-scaption)', results='asis'}
...
```
推荐阅读
- python - 将请求查询字符串反转回定义的模式
- mysql - 在 Rails 上将 mysql 与 ruby 连接的问题
- vue.js - 如何在Vuejs中将多个组件添加到父组件
- apache - 如何在 apache 虚拟主机中配置存在数据库应用程序?
- c# - 如何在不使用 Linq 的情况下更新 xml 文件?
- php - 使用 PHP 通过 HTTP 访问 MDB 文件
- java - java8中的gradle REPL用于scala开发
- html - React Native 中的聊天气泡
- ruby-on-rails - 无法在我的 Rails 应用程序中安装雾宝石
- php - 在 Laravel phpword 中找不到类“ZipArchive”