首頁  >  問答  >  主體

可能的問題:將“background-color”與“DT”一起使用時出現意外結果

自版本 v0.26 起,R shiny 中使用的 DT 套件的 background-color 已更改。這對你和我的問題來說是一樣的嗎?改變背景顏色不再起作用是個錯誤嗎? !

library(shiny)

testUI <- function(id) {
  tagList(
    DT::dataTableOutput(NS(id, "mytable")),
    verbatimTextOutput(NS(id, "selected"))
  )
}

testServer <- function(id) {
  moduleServer(id, function(input,output,session,data) {
    output$mytable <- DT::renderDataTable({
      mtcars
    }, selection = list(mode = "multiple", target = "row"))

    output$selected <- renderPrint(
      input$mytable_rows_selected  # Caution: The prefix must match the id of my namespace
    )

  })
}

testApp <- function(install_version = c("v0.25", "v0.26"), change_background_color = FALSE) {

  stopifnot(is.logical(change_background_color))

  install_version <- match.arg(install_version)

  if (install_version == "v0.25") {
    remotes::install_github("rstudio/DT", ref = "v0.25", force = TRUE, upgrade = TRUE)
  } else {
    remotes::install_github("rstudio/DT", ref = "v0.26", force = TRUE, upgrade = TRUE)
  }

  ui <- fluidPage(

    if (isTRUE(change_background_color)) {
      tags$style(HTML('table.dataTable tr.selected td, table.dataTable td.selected {background-color: #FC8995 !important;}'))  # red color
    },

    testUI("test")
  )
  server <- function(input, output, session) {
    testServer("test")
  }
  shinyApp(ui, server)
}

DT 版本 v0.25 不含或不帶更改背景顏色:

testApp(install_version = "v0.25", change_background_color = FALSE)
testApp(install_version = "v0.25", change_background_color = TRUE)

DT 版本 v0.26 不改變背景顏色和改變背景顏色:

testApp(install_version = "v0.26", change_background_color = FALSE)
testApp(install_version = "v0.26", change_background_color = TRUE)

摘要:

P粉983021177P粉983021177192 天前406

全部回覆(1)我來回復

  • P粉299174094

    P粉2991740942024-03-31 00:47:48

    新版本中所選行的背景顏色不是使用 background-color 屬性設定的:它是使用 box-shadow 屬性。 以下是更改所選行的背景顏色的方法:

    library(shiny)
    library(DT)
    
    css <- "
    table.dataTable tr.selected td, table.dataTable td.selected {
      box-shadow: inset 0 0 0 9999px #FC8995 !important;
    }
    "
    
    ui <- fluidPage(
      tags$style(HTML(css)),
      br(),
      DTOutput("dtable")
    )
    
    server <- function(input, output, session) {
      output[["dtable"]] <- renderDT({
        datatable(iris)
      })
    }
    
    shinyApp(ui, server)
    

    回覆
    0
  • 取消回覆