Jon
Jon

Reputation: 77

R shiny RenderDT format specific cell in table output

I am trying to format a SPECIFIC cell in a R shiny dashboard data table (using renderDT).

In the UI I use the following line of code:

DTOutput('dt_vols')

I also include this line in the UI as I dont want to display column names (not sure if that is relevant to the problem)

tags$head(tags$style(type = "text/css", "#dt_vols th {display:none;}")),

In the server code, I first create the following reactive 2x2 matrix (called dt_vols) - I've simplified the matrix in the example

dt_vols <- reactive({

    mtx_vols <- matrix(1:4, nrow = 2, ncol = 2)

    return(mtx_vols)

  })

Then I render the DT table as follows:

  output$dt_vols = renderDT(

    dt_vols(), options = list(pageLength = 4, dom = 't', autoWidth = FALSE), rownames= FALSE,   
    formatStyle(dt_vols(), columns = 1, border = '1px solid #ddd')

  )

It works until I add the formatstyle line. I am not sure how to get this line right and to get it pointing a specific cell (for example row1, column2). It seems to have a problem with the column argument. If i run the below I get the following error:

Warning: Error in : $ operator is invalid for atomic vectors

Upvotes: 1

Views: 1107

Answers (1)

ismirsehregal
ismirsehregal

Reputation: 33600

formatStyle expects a table object created from datatable() as input - you passed a matrix, which results in the error.

Please check the following:

library(shiny)
library(DT)

ui <- fluidPage(DTOutput('dt_vols'),
                tags$head(tags$style(type = "text/css", "#dt_vols th {display:none;}")))

server <- function(input, output, session) {
  dt_vols <- reactive({
    mtx_vols <- matrix(1:4, nrow = 2, ncol = 2)

    return(mtx_vols)

  })

  output$dt_vols = renderDT({
    myTable <- datatable(dt_vols(),
                         options = list(pageLength = 4, dom = 't', autoWidth = FALSE),
                         rownames = FALSE
    )

    formatStyle(myTable, columns = 1, border = '10px solid #ddd')
  })
}

shinyApp(ui, server)

Result

Upvotes: 1

Related Questions