【发布时间】:2021-07-05 11:54:44
【问题描述】:
我正在尝试为数据表中的所有行创建一个操作按钮(这只是一个示例。但实际表包含很多行)。因为,我已经硬编码了观察事件。因为我在这里面临一些问题。我现在得到的输出只有在单击第一行操作按钮时才会触发观察事件,但我需要在单击所有行时触发此触发器。 (基本上是一个for循环)
library(shiny)
library(shinydashboard)
library(DT)
library(glue)
number_compare <- data.frame(replicate(2, sample(1:100, 10, rep=TRUE)))
sidebar <- dashboardSidebar()
body <- dashboardBody(
fluidRow(box(width = 12, solidHeader = TRUE,
DTOutput("example_table"))
)
)
ui <- dashboardPage(dashboardHeader(title = "Example"),
sidebar,
body
)
server <- function(input, output) {
number_compare <- number_compare %>% mutate(rn = row_number(), button = glue::glue(HTML('<button id="button{rn}" type="button" class="btn btn-default action-button">Ask a question</button>')))
output$example_table <- DT::renderDT({
datatable(
number_compare,
escape = FALSE
,options=list(preDrawCallback=JS(
'function() {
Shiny.unbindAll(this.api().table().node());}'),
drawCallback= JS(
'function(settings) {
Shiny.bindAll(this.api().table().node());}')))
})
observeEvent(input[[paste0("button",number_compare["rn"][1,])]],{
print("Cleicked")
})
}
shinyApp(ui, server)
【问题讨论】: