我正在使用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()中的输入要求重新调整表的大小以适应1页?我累了谷歌搜索和帮助,但没有找到任何有用的信息。
另一个问题:有没有办法让列名超过2行,而不是1?我的问题是colnames太长了。
非常感谢!
答案 0 :(得分:4)
有很多选项可以在单元格中拆分文本,或者如果pander
太宽则拆分表格,例如:
> pander(tble, split.cells = 20, split.table = Inf)
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
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
。