Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate functionality sendSweetAlert buttons in Shiny

Tags:

r

shiny

Friends, I created two buttons (YES and NO) in the alert made by sendSweetAlert in Shiny. How do I generate a functionality in the Yes button so that when I press YES, a list with the df database industries appears? Every help is welcome. The executable code is below.

library(shiny)
library(rdist)
library(geosphere)
library(tidyverse)
library(shinyWidgets)
library(shinythemes)

function.cl<-function(df){

  #database df
  df<-structure(list(Industries = c(1,2,3,4,5,6,7), 
                     Latitude = c(-23.8, -23.8, -23.9, -23.9, -23.9,-23.8,-23.8), 
                     Longitude = c(-49.8, -49.8, -49.5, -49.8, -49.8,-49.8,-49.8), 
                     Waste = c(526, 350, 526, 469, 285, 433, 456)), class = "data.frame", row.names = c(NA, -7L))


  coordinates<-subset(df,select=c("Latitude","Longitude")) 
  d<-distm(coordinates[,2:1]) 
  diag(d)<-1000000 
  min_distancia<-as.matrix(apply(d,MARGIN=2,FUN=min))
  limite<-mean(min_distancia)+sd(min_distancia) 

  search_vec <- function(mat, vec, dim = 1, tol = 1e-7, fun = all)
    which(apply(mat, dim, function(x) fun((x - vec) > tol)))
  ind_exclude<-search_vec(min_distancia,limite,fun=any)
  if(is_empty(ind_exclude)==FALSE){
    for (i in 1:dim(as.array(ind_exclude))){
      df<-subset(df,Industries!=ind_exclude[i])}}

  return(list(
    "IND" =  ind_exclude

  ))

}

ui <- bootstrapPage(
  navbarPage(theme = shinytheme("flatly"), collapsible = TRUE,
             "Cl", 
             tabPanel("Solution",
                      sidebarLayout(
                        sidebarPanel(

                        ),
                        mainPanel(
                          tabsetPanel())))))  

server <- function(input, output, session) {

  Modelcl<-reactive({
    function.cl(df)
  })

  output$ind <- renderTable({
    IND <- ((Modelcl()[[1]]))
  })

  observe({
    if(is_empty(Modelcl()[[1]])==FALSE){
      sendSweetAlert(session = session,
                     title = "Hey",
                     btn_labels = c("Yes", "No"),
                     text = tags$div(h5("The industries that need to exclude are:"), 
                                     paste(Modelcl()[[1]], collapse = ", ")),
                     type = "info"
      )
    }
  })



}

shinyApp(ui = ui, server = server)

Thank you very much friends!

like image 201
Antonio Avatar asked Dec 01 '25 10:12

Antonio


1 Answers

This is the easiest way that i know

library(shiny)
library(shinyWidgets)
library(DT)
ui<-fluidPage(actionButton("actionbuttonID","Question"))

server <- function(input, output,session) {
  output$row_modif<-renderDT({ #row_modif is your id for the table
    
    datatable(
      iris, selection = "single") #replace iris by your dataframe
    
  })
  observeEvent(input$actionbuttonID,{ #in this line your actionbutton id
    confirmSweetAlert(    #when you click in your action button a confirmsweetalert open
      btn_labels = c("No","Yes"), # false first true second
      session = session,
      inputId = "idforthequestion", #  input$idforthequestion important for if()
      type = "warning", #success, info, question... 
      title = paste0("Your Title here"),
      danger_mode = F,
      closeOnClickOutside = T,
      showCloseButton =T
    )})
  observeEvent(input$idforthequestion,{ #observe your confirm sweet alert
    if(input$idforthequestion) { 
     #if is true do something 
      showModal(modalDialog(
        fluidPage(
          h3(strong("Your Another Title here"),align="center"),
          hr(),
          DTOutput('row_modif')))) } #row_modif is your id for the table}
      if(isFALSE(input$idforthequestion)){
        print("False")
      }
    
    } )
  
  } 

  shinyApp(ui,server)

like image 163
Vinicius Soares Avatar answered Dec 04 '25 02:12

Vinicius Soares