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

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

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

问题

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

我正在尝试在R中创建一组可以以HTML、LaTeX或PDF形式存在的表格,因此我正在使用kable。我想创建一个22列的表格,每个单元格包含来自不同数据框的信息。我希望在每个单元格的上方有不同的信息作为标题。对我来说棘手的部分是这些表格具有不同的宽度,所以我希望单元格的宽度或高度与我指定的一样,并且这将增加整个表格的宽度和高度。

基本上,我希望它看起来像这样:

![enter image description here](https://i.stack.imgur.com/IQEi7.png)

到目前为止,我有这段代码:

```R
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)

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

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


请注意,这是您提供的代码的翻译部分,不包括代码的解决方案或答案。如果您需要关于如何解决问题的帮助,请随时提出具体的问题。

<details>
<summary>英文:</summary>

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. 

Basically i want it to look like this 

![enter image description here](https://i.stack.imgur.com/IQEi7.png)

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)

but I cannot rbind the dataframes when they have different column lengths. So how do I get them into different panels.  


I included my code and expected it to panel the objects but it does not. 

</details>


# 答案1
**得分**: 0

以下是使用 `quarto` 中的网格布局的方法:


format: html
execute:
echo: false


```{r}
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()
::: {.grid}

::: {.g-col-6}

## 组合表格
```{r}
table1

:::

::: {.g-col-6}

{}

table2

:::

:::

::: {.grid}

::: {.g-col-6}

table3

:::

::: {.g-col-6}

table4

:::

:::


[![在这里输入图片描述][1]][1]
  
  [1]: https://i.stack.imgur.com/yu3u1.png

<details>
<summary>英文:</summary>

Here is a way using the grid layout in `quarto`:

    ---
    format: html
    execute: 
      echo: false
    ---
    
    
    ```{r}
    library(kableExtra)
    df1 &lt;- head(mtcars)
    df2 &lt;- head(iris)
    df3 &lt;- head(ChickWeight)
    df4 &lt;- head(airquality)
    
    table1 &lt;- kable(df1, format = &quot;html&quot;) %&gt;%
      kable_styling()
    table2 &lt;- kable(df2, format = &quot;html&quot;) %&gt;%
      kable_styling()
    table3 &lt;- kable(df3, format = &quot;html&quot;) %&gt;%
      kable_styling()
    table4 &lt;- kable(df4, format = &quot;html&quot;) %&gt;%
      kable_styling()
    ```
    
    ::: {.grid}
    
    ::: {.g-col-6}
    
    ## Combined Table
    ```{r}
    table1
    ```
    
    :::
      
    ::: {.g-col-6}
    ## {}
    ```{r}
    table2
    ```
    
    :::
      
    :::  
    
    ::: {.grid}
    
    ::: {.g-col-6}
    
    ```{r}
    table3
    ```
    
    :::
      
    ::: {.g-col-6}
    
    ```{r}
    table4
    ```
    
    :::
      
    :::  

[![enter image description here][1]][1]


  [1]: https://i.stack.imgur.com/yu3u1.png

</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:

确定