0

I want to conditionally format rows in tables. If a column has 1 then the column should be highlighted else not. I want to do this in plotly. Data is like:

Name Purchased
a    0
b    1
c    0
d    1

I want to highlight row with name b and d.

I am using the following type of code with plotly:

plot_ly(
          type = 'table',
          columnwidth = c(100, 100, 100),
          columnorder = c(0, 1, 2),
          header = list(
            values =  c(A,B),
            align = c("center", "center"),
            line = list(width = 1, color = 'black'),
            fill = list(color = c("grey", "grey")),
            font = list(family = "Arial", size = 14, color = "white")
          ),
          cells = list(
            values = rbind(df[[A]],df[[B]]),
            align = c("center", "center"),
            line = list(color = "black", width = 1),
            font = list(family = "Arial", size = 12, color = c("black"))
          ))

I am writing this code for R shiny app. Please let me know how can I do it.

Priyesh
  • 53
  • 5
  • your example is not [reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example), can you edit it? – bretauv Feb 19 '20 at 10:24
  • https://plot.ly/r/table/ , this link has tables made in plotly. I just want to highlight rows based on conditions. I have edited the above example because can't use the original code, it might have mistakes. – Priyesh Feb 20 '20 at 12:44

1 Answers1

1

I would add a highlight variable in the original dataset for each row that must be highlighted.

df <- df %>% 
  mutate(highlgt = ifelse(Purchased == 1, "lightblue", "white"))

Then, in the cells argument, update the fill value with a list of highlight values.
(I also changed the header/values and cells/values values to avoid error messages).

plot_ly(
  type = 'table',
  columnwidth = c(100, 100),
  #columnorder = c(0, 1, 2),
  header = list(
    values =  c("A","B"),
    align = c("center", "center"),
    line = list(width = 1, color = 'black'),
    fill = list(color = c("grey", "grey")),
    font = list(family = "Arial", size = 14, color = "white")
  ),
  cells = list(
    values = rbind(df[[1]], df[[2]]),
    align = c("center", "center"),
    line = list(color = "black", width = 1),
    font = list(family = "Arial", size = 12, color = c("black")),
    fill = list(color = list(df$highlgt))   # highlight rows
  ))

plotly_table_highlight_rows

i94pxoe
  • 577
  • 3
  • 11