[英]How to filter (search) formatted columns in R DT (DataTable)
When applying formatting to a column in DT::datatable()
, the DataTables automatic column filter is not working.将格式应用于
DT::datatable()
中的列时,DataTables 自动列过滤器不起作用。 For example:例如:
library(DT)
a <- letters[1:10]
b <- 1:10
df <- data.frame(a, b)
datatable(df, filter="bottom") %>% formatCurrency(columns = "b", currency = "$")
The filter for column b
fails. b
列的过滤器失败。
I think this is a common enough problem that there has to be a solution.我认为这是一个足够普遍的问题,必须有一个解决方案。 I have been searching, but without success so far.
我一直在寻找,但到目前为止没有成功。 Would be grateful if someone points me to where this has been solved.
如果有人指出我解决了这个问题,将不胜感激。
I am aware that by using the JS
callback function on the DT website ( https://rstudio.github.io/DT/options.html see 4.5 Row rendering and run it with filter="top"
), one can filter with formatted columns. I am aware that by using the
JS
callback function on the DT website ( https://rstudio.github.io/DT/options.html see 4.5 Row rendering and run it with filter="top"
), one can filter with formatted列。 But I am trying to implement a general solution for an R
package, so I was hoping for an R
solution.但我正在尝试为
R
package 实施通用解决方案,所以我希望有一个R
解决方案。
I have an approach that works in some cases.我有一种在某些情况下有效的方法。 The approach is to copy the raw numbers column to a new column that is hidden on rendering.
方法是将原始数字列复制到渲染时隐藏的新列。 But 'link' the formatted numbers (
column
) to the raw numbers column ( values_column
) in formatStyle
.但是将格式化数字(
column
)“链接”到formatStyle
中的原始数字列( values_column
)。 Then, luckily, and not exactly sure why, searching on the formatted column works.然后,幸运的是,并不完全确定为什么,在格式化的列上进行搜索是有效的。 But only in the case of suffixes (like
%
), but not for prefixes ( $
) or big number formatting with commas for thousands.但仅适用于后缀(如
%
),但不适用于前缀( $
)或带有逗号的大数字格式。
Code for each of these cases is below.每种情况的代码如下。
I don't mind using this workaround, but don't know enough about the inner workings of DT to make prefixes and other formats work.我不介意使用此解决方法,但对 DT 的内部工作原理了解不足,无法使前缀和其他格式正常工作。
library(DT)
a <- letters[1:10]
b <- 1:10
df <- data.frame(a, b)
xx <-
datatable(
data = df,
filter = "bottom",
options = list(columnDefs = list(list(
targets = which(stringr::str_detect(colnames(df), "hide")), visible = FALSE
)))
)
xx$x$data$hide_b <- xx$x$data$b
xx$x$data$b <- paste(xx$x$data$b, "%")
attr(xx$x, "colnames") <- colnames(xx$x$data)
xx %>%
formatStyle(
columns = "b",
valueColumns = "hide_b"
)
library(DT)
a <- letters[1:10]
b <- 1:10
df <- data.frame(a, b)
xx <-
datatable(
data = df,
filter = "bottom",
options = list(columnDefs = list(list(
targets = which(stringr::str_detect(colnames(df), "hide")), visible = FALSE
)))
)
xx$x$data$hide_b <- xx$x$data$b
xx$x$data$b <- paste("$", xx$x$data$b)
attr(xx$x, "colnames") <- colnames(xx$x$data)
xx %>%
formatStyle(
columns = "b",
valueColumns = "hide_b"
)
library(DT)
a <- letters[1:10]
b <- 1:10*10^6
df <- data.frame(a, b)
xx <-
datatable(
data = df,
filter = "bottom",
options = list(columnDefs = list(list(
targets = which(stringr::str_detect(colnames(df), "hide")), visible = FALSE
)))
)
xx$x$data$hide_b <- xx$x$data$b
xx$x$data$b <- format(xx$x$data$b,digits = 1, scientific = FALSE, big.mark = ",")
attr(xx$x, "colnames") <- colnames(xx$x$data)
xx %>%
formatStyle(
columns = "b",
valueColumns = "hide_b"
)
Fixed in latest dev version of DT.已在 DT 的最新开发版本中修复。 Formatting no longer breaks filtering.
格式化不再破坏过滤。
Using DR::renderDT
with server=TRUE
(the default) inside a Shiny app solves this problem using the regular DT::format*
functions.在 Shiny 应用程序中使用带有
server=TRUE
(默认值)的DR::renderDT
可以使用常规DT::format*
函数解决此问题。 Outside Shiny
context, the examples still don't work.在
Shiny
上下文之外,这些示例仍然不起作用。 But that is irrelevant in my case, as the tables will go in a Shiny app.但这在我的情况下无关紧要,因为表格将在 Shiny 应用程序中使用 go。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.