gpt4 book ai didi

r - knit pdf 中的 kable() 显得过大

转载 作者:行者123 更新时间:2023-12-01 12:35:29 24 4
gpt4 key购买 nike

我正在使用 kable() 函数在 Rmd knit pdf 文档中输出表格。然而,我遇到了一个问题。当我编织 pdf 时,它输出的表格宽度过大,最终被裁剪。请参阅下面在 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() 中的输入要求重新调整表格大小以适合一页?我厌倦了谷歌搜索和帮助,但没有找到任何有用的信息。

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

非常感谢!

最佳答案

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

> 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 不支持此功能,因为管道表 does not support换行符。将 pander 的默认多行样式与 pandoc 一起使用。

关于r - knit pdf 中的 kable() 显得过大,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30277511/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com