在R中创建一个堆叠的2 x 2 kable表格,使用不同维度的数据框。

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

Create a stacked 2 x 2 kable table in R with different dimension dataframes

问题

以下是您提供的代码的翻译部分:

  1. 我正在尝试在R中创建一组可以以HTMLLaTeXPDF形式存在的表格,因此我正在使用kable。我想创建一个22列的表格,每个单元格包含来自不同数据框的信息。我希望在每个单元格的上方有不同的信息作为标题。对我来说棘手的部分是这些表格具有不同的宽度,所以我希望单元格的宽度或高度与我指定的一样,并且这将增加整个表格的宽度和高度。
  2. 基本上,我希望它看起来像这样:
  3. ![enter image description here](https://i.stack.imgur.com/IQEi7.png)
  4. 到目前为止,我有这段代码:
  5. ```R
  6. library(kableExtra)
  7. df1 <- head(mtcars)
  8. df2 <- head(iris)
  9. df3 <- head(ChickWeight)
  10. df4 <- head(airquality)
  11. table1 <- kable(df1, format = "html") %>%
  12. kable_styling()
  13. table2 <- kable(df2, format = "html") %>%
  14. kable_styling()
  15. table3 <- kable(df3, format = "html") %>%
  16. kable_styling()
  17. table4 <- kable(df4, format = "html") %>%
  18. kable_styling()
  19. combined_table=kable(list(df1,df2, df3,df4), format="html") %>%
  20. kable_styling() %>%
  21. column_spec(column=1:4, border_left = T, border_right = T)

但当它们具有不同的列长度时,我无法rbind数据帧。那么如何将它们放入不同的面板?

我包括了我的代码,希望它能将对象分成不同的面板,但它没有这样做。

  1. 请注意,这是您提供的代码的翻译部分,不包括代码的解决方案或答案。如果您需要关于如何解决问题的帮助,请随时提出具体的问题。
  2. <details>
  3. <summary>英文:</summary>
  4. I am trying to create in R a set of tables that can be in either html or latex or pdf form so I&#39;m using kable. I want to create a 2 row and 2 column table with each cell containing information from a different dataframe. I want headers above each of the cells with different information. The tricky part for me is the tables have different so I want the cells to be as wide or long as a I specify and this would just add to the width and height of the overall table.
  5. Basically i want it to look like this
  6. ![enter image description here](https://i.stack.imgur.com/IQEi7.png)
  7. I have this code so far

library(kableExtra)
df1 <- head(mtcars)
df2 <- head(iris)
df3 <- head(ChickWeight)
df4 <- head(airquality)

table1 <- kable(df1, format = "html") %>%
kable_styling()
table2 <- kable(df2, format = "html") %>%
kable_styling()
table3 <- kable(df3, format = "html") %>%
kable_styling()
table4 <- kable(df4, format = "html") %>%
kable_styling()

combined_table=kable(list(df1,df2, df3,df4), format="html") %>%
kable_styling() %>%
column_spec(column=1:4, border_left = T, border_right = T)

  1. but I cannot rbind the dataframes when they have different column lengths. So how do I get them into different panels.
  2. I included my code and expected it to panel the objects but it does not.
  3. </details>
  4. # 答案1
  5. **得分**: 0
  6. 以下是使用 `quarto` 中的网格布局的方法:

format: html
execute:
echo: false

  1. ```{r}
  2. library(kableExtra)
  3. df1 <- head(mtcars)
  4. df2 <- head(iris)
  5. df3 <- head(ChickWeight)
  6. df4 <- head(airquality)
  7. table1 <- kable(df1, format = "html") %>%
  8. kable_styling()
  9. table2 <- kable(df2, format = "html") %>%
  10. kable_styling()
  11. table3 <- kable(df3, format = "html") %>%
  12. kable_styling()
  13. table4 <- kable(df4, format = "html") %>%
  14. kable_styling()
  1. ::: {.grid}
  2. ::: {.g-col-6}
  3. ## 组合表格
  4. ```{r}
  5. table1

:::

::: {.g-col-6}

{}

  1. table2

:::

:::

::: {.grid}

::: {.g-col-6}

  1. table3

:::

::: {.g-col-6}

  1. table4

:::

:::

  1. [![在这里输入图片描述][1]][1]
  2. [1]: https://i.stack.imgur.com/yu3u1.png
  3. <details>
  4. <summary>英文:</summary>
  5. Here is a way using the grid layout in `quarto`:
  6. ---
  7. format: html
  8. execute:
  9. echo: false
  10. ---
  11. ```{r}
  12. library(kableExtra)
  13. df1 &lt;- head(mtcars)
  14. df2 &lt;- head(iris)
  15. df3 &lt;- head(ChickWeight)
  16. df4 &lt;- head(airquality)
  17. table1 &lt;- kable(df1, format = &quot;html&quot;) %&gt;%
  18. kable_styling()
  19. table2 &lt;- kable(df2, format = &quot;html&quot;) %&gt;%
  20. kable_styling()
  21. table3 &lt;- kable(df3, format = &quot;html&quot;) %&gt;%
  22. kable_styling()
  23. table4 &lt;- kable(df4, format = &quot;html&quot;) %&gt;%
  24. kable_styling()
  25. ```
  26. ::: {.grid}
  27. ::: {.g-col-6}
  28. ## Combined Table
  29. ```{r}
  30. table1
  31. ```
  32. :::
  33. ::: {.g-col-6}
  34. ## {}
  35. ```{r}
  36. table2
  37. ```
  38. :::
  39. :::
  40. ::: {.grid}
  41. ::: {.g-col-6}
  42. ```{r}
  43. table3
  44. ```
  45. :::
  46. ::: {.g-col-6}
  47. ```{r}
  48. table4
  49. ```
  50. :::
  51. :::
  52. [![enter image description here][1]][1]
  53. [1]: https://i.stack.imgur.com/yu3u1.png
  54. </details>

huangapple
  • 本文由 发表于 2023年6月6日 05:26:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76410106.html
匿名

发表评论

匿名网友

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

确定