kable / data.table冲突?

时间:2014-08-20 08:11:20

标签: r data.table knitr

我的包kable中的knitr命令与data.table结合使用时出现小问题。

以前,我可以使用data.table在markdown中打印kable,但现在出现错误消息。现在的解决方案是转换为data.frame,但这很麻烦。

library(data.table)
library(knitr)

kable(mtcars)  # works

mtcars = data.table(mtcars, keep.rownames = T)
kable(mtcars)

Error in `[<-.data.table`(`*tmp*`, , isn, value = c("FALSE", "TRUE", "TRUE",  : 
 j must be vector of column name or positions
In addition: Warning message:
In `[<-.data.table`(`*tmp*`, , j, value = 1) :
 Coerced 'double' RHS to 'character' to match the column's type; may have truncated     precision. Either change the target column to 'double' first (by creating a new 'double' vector length 32 (nrows of entire table) and assign that; i.e. 'replace' column), or coerce RHS to 'character' (e.g. 1L, NA_[real|integer]_, as.*, etc) to make your intent clear and for speed. Or, set the column type correctly up front when you create the table and stick to it, please.

SessionInfo:

R version 3.1.0 (2014-04-10)
Platform: x86_64-pc-linux-gnu (64-bit)

locale:
[1] LC_CTYPE=en_US.UTF-8          LC_NUMERIC=C                      LC_TIME=en_US.UTF-8           LC_COLLATE=en_US.UTF-8        LC_MONETARY=en_US.UTF-8      
[6] LC_MESSAGES=en_US.UTF-8       LC_PAPER=en_US.UTF-8           LC_NAME=en_US.UTF-8           LC_ADDRESS=en_US.UTF-8        LC_TELEPHONE=en_US.UTF-8     
[11] LC_MEASUREMENT=en_US.UTF-8    LC_IDENTIFICATION=en_US.UTF-8

attached base packages:
[1] parallel  stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] rCharts_0.4.2              plyr_1.8.1                 googleVis_0.5.3               shiny_0.10.0               scagnostics_0.2-4          rJava_0.9-6               
 [7] ggvis_0.3.0.9001           knitr_1.6                  FSelector_0.19              randomForest_4.6-7         RWeka_0.4-21               rpart.plot_1.4-4          
[13] rpart_4.1-8                stringdist_0.7.3           PerformanceAnalytics_1.1.0  xts_0.9-7                  scales_0.2.3               gdata_2.13.2              
[19] stringr_0.6.2              freqparcoord_1.0.0         mvtnorm_0.9-9997            FNN_1.1                    GGally_0.4.6               treemap_2.1-1             
[25] tabplot_1.1                ffbase_0.11.3              ff_2.2-13                  bit_1.1-12                 ggplot2_1.0.0              reshape2_1.4              
[31] lubridate_1.3.3            zoo_1.7-10                 data.table_1.9.3  

1 个答案:

答案 0 :(得分:0)

这是他的解决方案,您需要在应用kable之前确保data.frame中的所有向量格式 knitr::kable()