闪亮:在弹出窗口中绘制结果

12
我正在尝试使用shiny构建一个Web应用程序,我希望在弹出窗口中显示R函数的结果图,而不是在主面板中显示。例如,对于下面的示例(来自http://shiny.rstudio.com/articles/action-buttons.html),单击“Go”按钮将在弹出窗口中显示相同的图形。
我尝试添加一些JavaScript代码,但还没有成功...有人可以帮忙吗?
提前感谢您!
library(shiny)
ui <- fluidPage(
    actionButton("go", "Go"),
    numericInput("n", "n", 50),
 plotOutput("plot")
)
server <- function(input, output) {
  randomVals <- eventReactive(input$go, {
  runif(input$n)
  })
  output$plot <- renderPlot({
   hist(randomVals())
 })
}
shinyApp(ui, server)
3个回答

16

看看 shinyBS 包,它提供了 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()
        }) 

    }
)

输入图像描述


非常感谢您的回答,它完美地解决了我的问题!如果我可以再问一件事,您如何添加一个下载图表的按钮?我尝试直接在HTML中添加一行代码,像这样:<div class="modal-footer"> <a id="downloadData" class="shiny-download-link" href="" target="_blank" >Download</a> <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> </div>但是它没有显示出来... - Erica Fary
1
我编辑了我的答案,加入了下载按钮,祝你编程愉快! - Pork Chop
再次感谢!我尝试了这种方法,但它没有起作用。我用你的代码再试一次,它完美地工作了...我应该在上面的代码中犯了一个错误...现在解决了!谢谢。 - Erica Fary
太棒了,这可以做到,答案很好。有没有想法如何使其与 plotly_click 事件一起工作?似乎无法使其正常工作。 - Gopala
@Gopala,也许你可以提出一个问题,我可以根据你的例子来研究一下。请明确你想要实现什么,我可以写一点Javascript来完成它。 - Pork Chop
https://dev59.com/15bfa4cB1Zd3GeqPu4QR - Gopala

8

使用原生Shiny功能

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


3
您可以使用条件面板来显示/隐藏包含您的绘图的绝对面板,将条件设置为一些由附加到按钮的函数切换的js变量。

例如:

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

然后在 JavaScript 中切换 popupActive 的值以显示/隐藏。
  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>'),

非常感谢您的回答。然而,我是一个shiny/javascript等方面的新手,我尝试添加HTML('<head><script> function myFunction(){popupActive=1;} </script></head>'), HTML('<button id="go" type="button" class="btn btn-default action-button" onclick="myFunction()">Go</button>')但它仍然不起作用。当弹出窗口关闭时,popupActive的值应该切换为0...请原谅我的无知,但我可以问您一个更明确(即使用js)的答案吗? - Erica Fary
1
Pork Chops的答案是正确的选择;shinyBS看起来是一个非常有用的包。为了完整起见,我已经编辑了我的答案,包括一些可以工作的js示例。你唯一需要修改的是创建popActive全局变量,然后在按钮点击时切换它... popupActive=!popupActive - user5219763

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接