Shiny: plot results in popup window

前端 未结 3 944
[愿得一人]
[愿得一人] 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: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()
            }) 
    
        }
    )
    

提交回复
热议问题