2017-03-03 6 views
1

DTを前の表の値に従ってフォーマットしようとしています。 例えば、 が増加したか、減少したか、または同じままであるかどうかを表示したいとします。 私はkableでこれを行うことができましたが、 は、セルをクリックして、関連するすべてのデータを別のDTのその値に表示する次のステップを取得できませんでした。この場合異なるデータセットの値に応じて光沢のあるデータテーブル(DT)の色をフォーマットします

library(shiny) 
library(DT) 
library(dplyr) 
ui <- fluidPage(
    mainPanel(
     dataTableOutput("iris_head") 
) 
) 

server <- function(input, output) { 

    #df_data <- iris 

    df_data <- head(iris[-5]) 

    # Just a dataset describing if iris has changed over a month 
    # If reference data is of the same size as the original data (df_data). 
    # If reference data is negative I want the cell in the df_data to be green; 
    # If zero blue and if positive then green. 
    # I can make changes with ranges within the current range, can we get the color encoding from another table? 
    # set the seed 
    set.seed(42) 
    reference_df <- (sapply(df_data, function(x) jitter(x, amount = 2)) - df_data) %>% 
    round(. , digits = 0) 

    print(reference_df) 


    output$iris_head <- renderDataTable(datatable(df_data, selection = "single")%>% 
             formatStyle('Sepal.Width', 
                color = styleInterval(c(3.4, 3.8), c('green', 'blue', 'red')), 
                backgroundColor = styleInterval(3.4, c('gray', 'yellow'))) %>% 
             formatString('Sepal.Width', suffix = '<font color="red">&uArr; </font>')) 


} 

shinyApp(ui = ui, server = server) 

reference_dfである:

Sepal.Length Sepal.Width Petal.Length Petal.Width 
     2   1   2   0 
     2   -1   -1   0 
     -1   1   0   2 
     1   1   2   -1 
     1   0   2   2 
     0   1   -2   2 

要求出力をIは、テキスト及び可能であればreference_dfの値に応じて背景を着色する図に示されています。

テキストの色の部分について

enter image description here

答えて

3

、あなたはformatStyleでそれを行うことができますが、あなたは値に基づいて、1〜4列のスタイルを、その後、cbinddf_datareference_dfに必要datatableにそれを渡すと変わってしまいます列5〜8:

datatable(cbind(df_data,reference_df), selection = "single", 
               options=list(columnDefs = list(list(visible=FALSE, targets=c(5:8)))))%>% 
             formatStyle(1:4, valueColumns=5:8, 
                color = JS("value < 0 ? 'red' : value > 0 ? 'green' : 'blue'")) 

columnDefs部は最後の4列を非表示にします。

あなたあなたが矢を追加したい場合は、あなたがdatatableに渡す前に色や矢印を追加するdf_dataを変更できるような値に基づいてすることができませんformatString

for(col in 1:dim(df_data)[2]){ 
    df_data[col] <- mapply(function(i,j){ 
     ifelse(i > 0, paste0("<span style='color:red'>",j,"<font>&uArr; </font></span>"), 
      ifelse(i<0, paste0("<span style='color:green'>",j,"<font>&dArr; </font></span>"), 
        paste0("<span style='color:blue'>",j,"<font>&hArr; </font></span>"))) 
    },reference_df[col],df_data[col]) 
    } 

    output$iris_head <- renderDataTable(
    datatable(df_data, selection = "single",escape = F) 
    ) 

これは、の値をループdf_dataとなり、値はreference_dfに応じて変更されます。 HTMLエスケープを防ぐためにdatatable呼び出しでは、escape=Fが引数として必要です。

あなたは、背景など

+0

感謝を色付けしたい場合はspanタグでより多くのCSSスタイルを追加することができます!非常によく説明された答えです。誰かがこのような良い答えを受け取ったという事実を考慮に入れてコメントなしでこの質問を下落させたというのは少し奇妙です。 – discipulus

関連する問題