R Shiny DataTables 用字符串替换数字并排序为小于数值
R Shiny DataTables replace numeric with string and sort as being less than numeric values
在 Shiny 应用程序中,我在 datatable
中有一列数字,出于安全原因,某些值已被抑制,我们想用特定字符串替换它们,在这里我将其称为 "my_string"
.在此列上排序时,这些被抑制的值需要排序,就好像它们 小于 所有实际数字一样。在此列中,所有值均为正值,但已编码为 -1
.
的抑制值除外
我尝试将 -1
重新编码为 "my_string"
(将列强制为 character
)并使用 natural
plug-in 对字符编码数字进行正确排序,但 "my_string"
正在排序,就好像它大于所有数值。
另一种可能的处理方法是使用 JavaScript
回调将 -1
替换为字符串,但我不知道如何编写该脚本并将其正确添加到datatable
.
这是我使用natural
插件的尝试。如果它像我想要的那样工作,带有 "my_string" 的行将位于列表的底部而不是顶部。
# Example data, representing how the data comes to me
my_mtcars <- mtcars[1:6, 1:4]
my_mtcars[1, 4] <- -1
# Here I am recoding the -1
my_mtcars[my_mtcars == -1] <- 'my_string'
# This is our demo app.R
library(shiny)
library(DT)
ui <- fluidPage(
dataTableOutput('example')
)
server <- function(input, output) {
output$example <- renderDataTable(
my_mtcars,
server = FALSE,
plugins = 'natural',
options = list(columnDefs = list(list(type = 'natural', targets = '_all')))
)
}
shinyApp(ui = ui, server = server)
使用自定义格式化程序/列呈现函数可能更容易。
请参阅 DT 文档中的列呈现:https://rstudio.github.io/DT/options.html
以及 DataTables 文档:https://datatables.net/reference/option/columns.render
my_mtcars <- mtcars[1:6, 1:4]
my_mtcars[1, 4] <- -1
formatSuppressedValues <- JS("
function(data, type) {
if (type !== 'display') return data;
if (data !== -1) return data;
return 'my_string';
}
")
library(shiny)
library(DT)
ui <- fluidPage(
DT::dataTableOutput('example')
)
server <- function(input, output) {
output$example <- DT::renderDataTable(
my_mtcars,
server = FALSE,
options = list(
columnDefs = list(list(
targets = '_all',
render = formatSuppressedValues
))
)
)
}
shinyApp(ui = ui, server = server)
在 Shiny 应用程序中,我在 datatable
中有一列数字,出于安全原因,某些值已被抑制,我们想用特定字符串替换它们,在这里我将其称为 "my_string"
.在此列上排序时,这些被抑制的值需要排序,就好像它们 小于 所有实际数字一样。在此列中,所有值均为正值,但已编码为 -1
.
我尝试将 -1
重新编码为 "my_string"
(将列强制为 character
)并使用 natural
plug-in 对字符编码数字进行正确排序,但 "my_string"
正在排序,就好像它大于所有数值。
另一种可能的处理方法是使用 JavaScript
回调将 -1
替换为字符串,但我不知道如何编写该脚本并将其正确添加到datatable
.
这是我使用natural
插件的尝试。如果它像我想要的那样工作,带有 "my_string" 的行将位于列表的底部而不是顶部。
# Example data, representing how the data comes to me
my_mtcars <- mtcars[1:6, 1:4]
my_mtcars[1, 4] <- -1
# Here I am recoding the -1
my_mtcars[my_mtcars == -1] <- 'my_string'
# This is our demo app.R
library(shiny)
library(DT)
ui <- fluidPage(
dataTableOutput('example')
)
server <- function(input, output) {
output$example <- renderDataTable(
my_mtcars,
server = FALSE,
plugins = 'natural',
options = list(columnDefs = list(list(type = 'natural', targets = '_all')))
)
}
shinyApp(ui = ui, server = server)
使用自定义格式化程序/列呈现函数可能更容易。
请参阅 DT 文档中的列呈现:https://rstudio.github.io/DT/options.html
以及 DataTables 文档:https://datatables.net/reference/option/columns.render
my_mtcars <- mtcars[1:6, 1:4]
my_mtcars[1, 4] <- -1
formatSuppressedValues <- JS("
function(data, type) {
if (type !== 'display') return data;
if (data !== -1) return data;
return 'my_string';
}
")
library(shiny)
library(DT)
ui <- fluidPage(
DT::dataTableOutput('example')
)
server <- function(input, output) {
output$example <- DT::renderDataTable(
my_mtcars,
server = FALSE,
options = list(
columnDefs = list(list(
targets = '_all',
render = formatSuppressedValues
))
)
)
}
shinyApp(ui = ui, server = server)