【问题标题】:Change backgorund color of cell of data table while its value is edited in Rshiny在 Rshiny 中编辑其值时更改数据表单元格的背景颜色
【发布时间】:2021-07-01 01:56:18
【问题描述】:

我有带有可编辑 = TRUE 选项的 renderDatatable,我正在寻找的是当用户修改单元格的任何值时,单元格背景颜色应该改变(比如 -“绿色”)。这是必要的,因为最终用户可以在稍后查看时了解他/她对表格所做的更改。

下面是我正在尝试的代码

library(shiny)
library(shinydashboard)
library(tidyverse)
library(DT)
library(data.table)

header <- dashboardHeader(title = "demo")
sidebar <- dashboardSidebar(
  sidebarMenu(id = 'sidebarmenu',
              menuItem("admin", tabName = "admin", icon = icon("adjust"))
              
  )
)

body <- dashboardBody(
  tabItems(
    tabItem(
      tabName = 'admin', 
      fluidRow(
          dataTableOutput('userTable')
        
      )

    )
  )
)


ui <- dashboardPage(title = 'admin function test', header, sidebar, body, skin='blue')

server <- function(input, output, session){
  
  dat <- data.table::data.table(v1 = c(1,2,3), v2 = c(2,3,4), v3=c(4,5,8), v4=c("a","b","c"))

  ###Tracking Changes###
  rvs <- reactiveValues(
    data = NA, #dynamic data object,
    logical = NA
  )
  
  observe({
    rvs$data <- dat
  })
  
  
  observeEvent(input$userTable_cell_edit, {
    rvs$data <<- editData(rvs$data, input$userTable_cell_edit, rownames = FALSE,resetPaging = TRUE)
    
    ## below code is to keep track of cell that is edited
    
    rvs$logical  <<- rvs$data == dat 
    
  
  })
  
  
  output$userTable <- renderDataTable({
    #rvs$data[, v3 := v1+v2]
    DT::datatable(rvs$data,editable = TRUE,rownames = FALSE) %>% formatStyle(
      
      colnames(rvs$data),
      target = "cell",
      
      ## here I am trying to change background color of cell which has been 
      ## edited using refrence from TRUE/FALSE of matrix rvs$logical
      ## but it is not working
      backgroundColor = styleEqual( c(1,0), c('green', 'red') )
      
      
    )
                  
  })
  
}  
 

shinyApp(ui = ui, server = server)

【问题讨论】:

  • 请提供一个您已尝试解决问题的可重现示例。
  • FAlonso——我已经用我正在尝试的代码编辑了查询。感谢您的回复。

标签: r shiny dt


【解决方案1】:
library(shiny)
library(shinyjs)
library(DT)

js <- HTML(
  "function colorizeCell(i, j){
    var selector = '#dtable tr:nth-child(' + i + ') td:nth-child(' + j + ')';
    $(selector).css({'background-color': 'yellow'});
  }"
)

colorizeCell <- function(i, j){
  sprintf("colorizeCell(%d, %d)", i, j)
}

ui <- fluidPage(
  useShinyjs(),
  tags$head(
    tags$script(js)
  ),
  br(),
  DTOutput("dtable")
)

dat <- iris[1:5, ]

server <- function(input, output, session){
  
  output[["dtable"]] <- renderDT({
    datatable(dat, editable = TRUE, selection = "none")
  }, server = FALSE)
  
  observeEvent(input[["dtable_cell_edit"]], {
    info <- input[["dtable_cell_edit"]]
    i <- info[["row"]]
    j <- info[["col"]]
    runjs(colorizeCell(i, j+1))
  })
  
}

shinyApp(ui, server)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-23
    • 2012-12-05
    • 2015-08-22
    • 1970-01-01
    • 1970-01-01
    • 2015-08-30
    • 1970-01-01
    • 2013-06-15
    相关资源
    最近更新 更多