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)
Bitte spezifizieren Sie im Detail, welche Art von Problemen auftreten. Ihre Aussage "Ich kann nicht alle 6 Tabellen bedingt formatieren" ist zu vage. – Omni
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
Als erstes sollten Sie die DataTable IDs nicht als Zahlen nennen, sondern eher: Table1, Table2 ... –