jrowen / rhandsontable

A htmlwidgets implementation of Handsontable.js
http://jrowen.github.io/rhandsontable/
Other
385 stars 148 forks source link

shiny actionButton in cell #413

Closed dleopold closed 2 years ago

dleopold commented 2 years ago

I would to place dynamically generated action buttons in rhandsontable cells, but can not seem to figure out how to do it. I can get an actionLink to work, but the action buttons to not seem to render properly. Here is an example shiny app with a table that has working actionLinks, and my failed attempt at adding actionButtons:

library(shiny)
library(rhandsontable)

ui <- fluidPage(

  rHandsontableOutput("hot")

)

server <- function(input, output, session) {

  tab <- reactive({
    tab <- iris
    links <- lapply(seq_len(nrow(tab)), function(idx){
      as.character(
        actionLink(
          inputId = paste("link", idx,tab$Species[idx],sep="-"),
          label = "LINK",
          onClick = sprintf("Shiny.onInputChange('%s', this.id)", "printSpp")
        )
      )
    })
    buttons <- lapply(seq_len(nrow(tab)), function(idx){
      as.character(
        actionButton(
          inputId = paste("button",idx,tab$Species[idx],sep="-"),
          label = "BUTTON",
          onClick = sprintf("Shiny.onInputChange('%s', this.id)", "printSpp")
        )
      )
    })
    tab$links <- links
    tab$buttons <- buttons
    return(tab)
  })

  output$hot <- renderRHandsontable({
    rhandsontable(tab()) |>
      hot_col('links', renderer = htmlwidgets::JS("safeHtmlRenderer")) |>
      hot_col('buttons', renderer = htmlwidgets::JS("safeHtmlRenderer"))
  })

  observeEvent(input$printSpp, ignoreInit = T, {
    print(input$printSpp)
  })

}

# Run the application
shinyApp(ui = ui, server = server)
dleopold commented 2 years ago

I found the answer after reading the docs again. There is a hidden parameter, allowedTags that can be used. So setting allowedTags="<a><button>" in rhandsontable() the example above will allow both the actionLinks and actionButtons to render properly.