Shiny: plot results in popup window

前端 未结 3 924
[愿得一人]
[愿得一人] 2020-12-28 22:19

I am trying to build a web app with shiny and I would like to display the resulting plot of a R function in a popup window rather than in mainPanel. For instance, for the b

相关标签:
3条回答
  • 2020-12-28 22:30

    You could use a conditional panel to show/hide an absolute panel containing your plot, setting the condition to some js variable toggled by a function attached to your button .

    e.g.

    conditionalPanel("popupActive==1",
                            absolutePanel(id = "popup", class = "modal", 
                                          fixed = FALSE, draggable = TRUE,
                                          top = 200, right = "auto", left = 400, 
                                          bottom = "auto",
                                          width = 500, height = 500,
                            plotOutput(#output plot stuff#)
                            )
                            )
    

    Then toggle the value of popupActive in js to show/hide

      HTML('<head><script>popupActive = 0; function myFunction(){popupActive=!popupActive;} </script></head>'), HTML('<button id="go" type="button" class="btn btn-default action-button" onclick="myFunction()">Go</button>'),
    
    0 讨论(0)
  • 2020-12-28 22:32

    Look into shinyBS package which offers modal popups. Example below shows the plot upon button click.

    EDIT - Added a download button to the Modal

    rm(list = ls())
    library(shiny)
    library(shinyBS)
    
    shinyApp(
      ui =
        fluidPage(
          sidebarLayout(
            sidebarPanel(numericInput("n", "n", 50),actionButton("go", "Go")),
            mainPanel(
              bsModal("modalExample", "Your plot", "go", size = "large",plotOutput("plot"),downloadButton('downloadPlot', 'Download'))
            )
          )
        ),
      server =
        function(input, output, session) {
    
          randomVals <- eventReactive(input$go, {
            runif(input$n)
          })
    
          plotInput <- function(){hist(randomVals())}
    
          output$plot <- renderPlot({
            hist(randomVals())
          })
    
          output$downloadPlot <- downloadHandler(
            filename = "Shinyplot.png",
            content = function(file) {
              png(file)
              plotInput()
              dev.off()
            }) 
    
        }
    )
    

    0 讨论(0)
  • 2020-12-28 22:40

    Using native Shiny functionality

    library(shiny)
    
    ui <- fluidPage(
      actionButton("go", "Go"),
      numericInput("n", "n", 50)
    )
    
    server <- function(input, output) {
      randomVals <- eventReactive(input$go, {
        runif(input$n)
      })
      
      output$plot <- renderPlot({
        hist(randomVals())
      })
      
      observeEvent(input$go, {
        showModal(modalDialog(
          plotOutput("plot"),
          footer = NULL,
          easyClose = TRUE
        ))
      })
    }
    
    shinyApp(ui, server)
    

    0 讨论(0)
提交回复
热议问题