Within a Shiny application, I encounter a scenario where a column of numbers in a datatable
has certain values suppressed for security reasons. To maintain confidentiality, these suppressed values are replaced with a specific string referred to as "my_string"
. When this column is sorted, it is essential that the suppressed values are treated as if they are lower than all actual numbers. These suppressed values are denoted by -1
, while all other values in the column are positive integers.
My initial approach involved recoding the -1
as "my_string"
, which converted the entire column into a character
type. By utilizing the natural
plug-in to sort the character-coded numerics appropriately, I aimed to achieve the desired sorting order. However, during execution, "my_string"
ended up being sorted above all numeric values, contrary to my expectations.
An alternative method could involve implementing a JavaScript
callback to substitute the -1
with the designated string. Nevertheless, lack of expertise in scripting and integrating it effectively within the datatable
poses a challenge.
The code snippet provided attempts to incorporate the natural
plug-in. Ideally, "my_string" should appear at the bottom rather than the top of the list, reflecting the correct sorting order.
# Sample dataset illustrating the data structure
my_mtcars <- mtcars[1:6, 1:4]
my_mtcars[1, 4] <- -1
# Recoding the suppressed value (-1)
my_mtcars[my_mtcars == -1] <- 'my_string'
# Implementation within our demonstration app.R file
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)