2017-08-29 9 views
1

5 colsと1列目が文字で、他の4列が数値であるテーブルがあります。私はShiny Appで同じものを表示するためにDT Data Tableを使用しています。今度は、各行の4つの列のそれぞれと、最大値がの行セルの色コードとを比較する必要があります。同じことをする方法を探しています。このリンクの見た目もStylingCellsでしたが、ここではすべての数値が数値です。ここでDTデータテーブルの条件付き書式R Shiny

コード

entity <- c('entity1', 'entity2', 'entity3') 
value1 <- c(21000, 23400, 26800) 
value2 <- c(21234, 23445, 26834) 
value3 <- c(21123, 234789, 26811) 
value4 <- c(27000, 23400, 26811) 
entity.data <- data.frame(entity, value1, value2, value3, value4) 

header <- dashboardHeader() 
sidebar <- dashboardSidebar() 
body <- dashboardBody(DT::dataTableOutput("entity.dataTable")) 

shinyApp(
    ui = dashboardPage(header, sidebar, body), 
    server = function(input, output) { 
    output$entity.dataTable <- renderDataTable({ 
     DT::datatable(
     entity.data, 
     selection = "single", 
     filter = 'bottom', 

     extensions = c('Buttons', 'ColReorder', 'FixedHeader', 'Scroller'), 
     rownames = FALSE, 
     options = list(
      dom = 'Bfrtip', 
      searching = T, 
      pageLength = 25, 
      searchHighlight = TRUE, 
      colReorder = TRUE, 
      fixedHeader = TRUE, 
      filter = 'top', 
      buttons = c('copy', 'csv', 'excel', 'print'), 
      paging = TRUE, 
      deferRender = TRUE, 
      scroller = TRUE, 
      scrollX = TRUE, 
      scrollY = 550 

     ) 

    ) 

    }) 
    } 
) 

答えて

0

はあなたの問題に私のソリューションです:

library(shinydashboard) 
library(DT) 
library(magrittr) 

entity <- c('entity1', 'entity2', 'entity3') 
value1 <- c(21000, 23400, 26800) 
value2 <- c(21234, 23445, 26834) 
value3 <- c(21123, 234789, 26811) 
value4 <- c(27000, 23400, 26811) 
entity.data <- data.frame(entity, value1, value2, value3, value4) 

# Create a vector of max values 
max_val <- apply(entity.data[, -1], 1, max) 

header <- dashboardHeader() 
sidebar <- dashboardSidebar() 
body <- dashboardBody(DT::dataTableOutput("entity.dataTable")) 

shinyApp(
    ui = dashboardPage(header, sidebar, body), 
    server = function(input, output) { 
    output$entity.dataTable <- renderDataTable({ 
     DT::datatable(
     entity.data, 
     selection = "single", 
     filter = 'bottom', 
     extensions = c('Buttons', 'ColReorder', 'FixedHeader', 'Scroller'), 
     rownames = FALSE, 
     options = list(
      dom = 'Bfrtip', 
      searching = T, 
      pageLength = 25, 
      searchHighlight = TRUE, 
      colReorder = TRUE, 
      fixedHeader = TRUE, 
      filter = 'top', 
      buttons = c('copy', 'csv', 'excel', 'print'), 
      paging = TRUE, 
      deferRender = TRUE, 
      scroller = TRUE, 
      scrollX = TRUE, 
      scrollY = 550 
     ) 
    ) %>% # Style cells with max_val vector 
     formatStyle(
     columns = 2:5, 
     backgroundColor = styleEqual(levels = max_val, values = rep("yellow", length(max_val))) 
    ) 
    }) 
    } 
) 

だから、あなたが何をする必要があるか最大値のベクトルを作成することです。その後、ヘルパー関数styleEqual()formatStyle()の中の上記のコードのようにそれを使用します。

+0

ありがとうございます。あなたが提案した現在の解決策は、列全体で最大値を色分けし、行間で最大値を取得して色分けするために探しているものです。たとえば、サンプルデータ - 1行目の最大値:27000,2行目の最大値:234789 \t、3行目の最大値:26834。同じように達成する方法を教えてください。 – string

+0

はい、簡単です。 apply関数の 'MARGIN'引数を1に変更してください。この変更を反映するようにコードを変更しました。 – jsb

+0

ありがとうございました:)。それは完璧に働いていました。 max_val < - apply(entity.data [、-1]、2、max) - Max Col Valueとmax_valを取得するには< - apply(entity.data [、-1]、1、max) - 最大行値を取得します。これを追加して、他の人に役立ててください。 – string