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

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

How do I center flextable in RMarkdown for loop?

问题

以下是您要翻译的内容:

我有以下的Rmd文档,其中在for循环内生成了flextables,然后打印出这些表格。我希望将这些flextables居中在HTML输出中。问题是无论如何,我都得到了所有表格都是左对齐的结果。我看到有关使用flextable_html_dependency()的内容,但没有足够的文档让我知道如何正确使用它。

  1. library(flextable)
  2. library(tidyverse)
  3. # Loop through data
  4. overview_table <- function(df) {
  5. finalTable <- df %>%
  6. flextable() %>%
  7. set_header_labels(rowname = "") %>%
  8. theme_zebra() %>%
  9. bg(bg="#186183", part="body") %>%
  10. bg(bg="white", part="header") %>%
  11. color(part = "body", color = "white") %>%
  12. height(height = 0.4, part = "header") %>%
  13. padding(padding.top = 0,
  14. padding.bottom = 0) %>%
  15. fontsize(size = 20, part = "body") %>%
  16. fontsize(size = 18, part = "header") %>%
  17. align(align = "center", part = "all")
  18. return(finalTable)
  19. }
  20. for (i in 1:3) {
  21. cat('<center> <h1 style="font-size:80px;">', i, '</h1> </center> <br>')
  22. a <- overview_table(head(mtcars)[,1:4])
  23. flextable_to_rmd(a)
  24. cat("<br>")
  25. }
英文:

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.

  1. ---
  2. title: &quot;Test&quot;
  3. output: html_document
  4. ---
  5. ```{r, echo=FALSE, results=&#39;asis&#39;, fig.align=&#39;center&#39;}
  6. library(flextable)
  7. library(tidyverse)
  8. # Loop through data
  9. overview_table &lt;- function(df) {
  10. finalTable &lt;- df %&gt;%
  11. flextable() %&gt;%
  12. set_header_labels(rowname = &quot;&quot;) %&gt;%
  13. theme_zebra() %&gt;%
  14. bg(bg=&quot;#186183&quot;, part=&quot;body&quot;) %&gt;%
  15. bg(bg=&quot;white&quot;, part=&quot;header&quot;) %&gt;%
  16. color(part = &quot;body&quot;, color = &quot;white&quot;) %&gt;%
  17. height(height = 0.4, part = &quot;header&quot;) %&gt;%
  18. padding(padding.top = 0,
  19. padding.bottom = 0) %&gt;%
  20. fontsize(size = 20, part = &quot;body&quot;) %&gt;%
  21. fontsize(size = 18, part = &quot;header&quot;) %&gt;%
  22. align(align = &quot;center&quot;, part = &quot;all&quot;)
  23. return(finalTable)
  24. }
  25. for (i in 1:3) {
  26. 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;)
  27. a &lt;- overview_table(head(mtcars)[,1:4])
  28. flextable_to_rmd(a)
  29. cat(&quot;&lt;br&gt;&quot;)
  30. }
  31. ```

答案1

得分: 1

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

  1. for (i in 1:3) {
  2. cat('&lt;center&gt; &lt;h1 style=&quot;font-size:80px;&quot;&gt;', i, '&lt;/h1&gt;')
  3. a &lt;- overview_table(head(mtcars)[,1:4])
  4. flextable_to_rmd(a)
  5. cat("&lt;br&gt;&lt;/center&gt;")
  6. }

如何在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:

  1. for (i in 1:3) {
  2. cat(&#39;&lt;center&gt; &lt;h1 style=&quot;font-size:80px;&quot;&gt;&#39;, i, &#39;&lt;/h1&gt;&#39;)
  3. a &lt;- overview_table(head(mtcars)[,1:4])
  4. flextable_to_rmd(a)
  5. cat(&quot;&lt;br&gt;&lt;/center&gt;&quot;)
  6. }

如何在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:

确定