code生成的表格
换行符的处理
kable函数不会对单元格中的\n
进行处理,因此需要提前替换为<br>
。
Code
library(tidyverse)
# 只替换某一列
journal <- readxl::read_excel('data/candidate-journals.xlsx') %>%
mutate(`Similar articles` = str_replace_all(`Similar articles`, "\n", "<br>"))
# 替换所有列
# 使用 mutate_all() 和 str_replace_all() 函数替换 \n
journal <- journal %>%
mutate_all(~ str_replace_all(., "\n", "<br>"))
markdown表格
列宽
用dash的数量表示宽度(貌似不起作用)
apple |
2.05 |
pear |
1.37 |
orange |
3.09 |
用tbl-colwidth属性
用在markdown表格: {tbl-colwidths="[75,25]"}
或者代码块中#| tbl-colwidths: [75,25]
apple |
2.05 |
pear |
1.37 |
orange |
3.09 |
caption
| Col1 | Col2 | Col3 |
|------|------|------|
| A | B | C |
| E | F | G |
| A | G | G |
: My Caption {#tbl-letters}
See @tbl-letters.
See 表 13.1.
DT包
举例
参考资料: https://rstudio.github.io/DT/
Code
library(DT)
iris2 = iris[c(1:10, 51:60, 101:110), ]
datatable(iris2, filter = 'top', options = list(
pageLength = 5, autoWidth = TRUE
))
渲染html标签
Code
library(DT)
# 假设你已经有了一个包含HTML标签的data.frame,比如df$PMID列包含的是带链接的HTML文本
df <- data.frame(
PMID = c('<a href="https://pubmed.ncbi.nlm.nih.gov/134343">PMID 1</a>',
'<a href="https://pubmed.ncbi.nlm.nih.gov/134345">PMID 2</a>'),
TITLE = c("Title 1", "Title 2"),
AUTHOR = c("Author 1", "Author 2"),
JOURNAL = c("Journal 1", "Journal 2")
)
# 在DT表格中显示并允许渲染HTML标签
datatable(df, escape = FALSE)
DataEditR包
Code
library(DataEditR)
# data_edit(mtcars)