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)))
Related
I created a data table in Shiny with the first column editable. I can enter texts but they will disappear after I switch input values and then come back. For example, I entered "testing 001" for the Category of "Male Premium BladeRazor System" and everything seemed fine (see the picture). However, after I switch my Category to another value and then come back to "Male Premium BladeRazor System", the entered text of "testing 001" would be gone. My code is pretty long, and I cut part of it for your reference. Any help will be highly appreciated. Thanks.
Example of text edits
output$tabofrandom <- DT::renderDataTable(
if (is.null(names(tab7data$dat))) {
datatable(tab7data$dat)
}
else {
datatable(isolate(tab7data$dat),
editable = list(target = 'cell', disable = list(columns = c(1,2, 3, 4,5,6,7,8,9))),
rownames = FALSE,
selection = list(mode = "single", target = "row", selected = previousSelection),
extensions = c('Buttons'),
options = list(searching=FALSE,
scrollX=T,
scrollY=277,
#processing=FALSE,
autoWidth = TRUE,
displayStart = previousPage,
dom = 'Blfrtip',
# pageLength = 5,
lengthChange = FALSE,
lengthMenu = list(c(5,10, -1), c("5","10", "All")),
buttons = list(c('pageLength','excel'),list(extend = 'colvis')),
# buttons = c('excel','colvis'),
columnDefs=list(list(targets=c(2,4,5,8),
render = JS("function(data){return data.replace(/;/g,
'<br>');}")),
...
))
) %>%
formatStyle(1:10, 'text-align' = 'left') %>%
formatStyle(1:10, 'vertical-align'='top')
} )
...
observeEvent(input[["tabofrandom_cell_edit"]], {
info <- input[["tabofrandom_cell_edit"]]
info$col=info$col+1
previousSelection <<- input$tabofrandom_rows_selected
previousPage <<- input$tabofrandom_rows_current[1] - 1
tab7data$dat<-isolate(DT::editData(tab7data$dat,info))
})
...
You have to set the server option of renderDataTable to FALSE or to use a proxy:
proxy <- DT::dataTableProxy("tabofrandom")
observeEvent(input[["tabofrandom_cell_edit"]], {
info <- input[["tabofrandom_cell_edit"]]
......
tab7data$dat <- DT::editData(tab7data$dat, info, proxy)
})
You don't have to do info$col + 1 for editData.
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)))
I am using in Shiny Buttons extension to download figures in a Excel-File.
DTa <- data.table(
dataSum()[,1],
format(round((10^-6)*dataSum()[,-1],2),nsmall = 2,decimal.mark=",",big.mark=".")
)
DTa<- DT::datatable( DTa, extensions=c("Buttons"),options = list(paging = FALSE,
searching = FALSE,
dom = 'Bfrtip',
#buttons = c('copy','excel')
buttons = list(
list(
extend = 'excel',
text = "Save ",
title = 'KRB'
), list(
extend = 'copy', title = 'krb'
)
)
),
caption= paste("Stichtag:",
as.character(sub("([0-9]{2})([0-9]{2})([0-9]{4})KRB.csv", "\\1.\\2.\\3",input$date))))
In the first part above, I transform the figures in the German format, i.e., I set , as a decimal separator and . as a thousands separator. In the second part, I call the extensions Buttons of DT.
In Shiny, the figures look as follows:
Problem: After pressing the Save Button in Shiny, all figures with just , and without . have the wrong format.
For example, after saving 34,21 becomes 3.421! But 67.809,97 is correct!
How can I keep the format of the figures during the export or save process?
I don't know if it helps:
When I change into the debug mode and execute the second part DTa<- DT::datatable( DTa, extensions ... I see the following:
As one can see the figures in data are characters!
Is it possible to write a JavaScript function in my server.R to use the language.decimal option? There is an example here, however I cannot use it exactly.
Try this
DTa<- DT::datatable(DTa, extensions=c("Buttons"), options = list(paging = FALSE,
searching = FALSE,
dom = 'Bfrtip',
#buttons = c('copy','excel')
buttons = list(
list(
extend = 'excel',
text = "Save ",
title = 'KRB'
), list(
extend = 'copy', title = 'KRB'
)
)
),
caption= paste("Stichtag:",
as.character(sub("([0-9]{2})([0-9]{2})([0-9]{4})KRB.csv", "\\1.\\2.\\3",input$date)))
) %>% formatCurrency(-1,' ', digits = 2 , interval = 3, mark = ".", dec.mark = ",")
Can you please help me with DT::datatable column formatting? I have for example this table:
DT::datatable(iris,
class = 'row-border stripe hover compact',
rownames = F,
autoHideNavigation = T,
options = list(pageLength = nrow(summary.month),
searching = F,
paging = F,
info = F))
I need to set:
1st column: bold, aligned left
3rd coumn: bold, aligned right
I found, that I should use columns.ClassName, but how to set the class styles in R?
The html output of datatable will be used in R markdown document then.
It has been a while since this question was initially asked, but I just had this same problem. Here is a simpler solution that doesn't require editing the source data or calling JS, but instead uses functions within the DT package itself.
DT::datatable(iris,
class = 'row-border stripe hover compact',
rownames = F,
autoHideNavigation = T, escape =FALSE) %>%
formatStyle(columns = c("Sepal.Length"), fontWeight = 'bold', `text-align` = 'left') %>%
formatStyle(columns = c("Petal.Length"), fontWeight = 'bold', `text-align` = 'right')
So far the only way I can get it to work is by manually setting the HTML tags first, and then using escape = FALSE
Here we wrap Sepal.Length in the bold HTML tag:
iris$SepalLength2 <- paste0("<b>", iris$Sepal.Length, "</b>")>
Then use escape = FALSE so that the HTML tags are parsed.
datatable(iris,
class = 'row-border stripe hover compact',
rownames = F,
autoHideNavigation = T, escape =FALSE)
Edit:
For align left/right, you can wrap in a <p align ="left"></p>
So: iris$SepalLength2 <- paste0('<p align ="right"><b>', iris$Sepal.Length, '</b></p>')
Note that I am neither an HTML guru, nor an expert on this particular library, but this seems like one way to get your desired result.
You don't need to modify the contents of your data. Instead, you can use the rowCallback option:
library(DT)
rowCallback <- c(
"function(row, data, index){",
" $(this.api().cell(index, 0).node())",
" .css('text-align', 'left')",
" .css('font-weight', 'bold');",
" $(this.api().cell(index, 2).node())",
" .css('text-align', 'right')",
" .css('font-weight', 'bold');",
"}"
)
DT::datatable(iris,
class = 'row-border stripe hover compact',
rownames = FALSE,
autoHideNavigation = TRUE,
options = list(pageLength = 5,
searching = FALSE,
paging = TRUE,
info = FALSE,
rowCallback = JS(rowCallback))
)
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)