因此,我创建了一个表,其中每行包含一个操作按钮,问题是当我在同一按钮中连续单击两次时,该按钮没有响应。我必须单击下面的按钮,然后再次单击上一个按钮才能使其正常工作。而且我发现这对于最终用户来说并不是很方便。
这是我的代码:
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
}
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 = "Delete",
onclick = 'Shiny.onInputChange(\"select_button\", this.id)')]
output$datatable <- DT::renderDataTable(DT::datatable({dt},
rownames = FALSE, escape = FALSE))
observeEvent(input$select_button, {
showModal(modalDialog(
title="TEST",
h3(paste("Hello world")),
footer = tagList(actionButton("confirmDelete", "Delete"),
modalButton("Cancel")
)
))
})
observeEvent(input$confirmDelete, {
removeModal()
})
}
# Run the application
shinyApp(ui = ui, server = server)
Run Code Online (Sandbox Code Playgroud)
有人有办法修复这个错误吗?
sta*_*rja 10
您此处使用的模式是单击按钮会更改input$select_buttonvia的值Shiny.onInputChange。为此输入设置的值是按钮 id,例如button_1。当您重复单击同一按钮时,该情况保持不变。我的解决方案是另外向 的值添加一个随机数input$select_button,因此它总是会发生变化并observeEvent触发 。请注意,建议使用Shiny.setInputValue代替Shiny.onInputChange.
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
}
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 = "Delete",
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])
showModal(modalDialog(
title="TEST",
h3(paste("Hello world")),
footer = tagList(actionButton("confirmDelete", "Delete"),
modalButton("Cancel")
)
))
})
observeEvent(input$confirmDelete, {
removeModal()
})
}
# Run the application
shinyApp(ui = ui, server = server)
Run Code Online (Sandbox Code Playgroud)