Chris Woodfield
Chris Woodfield

Reputation: 107

DataTable in Shiny App not displaying Icons in Each Row

Can anyone please tell me why the icons arent displaying in the rendered data table? All I can see is the code as opposed to one Trash Icon in the remove column for each of the rows in my table

It's worked fine for me on previous Shiny Apps - but when starting again for a new project I cant work out what's different.

Thanks!

library(dplyr)
library(data.table)
library(dplyr)
library(DT)


values <- data.table(a = c(1,2,3),b = c(4,5,6))

ui <- fluidPage(
  DT::dataTableOutput('TabBU')
)

server <- function(input, output,session) {
  
  shinyInput <- function(FUN, n, id, ses, ...) {
    as.character(FUN(paste0(id, n), ...))
  }
  
  getRemoveButton <- function(n, idS = "", lab = "Pit") {
    if (stringr::str_length(idS) > 0) idS <- paste0(idS, "-")
    ret <- shinyInput(actionButton, n,
                      'button_', label = "Remove",icon = icon("trash-alt"),
                      onclick = sprintf('Shiny.onInputChange(\"%sremove_button_%s\",  this.id)' ,idS, lab))
    return (ret)
  }
  
  
  
  
  values = values %>%
    mutate(id = 1:nrow(values)) 
  
  values = values %>%
    rowwise() %>%
    mutate(Remove = getRemoveButton(id, idS = "", lab = "Tab1")) 
  
  output$TabBU <- renderDT(values)
  
}





shinyApp(ui, server)

Upvotes: 1

Views: 596

Answers (2)

Luther Blissett
Luther Blissett

Reputation: 523

This is an old question but I had the same issue and for whatever reason, placing an actionButton with an icon outside of the datatable makes the icons show up on the buttons inside the datatable. I have no idea why this works, but I used the incredibly kludgy approach of putting that button inside of a div with display: none set so it doesn't show:

library(shiny)
library(DT)
library(tidyverse)

ui <- shinyUI({
  fluidPage(
    # comment out this top div and the icons disappear! why? who knows?!
    tags$div(
      class="container",
      style="display: none;",
      tags$div(
        style="margin-top: 50xp;",
        actionButton(
          "add_thing",
          label="do it",
          class="btn-success",
          icon=icon("plus")
        )
      )
    ),
    tags$div(
      class = "container",
      style = "margin-top: 50px;",
      DT::DTOutput("tabs",width="100%")
    )
  )
})



create_btns <- function(x) {
  x %>%
    purrr::map_chr(~
                     paste0(
                       '<div class = "btn-group">
                   <button class="btn btn-default action-button btn-info action_button" id="edit_',
                       .x, '" type="button" onclick=get_id(this.id)><i class="fas fa-edit"></i></button>
                   <button class="btn btn-default action-button btn-danger action_button" id="delete_',
                       .x, '" type="button" onclick=get_id(this.id)><i class="fa fa-trash-alt"></i></button></div>'
                     ))
}

server <- shinyServer(function(input,output,session) {
  dataset <- reactive({
    mtcars %>%
      rownames_to_column("car") %>%
      mutate(buttons = create_btns(1:nrow(.)))
  })
  
  output$tabs <- renderDT({
    datatable(
      isolate(dataset()),
      escape = F,
      rownames = FALSE,
      selection="none"
    )
  })
})

shinyApp(ui,server)

Upvotes: 1

Adriana Huante
Adriana Huante

Reputation: 362

If you want the Icon and the label, you can try writing the icon argument after onclick, like this:

ret <- shinyInput(actionButton, n,
                    'button_', label = "Remove",
                    onclick = sprintf('Shiny.onInputChange(\"%sremove_button_%s\",  this.id)' ,idS, lab),
                    style = "color: white;
                           background-color: red",
                    icon = icon("trash"))

And if you want just the icon you can set label = ""

Upvotes: 0

Related Questions