如何在RMarkdown的循环中居中flextable?

huangapple go评论60阅读模式
英文:

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: &quot;Test&quot;
output: html_document
---

```{r, echo=FALSE, results=&#39;asis&#39;, fig.align=&#39;center&#39;}
library(flextable)
library(tidyverse)

# Loop through data

overview_table &lt;- function(df) {
  finalTable &lt;- df %&gt;%
  flextable() %&gt;%
  set_header_labels(rowname = &quot;&quot;) %&gt;%
  theme_zebra() %&gt;%
  bg(bg=&quot;#186183&quot;, part=&quot;body&quot;) %&gt;%
  bg(bg=&quot;white&quot;, part=&quot;header&quot;) %&gt;%
  color(part = &quot;body&quot;, color = &quot;white&quot;) %&gt;%
  height(height = 0.4, part = &quot;header&quot;) %&gt;%
  padding(padding.top = 0,
          padding.bottom = 0) %&gt;%
  fontsize(size = 20, part = &quot;body&quot;) %&gt;%
  fontsize(size = 18, part = &quot;header&quot;) %&gt;%
  align(align = &quot;center&quot;, part = &quot;all&quot;)
  return(finalTable)
}

for (i in 1:3) {
  
  cat(&#39;&lt;center&gt; &lt;h1 style=&quot;font-size:80px;&quot;&gt;&#39;, i, &#39;&lt;/h1&gt; &lt;/center&gt; &lt;br&gt;&#39;)

  a &lt;- overview_table(head(mtcars)[,1:4])
    
  flextable_to_rmd(a)
  cat(&quot;&lt;br&gt;&quot;)
}
```

答案1

得分: 1

你需要更改你的cat()语句,以便&lt;center&gt;的开头和结束(&lt;/center&gt;) HTML 标签包围表格:


for (i in 1:3) {
  
  cat('&lt;center&gt; &lt;h1 style=&quot;font-size:80px;&quot;&gt;', i, '&lt;/h1&gt;')

  a &lt;- overview_table(head(mtcars)[,1:4])
    
  flextable_to_rmd(a)

  cat("&lt;br&gt;&lt;/center&gt;")
}

如何在RMarkdown的循环中居中flextable?

英文:

You have to change your cat() statement so that the &lt;center&gt; opening and closing (&lt;/center&gt;) HTML tag wraps around the table:


for (i in 1:3) {
  
  cat(&#39;&lt;center&gt; &lt;h1 style=&quot;font-size:80px;&quot;&gt;&#39;, i, &#39;&lt;/h1&gt;&#39;)

  a &lt;- overview_table(head(mtcars)[,1:4])
    
  flextable_to_rmd(a)

  cat(&quot;&lt;br&gt;&lt;/center&gt;&quot;)
}

如何在RMarkdown的循环中居中flextable?

huangapple
  • 本文由 发表于 2023年4月20日 01:58:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76057542.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定