我有一个函数,显示带有一个输入的模态,用于更改在单击保存按钮时执行函数的名称,并且我有验证绑定到输入:
editNameDialog <- function(input, value, name, save) {
showModal(modalDialog(
textInput("modalNewName", paste("Enter", name, ":"), value),
title = paste("Edit", name),
easyClose = TRUE,
footer = div(
modalButton("Cancel"),
actionButton("modalSaveName", "Save", class = "btn-primary")
)
))
validName <- function() {
nchar(input$newName) > 5
}
observeEvent(input$modalNewName, {
if (validName()) {
shinyjs::enable("modalSaveName")
} else {
shinyjs::disable("modalSaveName")
}
});
observeEvent(input$modalSaveName, {
save(input$modalNewName)
removeModal()
}, ignoreInit = TRUE, once = TRUE)
}
我这样调用这个函数(来自server.R):
editNameDialog(input, "default value", "enter name", function(value) {
other$name <- value
})
它工作但是当我执行该函数两次时,观察者输入$ modalNewName为每个按键执行两次。如何在添加新事件之前删除该事件?
答案 0 :(得分:1)
我通过在javascript中添加验证来修复此问题:
shinyjs::runjs("observeModalName()")
并执行:
> pairs [1,2] [3,4]
[(1,3),(1,4),(2,3),(2,4)]