R Shiny: descriptor control buttons in the data table

I have an R Shiny application with a data table. One column contains action buttons with a unique identifier. I would like to handle clicks on these buttons, but unfortunately my event processing code (a simple print statement) is never executed. See This Standalone Example ( app.R ):

 library(shiny) library(DT) ui <- shinyUI( fluidPage( title = "DataTable with Buttons", fluidRow( column( width = 8, dataTableOutput("employees") ) ) ) ) server <- shinyServer(function(input, output) { df <- data.frame( name = c('Dilbert', 'Alice', 'Wally', 'Ashok', 'Dogbert'), motivation = c(62, 73, 3, 99, 52), stringsAsFactors = FALSE ) fireButtons <- list() fireButtonIds <- list() for (r in rownames(df)) { id <- paste("fire_", r, sep = "") fireButtonIds[[r]] <- id button <- actionButton(id, label = "Fire") fireButtons[[r]] <- as.character(button) } df$actions <- fireButtons dt <- datatable(df, colnames = c("#", "Name", "Motivation", "Actions")) output$employees <- renderDataTable(dt) for (id in fireButtonIds) { # binding doesn't work # - is the path wrong? # - is it because the button is really a string, not an object? observeEvent(input$employees$x$data$actions[[id]], { print(paste("click on", i)) }) } }) shinyApp(ui = ui, server = server) 

I see two possible problems:

  • The path I'm using ( input$employees$x$data$actions[[id]] ) is incorrect
  • The path I use is correct, but it does not indicate that it can really be processed, i.e. it's just an HTML string, not a button object.

Or maybe there is a much better way to put buttons inside a data table ...?

+5
source share
1 answer

Does this do what you are trying to do?

 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( df$data, 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 }) } ) 
+9
source

Source: https://habr.com/ru/post/1270970/


All Articles