Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show Inf in DT::datatable()

I would like to explicitly show the Inf value inside a datatable instead of a blank

iris[1, 1] <- Inf
DT::datatable(iris[1:2, ])

enter image description here

I don't want to turn the column info character to be able to sort the column (If I do this, sorting will be alphabetically)

Any ideas?


Edit:

I thinks it's possible to adapt this kind of code :

datatable(iris[c(1:20), ], options = list(columnDefs = list(list(
  targets = 5,
  render = JS(
    "function(data, type, row, meta) {",
    "return type === 'display' && data.length > 2 ?",
    "'<span title=\"' + data + '\">' + data.substr(0, 2) + '...</span>' : data;",
    "}")
))))

with @MLavoie solution, it doesn't distinct NA and Inf

df = iris
df[1,1]<-Inf
df[2,1]<-NA
DT::datatable(df)
library(DT)
DT::datatable(df[,], options = list(columnDefs = list(list(
  targets = 1,
  render = JS(
    "function(data, type, row, meta) {",
    "return data === null ? 'Inf' : data;",
    "}")
))))
like image 419
Vincent Guyader Avatar asked Mar 08 '23 00:03

Vincent Guyader


2 Answers

The solution is :

options(htmlwidgets.TOJSON_ARGS = list(na = 'string'))

iris[1,1] <- NA
iris[2,1] <- Inf
DT::datatable(head(iris))

thanks to @yihui-xie and @Jeroen at : https://github.com/rstudio/DT/issues/496#issuecomment-363867449

like image 52
Vincent Guyader Avatar answered Mar 18 '23 14:03

Vincent Guyader


You can do this:

df = iris
df[1,1]<-Inf
datatable(df[1:2,], options = list(columnDefs = list(list(
    targets = 1,
    render = JS(
        "function(data, type, row, meta) {",
        "return data === null ? 'Inf' : data;",
        "}")
))))

and you could also do it manually:

DT::datatable(df[1:2,], editable = TRUE)
like image 22
MLavoie Avatar answered Mar 18 '23 13:03

MLavoie