2017-12-20 33 views
0

Ich baue eine Shiny-App in R, um mehrere Datenrahmen anzuzeigen, insgesamt 6 auf 1 Shiny-Seite. Ich bin in der Lage, 1 Datentabelle in Shiny zu formatieren, aber ich kann nicht alle 6 Tabellen bedingt formatieren. Jede der sechs Tabellen 5 Variablen und ich mag die zweite Variable in jeder Datentabelle: Hintergrund leuchtet rot und fett, wenn> = 75, und leuchtet grün und fett, wenn < = 75:Bedingtes Format mehrere Tabellen in Shiny R

library(shiny) 
ui <- fluidPage(
    titlePanel("xxx"), 
    title = 'xx', 
    fluidRow(
column(6 
     , fluidRow(
      column(6, DT::dataTableOutput('1'), style = "font-size: 
       75%; width: 50%"), 
      column(6, DT::dataTableOutput('2'), style = "font-size: 
      75%; width: 50%") 
     ), 
     # hr(), 
     fluidRow(
      column(6, DT::dataTableOutput('3'), style = "font-size: 
       75%; width: 50%"), 
      column(6, DT::dataTableOutput('4'), style = "font-size: 
       75%; width: 50%") 
     ), 
     # hr(), 
     fluidRow(
      column(6, DT::dataTableOutput('5'), style = "font-size: 
       75%; width: 50%"), 
      column(6, DT::dataTableOutput('6'), style = "font-size: 
       75%; width: 50%") 
     ) 
    ) 
    server <- function(input, output) { 
      output$1 = DT::renderDataTable(1 
        , server = FALSE, selection = 'single' 
        , options = list(rowCallback = JS('function(nRow 
        , aData, iDisplayIndex, iDisplayIndexFull) { 
        // Bold and green cells for conditions 
        if (parseFloat(aData[2]) 
         >= 75 | parseFloat(aData[2]) <= -75) 
         $("td:eq(2)", nRow).css("font-weight", "bold"); 
         if (parseFloat(aData[2]) >= 75) 
         $("td:eq(2)", nRow).css("background-color" 
         , "#FF0000"); 
        else if(parseFloat(aData[2]) <= -75) 
         $("td:eq(2)", nRow).css("background-color", 
          "#00FF00"); 
        else 
         $("td:eq(2)", nRow).css("background-color" 
         , "#FFFFFF"); 
            }' 
            ) 
         , drawCallback = JS() 
           )) 

     output$2 = DT::renderDataTable(2, server = FALSE, 
       selection = 'single') 
     output$3 = DT::renderDataTable(3, server = FALSE, 
       selection = 'single') 
     output$4 = DT::renderDataTable(4, server = FALSE, 
       selection = 'single') 
     output$5 = DT::renderDataTable(5, server = FALSE, 
      selection = 'single') 
     output$6 = DT::renderDataTable(6, server = FALSE, 
       selection = 'single') 
+1

Bitte spezifizieren Sie im Detail, welche Art von Problemen auftreten. Ihre Aussage "Ich kann nicht alle 6 Tabellen bedingt formatieren" ist zu vage. – Omni

+0

Wenn ich diesen Code ausführen, funktioniert es für die Ausgabe $ 1 für die bedingte Formatierung Spalte 2, wenn ich versuche, den gleichen Code für die Ausgabe $ 2 hinzuzufügen, ändert sich nichts. Ausgabe $ 1 bleibt in Spalte 2 bedingt formatiert, Ausgabe $ 2 hat jedoch keine bedingte Formatierung. Ich muss alle 6 Datenrahmen in Spalte 2 bedingt formatieren. Danke für Ihre Hilfe. – mrklkr

+0

Als erstes sollten Sie die DataTable IDs nicht als Zahlen nennen, sondern eher: Table1, Table2 ... –

Antwort

1

Ich habe Ihr Code versucht, und es funktioniert gut für mich, einen Blick unter:

library(shiny) 
library(DT) 
ui <- fluidPage(
    titlePanel("xxx"), 
    title = 'xx', 
    fluidRow(
    column(12 
      , fluidRow(
      column(6, DT::dataTableOutput('Table1'), style = "font-size: 
        75%; width: 50%"), 
      column(6, DT::dataTableOutput('Table2'), style = "font-size: 
        75%; width: 50%") 
      ), 
      # hr(), 
      fluidRow(
      column(6, DT::dataTableOutput('Table3'), style = "font-size: 
        75%; width: 50%"), 
      column(6, DT::dataTableOutput('Table4'), style = "font-size: 
        75%; width: 50%") 
      ), 
      # hr(), 
      fluidRow(
      column(6, DT::dataTableOutput('Table5'), style = "font-size: 
        75%; width: 50%"), 
      column(6, DT::dataTableOutput('Table6'), style = "font-size: 
        75%; width: 50%") 
      ) 
      ))) 
    server <- function(input, output) { 
     output$Table1 = DT::renderDataTable(mtcars 
            , server = FALSE, selection = 'single' 
            , options = list(rowCallback = JS('function(nRow 
                     , aData, iDisplayIndex, iDisplayIndexFull) { 
                     // Bold and green cells for conditions 
                     if (parseFloat(aData[2]) 
                     >= 1 | parseFloat(aData[2]) <= 3) 
                     $("td:eq(2)", nRow).css("font-weight", "bold"); 
                     if (parseFloat(aData[2]) >= 1) 
                     $("td:eq(2)", nRow).css("background-color" 
                     , "#FF0000"); 
                     else if(parseFloat(aData[2]) <= 3) 
                     $("td:eq(2)", nRow).css("background-color", 
                     "#00FF00"); 
                     else 
                     $("td:eq(2)", nRow).css("background-color" 
                     , "#FFFFFF"); 
                     }' 
            ))) 

     output$Table2 = DT::renderDataTable(iris, server = FALSE, 
            selection = 'single', options = list(rowCallback = JS('function(nRow 
                     , aData, iDisplayIndex, iDisplayIndexFull) { 
                          // Bold and green cells for conditions 
                          if (parseFloat(aData[2]) 
                          >= 1 | parseFloat(aData[2]) <= 3) 
                          $("td:eq(2)", nRow).css("font-weight", "bold"); 
                          if (parseFloat(aData[2]) >= 1) 
                          $("td:eq(2)", nRow).css("background-color" 
                          , "#FF0000"); 
                          else if(parseFloat(aData[2]) <= 3) 
                          $("td:eq(2)", nRow).css("background-color", 
                          "#00FF00"); 
                          else 
                          $("td:eq(2)", nRow).css("background-color" 
                          , "#FFFFFF"); 
    }' 
            ))) 
     output$Table3 = DT::renderDataTable(iris, server = FALSE, 
            selection = 'single', options = list(rowCallback = JS('function(nRow 
                     , aData, iDisplayIndex, iDisplayIndexFull) { 
                          // Bold and green cells for conditions 
                          if (parseFloat(aData[2]) 
                          >= 1 | parseFloat(aData[2]) <= 3) 
                          $("td:eq(2)", nRow).css("font-weight", "bold"); 
                          if (parseFloat(aData[2]) >= 1) 
                          $("td:eq(2)", nRow).css("background-color" 
                          , "#FF0000"); 
                          else if(parseFloat(aData[2]) <= 3) 
                          $("td:eq(2)", nRow).css("background-color", 
                          "#00FF00"); 
                          else 
                          $("td:eq(2)", nRow).css("background-color" 
                          , "#FFFFFF"); 
    }' 
            ))) 
     output$Table4 = DT::renderDataTable(iris, server = FALSE, 
            selection = 'single', options = list(rowCallback = JS('function(nRow 
                     , aData, iDisplayIndex, iDisplayIndexFull) { 
                     // Bold and green cells for conditions 
                     if (parseFloat(aData[2]) 
                     >= 1 | parseFloat(aData[2]) <= 3) 
                     $("td:eq(2)", nRow).css("font-weight", "bold"); 
                     if (parseFloat(aData[2]) >= 1) 
                     $("td:eq(2)", nRow).css("background-color" 
                     , "#FF0000"); 
                     else if(parseFloat(aData[2]) <= 3) 
                     $("td:eq(2)", nRow).css("background-color", 
                     "#00FF00"); 
                     else 
                     $("td:eq(2)", nRow).css("background-color" 
                     , "#FFFFFF"); 
                     }' 
            ))) 
     output$Table5 = DT::renderDataTable(iris, server = FALSE, 
            selection = 'single', options = list(rowCallback = JS('function(nRow 
                     , aData, iDisplayIndex, iDisplayIndexFull) { 
                          // Bold and green cells for conditions 
                          if (parseFloat(aData[2]) 
                          >= 1 | parseFloat(aData[2]) <= 3) 
                          $("td:eq(2)", nRow).css("font-weight", "bold"); 
                          if (parseFloat(aData[2]) >= 1) 
                          $("td:eq(2)", nRow).css("background-color" 
                          , "#FF0000"); 
                          else if(parseFloat(aData[2]) <= 3) 
                          $("td:eq(2)", nRow).css("background-color", 
                          "#00FF00"); 
                          else 
                          $("td:eq(2)", nRow).css("background-color" 
                          , "#FFFFFF"); 
    }' 
            ))) 
     output$Table6 = DT::renderDataTable(iris, server = FALSE, 
            selection = 'single', options = list(rowCallback = JS('function(nRow 
                     , aData, iDisplayIndex, iDisplayIndexFull) { 
                          // Bold and green cells for conditions 
                          if (parseFloat(aData[2]) 
                          >= 1 | parseFloat(aData[2]) <= 3) 
                          $("td:eq(2)", nRow).css("font-weight", "bold"); 
                          if (parseFloat(aData[2]) >= 1) 
                          $("td:eq(2)", nRow).css("background-color" 
                          , "#FF0000"); 
                          else if(parseFloat(aData[2]) <= 3) 
                          $("td:eq(2)", nRow).css("background-color", 
                          "#00FF00"); 
                          else 
                          $("td:eq(2)", nRow).css("background-color" 
                          , "#FFFFFF"); 
    }' 
            )))} 
shinyApp(ui, server) 

Jede Tabelle bekam Spalte hervorgehoben, wie Sie wollten.

-> Ich habe mich verändert nur die Id von DataTable, lesen DT Bibliothek und Ihre Klammern korrigiert.

In Ihrem Fall würde ich überlegen, eine ganze Spalte ohne JS mit Hilfe von formatStyle() zu formatieren.

Bitte nächstes Mal ein reproducible example.

[UPDATE] Lösung mit formatStyle()

Unten sehen Sie die Lösung formatStyle() Option sehen, Kommentare zum Code sind direkt neben den spezifischen Linien.

library(shiny) 
library(DT) 
ui <- fluidPage(
    titlePanel("xxx"), 
    title = 'xx', 
    fluidRow(
    column(12 
      , fluidRow(
      column(6, DT::dataTableOutput('Table1'), style = "font-size: 
        75%; width: 50%"), 
      column(6, DT::dataTableOutput('Table2'), style = "font-size: 
        75%; width: 50%") 
      ), 
      # hr(), 
      fluidRow(
      column(6, DT::dataTableOutput('Table3'), style = "font-size: 
        75%; width: 50%"), 
      column(6, DT::dataTableOutput('Table4'), style = "font-size: 
        75%; width: 50%") 
      ), 
      # hr(), 
      fluidRow(
      column(6, DT::dataTableOutput('Table5'), style = "font-size: 
        75%; width: 50%"), 
      column(6, DT::dataTableOutput('Table6'), style = "font-size: 
        75%; width: 50%") 
      ) 
      ))) 
server <- function(input, output) { 
    output$Table1 = DT::renderDataTable(
    datatable(mtcars) %>% 
     formatStyle('cyl', fontWeight = styleInterval(4, c('normal', 'bold')), # Font bold if cyl > 4 
        backgroundColor = styleInterval(4, c('green', 'red'))) # Red background if cyl > 4 
) 

} 
shinyApp(ui, server) 
+0

Vielen Dank. Ich würde lieber formatStyle() verwenden, ich konnte es einfach nicht zum Laufen bringen. Irgendwelche Vorschläge wären so hilfreich. Vielen Dank! – mrklkr

+0

Mein Code funktioniert jetzt, allerdings würde ich lieber formatsyle verwenden. Was wäre, wenn ich ein Plot erstellen wollte, wenn ich also in einer der 6 Datentabellen "klickte", würde sich ein Plot aus den Daten ergeben. – mrklkr

+0

Sehen Sie sich mein Antwort-Update an –