2017-04-12 44 views
1

我一直在使用RStudio DT如何與DT ::科學數據表的列數

library(shiny) 
library(DT) 
shinyApp(
    ui = fluidPage(
    DT::dataTableOutput('example') 
), 
    server = function(input, output) { 
    output$example <- DT::renderDataTable({ 
     table = cbind(LETTERS[1:5],c("9.95e-04","9.93e-06","9.93e-02","9.49e-03","9.10e-02")) 
     table 
    }, options = list(
     columnDefs = list(list(type = "scientific", targets = 1)) 
    )) 
    } 
) 

下面的代碼,但排序它,因爲我打算不科學的列進行排序。什麼是正確的做法?

enter image description here

正確的遞減順序應該是:

V1   V2 
C  0.0993 
E  0.091 
D 0.00949 
A 0.000995 
B 0.00000993 
+0

這裏http://stackoverflow.com/questions/35657271/ordering-factors-in-data-table-using-dt-package –

+1

看一看你V2是一個字符,而不是數字。目前它按字母順序排序。一旦將它強制轉換爲數字,排序應該起作用。 –

+0

@RomanLuštrik但我想保留科學記數法。如果我做'as.numeric',科學記數法就不復存在了。 – neversaint

回答

1

這是我在評論部分提出的第一個解決方案的實施。

xy <- read.table(text = "V1   V2 
C  0.0993 
E  0.091 
D 0.00949 
A 0.000995 
B 0.00000993", header = TRUE, colClasses = c("character", "character")) 
xy$V3 <- as.numeric(xy$V2) 

xy[order(xy$V3, decreasing = TRUE), c("V1", "V2")] 

    V1   V2 
1 C  0.0993 
2 E  0.091 
3 D 0.00949 
4 A 0.000995 
5 B 0.00000993 

編輯

你可以用你的例子試試這個。注意我使用了一個data.frame。矩陣不適合此解決方案。

output$example <- DT::renderDataTable({ 
    xy <- data.frame(letter = LETTERS[1:5], value = c("9.95e-04","9.93e-06","9.93e-02","9.49e-03","9.10e-02")) 
    xy$num_val <- as.numeric(as.character(xy$value)) 
    xy[order(xy$num_val, decreasing = TRUE), c("letter", "value")] 
} 
+0

謝謝。但是,你可以舉例* Shiny *,* DT * – neversaint

+0

@neversaint我加了一個小塊可能會有所幫助。 –