This short example shows how having Inf and -Inf values can break up the dTable()
function in rCharts
. The fix is simple: replace those values by some other large/small number respecitvely.
library(data.table) library(rCharts) library(knitr) render_html()
The following table does not because Inf and -Inf are not recognized by DataTables as shown in the browser console.
data <- data.frame(x = c(-Inf, Inf, 3, 4), y = letters[1:4]) t1 <- dTable(data.table(data), sPaginationType = "full_numbers", iDisplayLength = 100, sScrollX = "100%") t1$print("error", cdn = TRUE)
The following function takes a data.frame and for the columns of interest it replaces the Inf and -Inf values by 1e100 and -1e100 respectively. The colsubset argument can be useful if your data.frame has columns that are not numeric.
replaceInf <- function(df, colsubset = seq_len(ncol(df))) { for (i in colsubset) { inf.idx <- !is.finite(df[, i]) if (any(inf.idx)) { inf.sign <- sign(df[inf.idx, i]) df[inf.idx, i] <- inf.sign * 1e+100 } } return(df) } data2 <- replaceInf(data)
Now the table is displayed =)
t2 <- dTable(data.table(data2), sPaginationType = "full_numbers", iDisplayLength = 100, sScrollX = "100%") t2$print("fixed", cdn = TRUE)
sessionInfo()
## R version 3.0.1 (2013-05-16) ## Platform: x86_64-apple-darwin10.8.0 (64-bit) ## ## locale: ## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8 ## ## attached base packages: ## [1] stats graphics grDevices utils datasets methods base ## ## other attached packages: ## [1] rCharts_0.3.52 data.table_1.8.8 knitr_1.4.1 ## ## loaded via a namespace (and not attached): ## [1] digest_0.6.3 evaluate_0.4.7 formatR_0.9 grid_3.0.1 ## [5] highr_0.2.1 lattice_0.20-15 plyr_1.8 RJSONIO_1.0-3 ## [9] stringr_0.6.2 tools_3.0.1 whisker_0.3-2 yaml_2.1.7