knit pdf 中的 kable() 显得过大

kable() in knit pdf appears oversized

我正在使用 kable() 函数在 Rmd knit pdf 文档中输出 table。然而,我遇到了一个问题。当我编织 pdf 时,它输出的 table 宽度过大,最终被裁剪。请在下面查看 Rmd knit pdf 中 运行 的模拟代码。如您所见,缺少列:

```{r, echo=FALSE}
require(knitr)
col1 <- c("City", "Abidjan","Accra","Amman")
col2 <- c("Optimized HDH","217","281","26398")
col3 <- c("Standard HDH","0","0","37056")
col4 <- c("Heating Demand [W/(°C x Capita)]", "0.0","0.0","28.3")
col5 <- c("Heating Energy [kWh/(capita x yr)]","0","0","469.68")
col6 <- c("Threshold Temperature","23.3","24","17.6")
col7 <- c("Optimized CDH","31781","25264","32198")
col8 <- c("Standard CDH", "59398","58895","22059")
col9 <- c("Cooling Demand [W/(°C x capita)]", "1.67", "2.83", "45.72")
col10 <- c("Cooling Energy [kWh/(capita x yr)]", "53", "143.56", "1256.62")
tble <- data.frame(col1, col2, col3, col4, col5, col6, col7, col8, col9, col10)

colnames(tble) <- tble[1,]
tble <- tble[2:4,]
````

```{r, results='asis'}
# kable(tble)
```


|City    |Optimized HDH |Standard HDH |Heating Demand [W/(°C x Capita)] |Heating Energy [kWh/(capita x yr)] |Threshold Temperature |Optimized CDH |Standard CDH |Cooling Demand [W/(°C x capita)] |Cooling Energy [kWh/(capita x yr)] |
|:-------|:-------------|:------------|:--------------------------------|:----------------------------------|:---------------------|:-------------|:------------|:--------------------------------|:----------------------------------|
|Abidjan |217           |0            |0.0                              |0                                  |23.3                  |31781         |59398        |1.67                             |53                                 |
|Accra   |281           |0            |0.0                              |0                                  |24                    |25264         |58895        |2.83                             |143.56                             |
|Amman   |26398         |37056        |28.3                             |469.68                             |17.6                  |32198         |22059        |45.72                            |1256.62                            |

您知道如何避免这种情况吗?也许 kable() 中的输入要求重新调整 table 的大小以适合一页?我厌倦了谷歌搜索和帮助,但没有找到任何有用的信息。

另一个问题:有没有办法让列名超过 2 行,而不是 1 行?我的问题是 colnames 太长了。

非常感谢!

有很多选项可以拆分单元格中的文本,或者如果 pander 太宽则拆分 table,例如:

> pander(tble, split.cells = 20, split.table = Inf)

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
&nbsp;   City    Optimized HDH   Standard HDH    Heating Demand       Heating Energy       Threshold    Optimized CDH   Standard CDH    Cooling Demand       Cooling Energy    
                                                [W/(°C x Capita)]   [kWh/(capita x yr)]   Temperature                                  [W/(°C x capita)]   [kWh/(capita x yr)] 
------- ------- --------------- -------------- ------------------- --------------------- ------------- --------------- -------------- ------------------- ---------------------
 **2**  Abidjan       217             0                0.0                   0               23.3           31781          59398             1.67                  53          

 **3**   Accra        281             0                0.0                   0                24            25264          58895             2.83                143.56        

 **4**   Amman       26398          37056             28.3                469.68             17.6           32198          22059             45.72               1256.62       
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

请注意,style=rmarkdown 不支持此功能,因为竖线-tables does not support 换行符。使用 pander 的默认多行样式 pandoc