Show Inf in DT::datatable() - r

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, ])
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;",
"}")
))))

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

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)

Related

R Shiny DT change cell colour based on string pattern

I have a DT data table which contain strings like below:
I want to change the colour of the cell, so that the cells with strings that contain "OK" will turn green, and cells with strings that contain "ERROR" will turn red. I know that I can change the cell colour by using formatStyle and styleEqual but it does not seem to work when matching string patterns.
library(DT)
render <- c(
"function(data, type, row){",
" if(type === 'display'){",
" var color = /OK$/.test(data) ? 'green' : (/ERROR$/.test(data) ? 'red' : 'black');",
" data = '<span style=\"color: ' + color + ';\">' + data + '</span>';",
" }",
" return data;",
"}"
)
dat <- data.frame(
a = c("1 - OK", "1 - ERROR", "2 - ERROR"),
b = c("3 - ERROR", "2 - OK", "3 - OK")
)
datatable(
dat,
options = list(
columnDefs = list(
list(targets = c(1, 2), render = JS(render))
)
)
)

Sorting with NA using datatable function in DT package

I am trying to create an html table using the datatable function in the DT package so that when I sort the data in R markdown, missing rows are sorted after the highest number.
For example, in the following table, when I sort by "age" in the markdown file, I would like the row with NA to be listed last so that the order is 14,15,21,NA.
dat <- data.frame("Age" = c(21,15,NA,14),
"Name" = c("John","Dora", "Max", "Sam"),
"Gender" = c("M","F","M",NA))
DT::datatable(dat, filter = c("top"))
I have tried using "na.last = TRUE" and this works when the datatable initially prints, however when clicking the column to sort, NA is still before 14.
Any help would be much appreciated!
With the render columnwise option, you can set the value of the missing values during the sorting:
library(DT)
dat <- data.frame("Age" = c(21,15,NA,14),
"Name" = c("John","Dora", "Max", "Sam"),
"Gender" = c("M","F","M",NA))
render <- JS(
"function(data, type, row) {",
" if(type === 'sort' && data === null) {",
" return 999999;",
" }",
" return data;",
"}"
)
datatable(
dat,
filter = "top",
options = list(
columnDefs = list(
list(targets = 1, render = render)
)
)
)

Passing column name to target option in a shiny datatable

I'm stuck in trying to pass the column name (instead the column number) in the target option of columnDefs. The table is dynamic so I definitely need the option to target the column name. Below is a reproducible example. The example is not dynamic, however.
datatable(iris[c(1:20, 51:60, 101:120), ], options = list(columnDefs = list(list(
targets = 5,
render = JS(
"function(data, type, row, meta) {",
"return type === 'display' && data.length > 6 ?",
"'<span title=\"' + data + '\">' + data.substr(0, 6) + '...</span>' : data;",
"}")
))), callback = JS('table.page(3).draw(false);'))
Tried with targets = 'Species' , targets = iris$Species but they didn't work.
if you are interested in setting multiple columns to different widths, consider the following (take care that R is one based, while javascript is one based):
col_a <- which(names(dat) %in% c("column_name1", "column_name2"))
col_b <- which(names(dat) %in% c("column_name3", "column_name4"))
col_c <- which(names(dat) %in% c("column_name5"))
columnDefs = list(list(width = '30px', targets = as.list(col_a - 1)),
list(width = '80px', targets = as.list(col_b - 1)),
list(width = '200px', targets = as.list(col_c - 1)))

Rshiny Table collapse text overflow

How do we pass CSS arguments text-overflow: ellipsis or other arguments to renderDataTable in R shiny ? I have uneven text description in columns, by Autowidth the rows and columns are expanded based on the contents in respective cell.
I would like to be able to input "ellipsis", for user to be able to expand the cell to read text. Below is my server.r code. I tried to use the eg, explained in http://rstudio.github.io/DT/options.html. However, could not get what I am looking for. Appreciate if any inputs , suggestions are provided.
Thank you
Solved.
The problem was with improper way I passed the arguments. The correct way is below.
output$PM_output <- DT::renderDataTable(
expr = DT::datatable(data.frame.eg),
class = 'cell-border stripe compact hover',
escape = F, selection = 'multiple',
options = list(
autoWidth = T,
LengthMenu = c(5, 30, 50),
columnDefs = list(list(
targets = 6,
render = JS(
"function(data, type, row, meta) {",
"return type === 'display' && data.length > 100 ?", "'<span title=\"' + data + '\">' +
data.substr(0, 100) + '...</span>' : data;", "}"))),
pageLength = 1, server = T)))

Change number format in renderDataTable

How do I define number format for datatable in Shiny? I would like to display 2 decimal digits only for some columns, but do not understand where it should be defined in my application. In server.R or ui.R? In server.R, this what I have in renderDataTable:
output$woeTable <- renderDataTable({
input$tryTree
input$threshold
# The following returns data frame with numeric columns
get(input$dfDescr)[['variables']][[input$columns]][['woe']]
},
options=list(
paging = FALSE,
searching = FALSE)
)
How do I format 2nd and 3rd column to display only two decimal digits?
just use round command:
output$woeTable <- renderDataTable({
input$tryTree
input$threshold
# The following returns data frame with numeric columns
A = get(input$dfDescr)[['variables']][[input$columns]][['woe']]
A[,2] = round(x = A[,2],digits = 2)
A[,3] = round(x = A[,3],digits = 2)
A
},
options=list(
paging = FALSE,
searching = FALSE)
)
you can also use fnRowCallback option in renderDataTable function if you insist to keep data with more digit and just change representation in output:
output$woeTable <- renderDataTable({
input$tryTree
input$threshold
# The following returns data frame with numeric columns
get(input$dfDescr)[['variables']][[input$columns]][['woe']]
},
options=list(
paging = FALSE,
searching = FALSE,
fnRowCallback = I("function( nRow, aData, iDisplayIndex, iDisplayIndexFull ) {ind = 2; $('td:eq('+ind+')', nRow).html( (aData[ind]).toFixed(2) );}"))
)
update for DT 1.1:
you should change
fnRowCallback = I("function( nRow, aData, iDisplayIndex, iDisplayIndexFull ) {ind = 2; $('td:eq('+ind+')', nRow).html( (aData[ind]).toFixed(2) );}"))
to
rowCallback = I("function( nRow, aData) {ind = 2; $('td:eq('+ind+')', nRow).html( parseFloat(aData[ind]).toFixed(2) );}"))

Resources