Home > Enterprise >  Calling R function when click on an HTML button
Calling R function when click on an HTML button

Time:05-23

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

CodePudding user response:

You can give the shinyalert a particular inputId which can be used to trigger foo() after the alert closes. This can be archived by subscribing to the corresponding input using observeEvent.

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 functions ui and server, respectively.

library(shiny)
library(shinyalert)

ui <- fluidPage(
  useShinyalert()
)


foo <- function() {
  print("hi")
}

server <- function(input, output) {
  observeEvent(
    eventExpr = input$myAltert,
    handlerExpr = foo()
  )

  shinyalert(
    inputId = "myAltert",
    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',<img src='delete-button.png' alt ='closr'
               style='width:30px;height:30px; !important' /></button>
              </div>
             </p> ")
  )
}

shinyApp(ui, server)

CodePudding user response:

@danloo answer is correct but I add another one for knowledge.

You can use the callbackR=foo parameter and remove onclick=",foo(). The callbackR is a function that gets called when the modal exits.

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,
    callbackR = foo,
    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'><img src='delete-button.png' alt ='closr' 
               style='width:30px;height:30px; !important' /></button>
              </div>
             </p> " ) 
    
  )
  • Related