diagModal

Published onesixx on

https://shiny.rstudio.com/reference/shiny/latest/modalDialog.html

https://shiny.rstudio.com/reference/shiny/latest/showModal.html

https://shiny.rstudio.com/reference/shiny/latest/showNotification.html

ui <- fluidPage(
    actionButton("btn_popupEnable", "Show Popup"),
    verbatimTextOutput("text_dataInfo")
)
        
server <- function(input, output, session) {
    storeDataset <- reactiveValues(resultData=NULL)
    
    popupModalUI <- function(failed=FALSE){ 
        modalDialog(
            textInput("myDataset", "Choose data set", placeholder='Enter dataset...'),
            span("(Try 'mtcars', ", "like 'abc')"),
            if(failed) div(tags$b("Invalid name of Data", style="color:red;")),
            
            footer=tagList(
                actionButton("popup_ok", "OK"),
                actionButton("popup_cancel", "Cancel") # modalButton("Cancel")
            )
        )
    }
    
    observeEvent(input$btn_popupEnable, {
        showModal(popupModalUI())
    })
    
    observeEvent(input$popup_ok, {
        if (!is.null(input$myDataset) && nzchar(input$myDataset) && 
                exists(input$myDataset) && is.data.frame(get(input$myDataset))){
            storeDataset$resultData <- get(input$myDataset)
            removeModal()
        } else {
            showModal(popupModalUI(failed=TRUE))
        }
    })
    observeEvent(input$popup_cancel, {
        showNotification("Operation Canceled"#,
            #action = a(href = "javascript:location.reload();", "Reload page")
        )
    })
    
    
    
    # Display information about selected data
    output$text_dataInfo <- renderPrint({
        if (is.null(storeDataset$resultData))
            "No data selected"
        else
            summary(storeDataset$resultData)
    })
}

shinyApp(ui,server)

https://stackoverflow.com/questions/48555012/how-to-close-a-modal-but-with-the-original-model-remain-open

 

I’ve managed to create a MWE Solution for your problem.

  • This has two Modals
  • Inner Modal is triggered when the Ok button of Outer Modal is clicked
  • Outer Modal is shown back when the Dismiss button of Inner Modal is clicked

shinyApp(
  ui = basicPage(
    actionButton("show", "Show modal dialog")  ),

  server = function(input, output) {

    dataModal <- function() {
       modalDialog(
        span('First Modal'),

        footer = tagList(
          modalButton("Cancel"),
          actionButton("ok", "OK")
        )
       )

    }

    subModal <- function(){
      modalDialog(
      span('Inner Modal '),

      footer = tagList(
        modalButton("Cancel"),
        actionButton('dismiss','Dismiss Inner Modal')

      ))
    }

    observeEvent(input$show, {
      showModal(dataModal())
    })


    observeEvent(input$ok, {
       showModal(subModal())
    })

    observeEvent(input$dismiss, {
      showModal(dataModal())
    })



  }
)

 

 

 

 

Categories: Shiny

onesixx

Blog Owner

Leave a Reply

Your email address will not be published.