Shiny: plot results in popup window

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

    }
)

enter image description here


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)

modal demo


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>'),

Tags:

R

Shiny