Exporting datatable to Excel with cell background colors

Hello Shiny developers,

I'm developing a Shiny App and I don't know anything about JavaScript. On my app, there is a datatable from DT package. I used formatStyle and formatCurrency functions to get a better info. As you can see the Review column on the table, there are 5 categories with their own special color.

When I want to export the data, I click Excel button and it gives me an excel file. In the excel file, I can't see the colors on the Review column but formatCurrency works.

How can I keep colors in the excel file after I export the data?

In terms of reporting what you do, I think that your answers will help for everyone.

Thanks!

These are my codes.

# Datatable JavaScript
DT_JS = DT::JS(
  "function(settings, json) {",
  # Columns Background Color
  "$(this.api().table().header()).css({'background-color': '#002749', 'color': 'white'});",
   "}"
# Datatable Options
opt2 <- list(
  autoWidth = TRUE,
  columnDefs = list(
    list(width = '80px', targets=c(0, 4, 11, 12)), 
    list(width = '100px', targets=c(1)), 
    list(width = '150px', targets=c(2,10)), 
    list(width = '350px', targets=c(20,21)), 
    list(width = '50px', targets= c(14, 15,16,17,18, 19)) 
  ) ,
  dom = 'PBrltip',
  #Scroll
  scrollY = 650,
  scroller = TRUE,
  scrollX = TRUE,
  # Page Length
  pageLength = 25,
  # JavaScript
  initComplete = DT_JS,
  # Search
  search = list(regex = TRUE, caseInsensitive = FALSE),
  #Buttons
  buttons = c("excel", 'copy')
)
#  Review Table
  output$responses_table2 <- renderDataTable({
    datatable(
      review_df() %>% 
        mutate(
          Player = paste0('<a href = "', URL, '">',Player, '</a>')
          ) %>% 
        select(-row_id,-URL) %>% 
        rename_all(funs(gsub("[[:punct:]]", " ", .))),
      rownames = FALSE,
      escape = FALSE,
      filter = list(position = 'top', clear = FALSE, plain = F),
      class = 'cell-border stripe',
      selection = "multiple",

      # Options
      options = opt2,
      # Buttons
      extensions = c('Buttons')

      ) %>% 
      formatCurrency("Market Value",currency = "€") %>% 

      # Column Background Color
      formatStyle(
        'Review',
        target = 'cell',
        backgroundColor = styleEqual(c("Very Good", "Good", "Fair","Average","Bad",NA_character_), c("#00B050","#B1CF95","yellow","orange","red", NA_character_))
        ) %>% 
      formatStyle(
        c("Not Needed", "Youngster", 'Rotation', "First Team", "Key Player"),
        target = 'cell',
        backgroundColor = styleEqual(c("X",NA_character_), c("#98D7EE",NA_character_))
      )
    
  })

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.