為了上帝的愛,有人可以幫助我嗎?如何modalDialog在回圈內使用?我看過一些論壇,但沒有一個令人滿意或不適合我的問題。下面是模擬我的問題的最小可重現代碼。RShiny中提出的解決方案:如何在 for 回圈中使用順序模式不起作用,因為actionbutton我在函式的text引數中放入的.shinyalertobserveEvent
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)
在此先感謝您提供幫助的任何嘗試。
uj5u.com熱心網友回復:
這是一種使用shinyalert的方法。這很奇怪,如果您在閃亮警報中放置一個按鈕,然后單擊該按鈕會自動關閉警報。這就是為什么我使用actionLink而不是actionButton. 單擊一個按鈕/鏈接時,該應用程式不會像往常一樣做出反應,因此我使用onclick運行屬性Shiny.setInputValue。如您所見,我local在回圈中使用,否則無法按預期作業。但我認為(我沒有測驗)for local你可以使用普通的lapply.
library(shiny)
library(shinyalert)
dialog_filtro <- function(i, input, session, RESFIL, ID, LabelID, messagee) {
al <- shinyalert(
title = "Menssagem importante",
text = tagList(
tags$p(messagee),
actionLink(
ID[1], LabelID[1], class = "btn btn-primary",
onclick = sprintf(
'Shiny.setInputValue("%s", true, {priority: "event"});',
ID[1]
)
),
actionLink(
ID[2], LabelID[2], class = "btn btn-primary",
onclick = sprintf(
'Shiny.setInputValue("%s", true, {priority: "event"});',
ID[2]
)
),
),
html = TRUE,
session = session
)
observeEvent(input[[ID[1]]], {
RESFIL$dest[[i]] <- i 10
closeAlert(id = al)
}, domain = session)
observeEvent(input[[ID[2]]], {
RESFIL$dest[[i]] <- i 100
closeAlert(id = al)
}, domain = session)
}
ui <- fluidPage(
verbatimTextOutput("res")
)
server <- function(input, output, session) {
RESFIL <- reactiveValues(dest = NULL)
lista <- list(a = 2, a = 3)
grupdest <- rep(list(0), length(lista))
RESFIL$dest <- grupdest
for (i0 in 1:length(lista)) {
local({
i <- i0
if (lista[[i]] > 0) {
dialog_filtro(
i,
input,
session,
RESFIL,
ID = c(paste0("yes", i), paste0("no", i)),
LabelID = c("Yes", "No"),
messagee = paste0("This is the loop ", i)
)
} else {
RESFIL$dest[[i]] <- i 1000
# removeModal() not clear what you want to do here: remove which modal?
}
})
}
output$res <- renderPrint({
RESFIL$dest
})
}
shinyApp(ui = ui, server = server)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/481511.html
