单击后在闪亮的data.table中重命名操作按钮

时间:2020-09-16 12:54:55

标签: r shiny shinydashboard

enter image description here

单击任何按钮“ rename-me”后,我想调用label_function(),它会产生一个随机值,该值成为各个按钮的新按钮标签。使用updateActionButton机制不会显示,即标签不会更改。

此外,当单击按钮时,同时选择表格行。是否可以仅单击按钮,否则行选择可能会导致其他触发,但如果在其他位置单击该行,则保留行选择?

正在运行的参考代码是:

library(shiny)
library(data.table)
library(DT)

ui = fluidPage(
  mainPanel(
    DT::dataTableOutput("datatable")
  )
)

server = function(session, input, output) {
  
  shinyInput <- function(FUN, len, id, ...) {
    inputs <- character(len)
    for (i in seq_len(len)) {
      inputs[i] <- as.character(FUN(paste0(id, i), ...))
    }
    inputs
  }
  
  label_function <- function(id = character()) {
    round(rnorm(1, 0, 1), 2)
  }
  
  employee <- c('John Doe','Peter Gynn','Jolie Hope')
  salary <- c(21000, 23400, 26800)
  startdate <- as.Date(c('2010-11-1','2008-3-25','2007-3-14'))
  dt <- setDT(data.frame(employee, salary, startdate))
  
  dt <- dt[, action := shinyInput(actionButton, 
                                  dim(dt)[1], 
                                  'button_', 
                                  label = "rename-me", 
                                  onclick = 'Shiny.setInputValue(\"select_button\",  this.id.concat(\"_\", Math.random()))')]
  
  output$datatable <- DT::renderDataTable(DT::datatable({dt}, rownames = FALSE, escape = FALSE))
  
  observeEvent(input$select_button, {
    rowid <- as.numeric(strsplit(input$select_button, "_")[[1]][2])
    selected_botton <- paste0("botton_", rowid)
    updateActionButton(session, selected_botton, label = label_function(id = selected_botton))
    print(paste0("botton_", rowid))
  })
}

# Run the application 
shinyApp(ui = ui, server = server)

0 个答案:

没有答案
相关问题