I've this ShinyAlert pop-up:
library(shiny)
library(shinyalert)
ui <- fluidPage(
useShinyalert()
)
foo <- function(){
print("hi")
}
server <- function(input, output) {
shinyalert(
title = "Hello",
size = "s",
closeOnEsc = TRUE,
closeOnClickOutside = FALSE,
html = TRUE,
type = "success",
showConfirmButton = TRUE,
showCancelButton = FALSE,
confirmButtonText = "OK",
confirmButtonCol = "#AEDEF4",
timer = 0,
imageUrl = "",
animation = TRUE,
text =
HTML("<br><p >
<div class= 'ignore-css' ;align='center'>
<button id='close' style= 'width:1px;height:1px; color: #fff; background-color: white;' class='closing', onclick=",foo(),"><img src='delete-button.png' alt ='closr'
style='width:30px;height:30px; !important' /></button>
</div>
</p> " )
)
}
shinyApp(ui, server)
I want to call R function - foo() when click on the button. Now it is calling automatically when run the code.
any suggestions will be welcome
You can give the
shinyalert
a particularinputId
which can be used to triggerfoo()
after the alert closes. This can be archived by subscribing to the correspondinginput
usingobserveEvent
.However, the alert is designed to work as an independent modal module. The only job of an alert is to message the user. It should not trigger something else. Use the normal
actionbutton
instead to get a cleaner explicit code. Shiny follows a design pattern in which, if possible, the UI and the business in the logic are clearly separated in functionsui
andserver
, respectively.