2017-10-11 23 views
0

少一個閃亮的應用程序,我有一個datatable一列數字,其中出於安全原因,一些值已被抑制,我們要替換這些帶有特定字符串,這裏我會打電話給"my_string"。當這列排序,這些抑制值需要排序,儘管它們是小於所有實際數字。在該列中的所有的值都是正的,除了已被編碼爲-1的抑制值。[R閃亮的數據表代替數字用繩子和排序爲比數值

我已經試過重新編碼-1"my_string"(其要挾的列character),並使用natural plug-in的字符編碼NUMERICS正確排序,但"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) 

enter image description here

回答

1

這可能是一個自定義格式/列更容易渲染功能。

見第二欄渲染的DT文檔:https://rstudio.github.io/DT/options.html

而數據表文檔: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) 
+0

完美,謝謝!查看通用數據表文檔如何映射到R'DT'上下文也很有幫助。對於未來的參觀者的注意事項 - 我已經列入'服務器= FALSE'在我的例子只是因爲'natural'插件需要它,它實際上不是必需的這一解決方案。 –