2017-10-11 4 views
0

반짝이는 앱에서는 보안상의 이유로 일부 값이 표시되지 않고 특정 문자열로 대체하려는 datatable에 숫자 열이 있습니다. 여기서 나는 "my_string"라고 부를 것이다. 이 열을 정렬 할 때 이러한 억제 된 값은 인 것처럼 실제 숫자 인 미만으로 정렬해야합니다. 이 열에서 -1으로 코드화 된 억제 된 값을 제외하고는 모든 값이 양수입니다.R Shiny DataTables는 숫자를 문자열로 바꾸고 숫자 값보다 작은 숫자로 정렬합니다.

내가 레코딩 시도한 -1 올바르게 문자 코드 수치를 정렬 할 natural plug-in를 사용합니다 (character에 열을 강제 변환)하지만 숫자 모든 값보다 큰 것처럼 "my_string" 정렬되고 "my_string"로 .

문자열로 -1을 대체 할 JavaScript 콜백을 사용할 수 있습니다이 처리하는 또 다른 가능한 방법,하지만 난 그 스크립트를 작성하고 올바르게은 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

그리고 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) 
+0

완벽, 감사합니다! 또한 일반적인 datatables 문서가 R'DT' 문맥에 어떻게 매핑되는지 보는 것이 매우 유용합니다. 미래의 방문객을위한 노트 - 필자는'natural' 플러그인이 필요하기 때문에'server = FALSE'를 포함 시켰습니다. 실제로이 솔루션에는 필요하지 않습니다. –