[英]R Markdown document with html/docx output, using LaTeX package bbm?
[英]Feeding R data to html in R markdown, where the output is a Word document
我想在 R Markdown 中自動創建一個表,其中表是在html
中創建的,但數據來自R
。 誰能幫我解決這個問題?
R 數據:
dat <- structure(list(rn = c("W", "M"), `[ 0, 25)` = c(5L,
0L), `[ 25, 50)` = c(0L, 0L), `[ 25, 100)` = c(38L,
3L), `[ 50, 100)` = c(0L, 0L), `[ 100, 250)` = c(43L,
5L), `[ 100, 500)` = c(0L, 0L)), row.names = c(NA, -2L), class = c("data.table",
"data.frame"))
rn [ 0, 25) [ 25, 50) [ 25, 100) [ 50, 100) [ 100, 250) [ 100, 500)
1: W 5 0 38 0 43 0
2: M 0 0 3 0 5 0
Html 代碼:
<html>
<table border="1">
<tr>
<td width="150">Lower threshold</td>
<td width="50">0</td>
<td width="50">25</td>
<td width="50">50</td>
<td width="50">100</td>
<td width="50">250</td>
<tr>
<td width="150">Upper threshold</td>
<td width="50">25</td>
<td width="50">50</td>
<td width="50">100</td>
<td width="50">250</td>
<td width="50">500</td>
<tr>
<td width="150">Category W</td>
<td width="100" colspan="1">5</td>
<td width="100" colspan="2">38</td>
<td width="100" colspan="2">5</td>
</tr>
<tr>
<td width="150">Category M</td>
<td width="100" colspan="1">0</td>
<td width="100" colspan="2">3</td>
<td width="100" colspan="2">5</td>
</tr>
<html>
<head>
<style>
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #dddddd;
}
</style>
</head>
從頭開始創建 HTML 表的一種方法大量使用htmltools
和purrr
可能看起來像這樣。 為了完成這項工作,您必須進行一些數據整理,以將您的數據和創建數據所需的信息以整潔的數據格式放置:
---
title: "HTML Table"
date: '2022-04-19'
output: html_document
---
```{r echo=FALSE}
dat <- structure(list(
row = structure(c(
1L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L
), .Label = c(
"Lower threshold",
"Upper threshold", "Category W", "Category M"
), class = "factor"),
value = c(
0, 25, 50, 100, 250, 25, 50, 100, 250, 500, 5,
38, 5, 0, 3, 5
), width = c(
50, 50, 50, 50, 50, 50, 50, 50,
50, 50, 100, 100, 100, 100, 100, 100
), colspan = c(
NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, 1, 2, 2, 1, 2, 2
)
), row.names = c(
NA,
-16L
), class = "data.frame")
```
```{r}
head(dat)
```
```{r echo=FALSE}
make_html_table_row <- function(row, label) {
htmltools::tagList(
htmltools::tags$td(label, width = "150"),
purrr::pmap(row, function(row, value, width, colspan) {
htmltools::tags$td(value, width = width, colspan = if (!is.na(colspan)) colspan)
})
) |>
htmltools::tags$tr()
}
make_html_table <- function(x) {
x_split <- split(x, x$row)
purrr::imap(x_split, make_html_table_row) |>
htmltools::tagList() |>
htmltools::tags$table(border = "1") |>
htmltools::tags$html()
}
```
```{r results='asis'}
make_html_table(dat)
```
數據
用於創建數據的代碼。
dat <- list(
data.frame(
row = "Lower threshold",
value = c(0, 25, 50, 100, 250),
width = 50,
colspan = NA
),
data.frame(
row = "Upper threshold",
value = c(25, 50, 100, 250, 500),
width = 50,
colspan = NA
),
data.frame(
row = "Category W",
value = c(5, 38, 5),
width = 100,
colspan = c(1, 2, 2)
),
data.frame(
row = "Category M",
value = c(0, 3, 5),
width = 100,
colspan = c(1, 2, 2)
)
) |>
dplyr::bind_rows()
dat$row <- factor(dat$row, levels = c("Lower threshold", "Upper threshold", "Category W", "Category M"))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.