DT in Shiny: change only the color of one line

I have a dataset:

ID Value
102 306
41  800
101 783
105 193
myID 334

I would like to make it datatable, where only the row with "myID" is orange and the rest of the table is blue. Looking at helper functions and other examples , it seems like I should use styleEqual. However, I do not know what the values ​​in my other lines, and also they will change dynamically.

I tried to use

datatable(tableData) %>%
formatStyle(0, target= 'row',color = 'black', backgroundColor = tableColour, 
                lineHeight='70%', padding = '3px 3px', fontSize = '80%') %>%
    formatStyle('ID', target = 'row', 
    backgroundColor = styleEqual(c("myID"), c('orange')))

However, this does not work - the entire table is blue, and the second instruction is formatStyleignored. If I delete the first formatStyle, I get the line in orange, but all other formatting lose. Is there a way to use styleEqual to determine, for example. c("myID", "All other IDs"), or is there another workaround?

+4
2

:

  • , 1 0, , myID , .
  • ID, , myID, .

. , !


enter image description here

df = read.table(text='ID Value
102 306
41  800
101 783
105 193
myID 334',header=T)

library(DT)

my_vals = unique(df$ID)
my_colors = ifelse(my_vals=='myID','orange','grey')

datatable(df) %>%
  formatStyle('ID', target = 'row', 
              backgroundColor = styleEqual(my_vals,my_colors))
0

DT, styleEqual(), , . , , JS_EVAL ( styleEqual()) :

background <- "value == 'myID' ? 'orange' : value != 'else' ? 'blue' : ''"  
class(background) <- "JS_EVAL"

datatable(tableData) %>% formatStyle(
  'ID',
  target = 'row',
  backgroundColor = background
)

:

enter image description here

, tableHTML:

library(tableHTML)

tableData %>% 
  tableHTML(rownames = FALSE,
            widths = c(100, 100)) %>% 
  add_css_row(rows = which(tableData$ID == 'myID') + 1,
              css = list(c("background-color"),
                         c("orange"))) %>% 
  add_css_row(rows = which(tableData$ID != 'myID') + 1,
              css = list(c("background-color"),
                         c("blue")))

:

enter image description here

+6

Source: https://habr.com/ru/post/1694655/


All Articles