英文:
How do I center flextable in RMarkdown for loop?
问题
以下是您要翻译的内容:
我有以下的Rmd文档,其中在for循环内生成了flextables,然后打印出这些表格。我希望将这些flextables居中在HTML输出中。问题是无论如何,我都得到了所有表格都是左对齐的结果。我看到有关使用flextable_html_dependency()
的内容,但没有足够的文档让我知道如何正确使用它。
library(flextable)
library(tidyverse)
# Loop through data
overview_table <- function(df) {
finalTable <- df %>%
flextable() %>%
set_header_labels(rowname = "") %>%
theme_zebra() %>%
bg(bg="#186183", part="body") %>%
bg(bg="white", part="header") %>%
color(part = "body", color = "white") %>%
height(height = 0.4, part = "header") %>%
padding(padding.top = 0,
padding.bottom = 0) %>%
fontsize(size = 20, part = "body") %>%
fontsize(size = 18, part = "header") %>%
align(align = "center", part = "all")
return(finalTable)
}
for (i in 1:3) {
cat('<center> <h1 style="font-size:80px;">', i, '</h1> </center> <br>')
a <- overview_table(head(mtcars)[,1:4])
flextable_to_rmd(a)
cat("<br>")
}
英文:
I have the following Rmd doc that generates flextables within a for-loop and then prints out the tables. Im hoping to center the flextables within the html output. The issue is I keep getting all the tables as left aligned no matter what. I saw something about having to use flextable_html_dependency()
but didnt see enough documentation for me to know how to properly use it.
---
title: "Test"
output: html_document
---
```{r, echo=FALSE, results='asis', fig.align='center'}
library(flextable)
library(tidyverse)
# Loop through data
overview_table <- function(df) {
finalTable <- df %>%
flextable() %>%
set_header_labels(rowname = "") %>%
theme_zebra() %>%
bg(bg="#186183", part="body") %>%
bg(bg="white", part="header") %>%
color(part = "body", color = "white") %>%
height(height = 0.4, part = "header") %>%
padding(padding.top = 0,
padding.bottom = 0) %>%
fontsize(size = 20, part = "body") %>%
fontsize(size = 18, part = "header") %>%
align(align = "center", part = "all")
return(finalTable)
}
for (i in 1:3) {
cat('<center> <h1 style="font-size:80px;">', i, '</h1> </center> <br>')
a <- overview_table(head(mtcars)[,1:4])
flextable_to_rmd(a)
cat("<br>")
}
```
答案1
得分: 1
你需要更改你的cat()
语句,以便<center>
的开头和结束(</center>
) HTML 标签包围表格:
for (i in 1:3) {
cat('<center> <h1 style="font-size:80px;">', i, '</h1>')
a <- overview_table(head(mtcars)[,1:4])
flextable_to_rmd(a)
cat("<br></center>")
}
英文:
You have to change your cat()
statement so that the <center>
opening and closing (</center>
) HTML tag wraps around the table:
for (i in 1:3) {
cat('<center> <h1 style="font-size:80px;">', i, '</h1>')
a <- overview_table(head(mtcars)[,1:4])
flextable_to_rmd(a)
cat("<br></center>")
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论