【发布时间】:2022-07-31 04:25:04
【问题描述】:
如何在循环中使用modalDialog?我查看了一些论坛,但没有一个令人满意或不适合我的问题。
以下是模拟我的问题的最小可重现代码。 RShiny: How to have sequential Modals in for loop 中提出的解决方案不起作用,因为我在shinyalert 函数的text 参数中输入的actionbutton 无法在observeEvent 中识别。
library(shiny)
dialog_filtro <- function(ID,LabelID,messagee){
modalDialog(
title = "Menssagem importante",
messagee,
footer = tagList(
actionButton(ID[1],LabelID[1]),
actionButton(ID[2],LabelID[2])
)
)
}
ui <- fluidPage(
uiOutput('res')
)
server <- function(input, output, session) {
RESFIL <- reactiveValues(dest = NULL)
lista <- list(a=2,a=3)
grupdest <- rep(list(NA),length(lista))
RESFIL$dest <- grupdest
for(i in 1:length(lista)){
if(lista[[i]] > 0){
showModal(dialog_filtro(ID = c(paste0('yes',i),paste0('no',i)),
LabelID = c('Yes','No'),
messagee = paste0('This is the loop ',i)
))
observeEvent(input[[paste0('yes',i)]], {
RESFIL$dest[[i]] <- i+10
removeModal()
})
observeEvent(input[[paste0('no',i)]], {
RESFIL$dest[[i]] <- i+100
removeModal()
})
}else{
RESFIL$dest[[i]] <- i+1000
removeModal()
}
}
output$res <- renderPrint({ RESFIL$dest })
}
shinyApp(ui = ui, server = server)
【问题讨论】:
-
循环不是这里的问题:你不能打开两个模态。第二个替换第一个。我去看看能不能找到替代方法。