Maison  >  Questions et réponses  >  le corps du texte

Problème possible : résultats inattendus lors de l'utilisation de "background-color" avec "DT"

Modifié depuis la version v0.26 起,R shiny 中使用的 DT 包的 background-color. Est-ce le même problème pour vous et moi ? Est-ce un bug qui fait que changer la couleur de fond ne fonctionne plus ? !

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 version v0.25 sans ou avec changement de couleur de fond :

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

DT version v0.26 sans changer la couleur de fond et changer la couleur de fond :

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

Résumé :

P粉983021177P粉983021177192 Il y a quelques jours407

répondre à tous(1)je répondrai

  • P粉299174094

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

    La couleur d'arrière-plan de la ligne sélectionnée dans la nouvelle version n'est pas définie à l'aide de l'attribut background-color : elle est définie à l'aide de l'attribut box-shadow. Voici comment changer la couleur d'arrière-plan d'une ligne sélectionnée :

    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)
    

    répondre
    0
  • Annulerrépondre