Shiny R中的条件格式多个表
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Shiny R中的条件格式多个表相关的知识,希望对你有一定的参考价值。
我正在R中构建一个Shiny应用程序来显示多个数据框,在1个Shiny页面上共有6个。我能够在Shiny条件格式化1个数据表,但我无法有条件地格式化所有6个表。 6个表中的每一个都有5个变量,我希望每个数据表中都有第2个变量:如果> = 75,背景亮红色和粗体,如果<= 75,则亮绿色和粗体:
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')
答案
我已经尝试过你的代码,它对我来说效果很好,看看下面:
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");
以上是关于Shiny R中的条件格式多个表的主要内容,如果未能解决你的问题,请参考以下文章