首页 > 解决方案 > 如何在闪亮的modalDialog中呈现不同的表格

问题描述

当我渲染表 mtcars 时,我无法回到我通常的主页。所以我想在我的弹出窗口中选择一个数据框,在显示后我想关闭我的窗口以便我回到我的主页。如何在下面的代码中实现这个想法

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)
    })
    
    })
  }
) 

如何在 diamonds 和 mtcars 之间进行选择,以便弹出一个表格,关闭对话框后我回到我的主页。

标签: rshinymodal-dialogshowmodaldialog

解决方案


所以我可以做到。有没有更聪明的方法?如何在更大的弹出窗口中显示数据框。

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()
      
    })
  }
)

推荐阅读