1

I have used the directions in this other question to create a datatable with buttons inside.

I added some formatting to the renderTable() call to highlight in yellow motivation == 3

    output$data <- DT::renderDataTable(
      datatable(df$data) %>% formatStyle(
        'Motivation',
        target = 'row',
        backgroundColor = styleEqual(c(3), c('yellow'))
      ),
      server = FALSE, escape = FALSE, selection = 'none'
    )

This highlights the correct row:

enter image description here

The problem is that color formatting messes up with the buttons. I find the same problem when trying to format dates (datatable automatically shows them in UTC and I want them on local time). Are both formatting and buttons inside the table incompatible?

I get the following warning

renderDataTable ignores ... arguments when expr yields a datatable object; see ?renderDataTable

Here's the code for the app:

library(shiny)
library(DT)

shinyApp(
  ui <- fluidPage(
    DT::dataTableOutput("data"),
    textOutput('myText')
  ),

  server <- function(input, output) {

    myValue <- reactiveValues(employee = '')

    shinyInput <- function(FUN, len, id, ...) {
      inputs <- character(len)
      for (i in seq_len(len)) {
        inputs[i] <- as.character(FUN(paste0(id, i), ...))
      }
      inputs
    }

    df <- reactiveValues(data = data.frame(

      Name = c('Dilbert', 'Alice', 'Wally', 'Ashok', 'Dogbert'),
      Motivation = c(62, 73, 3, 99, 52),
      Actions = shinyInput(actionButton, 5, 'button_', label = "Fire", onclick = 'Shiny.onInputChange(\"select_button\",  this.id)' ),
      stringsAsFactors = FALSE,
      row.names = 1:5
    ))


    output$data <- DT::renderDataTable(
      datatable(df$data) %>% formatStyle(
        'Motivation',
        target = 'row',
        backgroundColor = styleEqual(c(3), c('yellow'))
      ),
      server = FALSE, escape = FALSE, selection = 'none'
    )

    observeEvent(input$select_button, {
      selectedRow <- as.numeric(strsplit(input$select_button, "_")[[1]][2])
      myValue$employee <<- paste('click on ',df$data[selectedRow,1])
    })


    output$myText <- renderText({

      myValue$employee

    })

  }
)

Matias Andina
  • 4,029
  • 4
  • 26
  • 58

1 Answers1

1

You should look at this answer. Applying his advice to your problem, I move escape = FALSE, selection = 'none' in datatable(df$data) and it works (you need to remove server = FALSE, which is not accepted in datatable):

    output$data <- DT::renderDataTable(
      datatable(df$data, escape = FALSE, selection = 'none') %>% formatStyle(
        'Motivation',
        target = 'row',
        backgroundColor = styleEqual(c(3), c('yellow'))
      )
    )

In case the answer I refer above is deleted (don't know if it can be), I also put it here:

You are getting this error because you are returning a DT::datatable AND you are also specifying filter='top' as one of the ... arguments to DT::renderDataTable. As the message is trying to tell you ... arguments are ignored since you are returning a DT::datatable. This is because the ... arguments are intended to be passed through to the DT:datatable constructor.

Either move filter='top' inside the DT::datatable constructor or return a data.frame and the filter='top will be used when DT::renderDataTable constructs a DT::datatable with your specified data.frame.

Community
  • 1
  • 1
bretauv
  • 7,756
  • 2
  • 20
  • 57