How to render different table in modalDialog in shiny

735 views Asked by At

When I render the table mtcars I cant get back to my usual homepage. So I want to choose a dataframe in my pop up window and after showing I want to close my window so that I get back to my homepage. How to implement this idea in the following code

library(shiny)
library(ggplot2)

diamonds <- diamonds
mtcars <- mtcars

shinyApp(
  ui = basicPage(
    actionButton("show", "Show modal dialog"),
    verbatimTextOutput("dataInfo")
  ),
  
  server = function(input, output) {
    vals <- reactiveValues(data = NULL)
    
    dataModal <- function(failed = FALSE) {
      modalDialog(
        textInput("dataset", "Choose data set",
                  placeholder = 'Try "mtcars" or "diamonds"'
        ),
        span('(Try the name of a valid data object like "mtcars" or "diamonds", ',
             'then a name of a non-existent object like "abc")'),
        if (failed)
          div(tags$b("Invalid name of data object", style = "color: red;")),
        
        footer = tagList(
          modalButton("Cancel"),
          actionButton("ok", "OK")
        )
      )
    }
    
    observeEvent(input$show, {
      showModal(dataModal())
   
    
    observeEvent(input$ok, {
   
      if (!is.null(input$dataset) && nzchar(input$dataset) &&
          exists(input$dataset) && is.data.frame(get(input$dataset))) {
        vals$data <- get(input$dataset)
        
        output$dataInfo <- renderPrint({
          if (is.null(vals$data))
            "No data selected"
          else
            summary(vals$data)
        })
        
        removeModal()
      } else {
        showModal(dataModal(failed = TRUE))
      }
    })
    
    output$dataInfo <- renderPrint({
      if (is.null(vals$data))
        "No data selected"
      else
        summary(vals$data)
    })
    
    })
  }
) 

How to choose between diamonds and mtcars so that a table pops up and after closing the dialog I get back to my homepage.

1

There are 1 answers

1
jimbo On

So I can do it. Is there a smarter way? How to display the dataframe in a bigger pop up window.

shinyApp(
  ui = basicPage(
    actionButton("show", "Show modal dialog")
    #verbatimTextOutput("dataInfo"),
   
  ),
  
  server = function(input, output) {

    vals <- reactiveValues(data = NULL)

    dataModal <- function(failed = FALSE) {
      modalDialog(
        textInput("dataset", "Choose data set",
                  placeholder = 'Try "mtcars" or "abc"'
        ),
        span('(Try the name of a valid data object like "mtcars" or "diamonds", ',
             'then a name of a non-existent object like "abc")'),
        if (failed)
          div(tags$b("Invalid name of data object", style = "color: red;")),
        
        footer = tagList(
          modalButton("Cancel"),
          actionButton("ok", "OK")
        )
      )
    }
    
    
    dataModal2 <- function() {
      
      modalDialog(
        
        dataTableOutput("table"),
        
        footer = tagList(
          modalButton("Cancel"),
          actionButton("ok2", "OK")
        )
      )
    }
    

    observeEvent(input$show, {
      showModal(dataModal())
      #showModal(modalDialog(rHandsontableOutput("hot")))
    })    
    

    observeEvent(input$ok, {
    
      if (!is.null(input$dataset) && nzchar(input$dataset) &&
          exists(input$dataset) && is.data.frame(get(input$dataset))) {
        vals$data <- get(input$dataset)
        
        output$table <- renderDataTable(summary(vals$data))
      
        showModal(dataModal2())
        
      } else {
        showModal(dataModal(failed = TRUE))
      }
    })
    
    observeEvent(input$ok2, {
       
      removeModal()
      
    })
  }
)