【问题标题】:Shiny: plot results in popup windowShiny:在弹出窗口中绘制结果
【发布时间】:2015-11-25 20:02:13
【问题描述】:

我正在尝试构建一个闪亮的网络应用程序,我想在弹出窗口中显示 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)

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    查看提供modal 弹出窗口的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 中添加一行,例如:&lt;div class="modal-footer"&gt; &lt;a id="downloadData" class="shiny-download-link" href="" target="_blank" &gt;Download&lt;/a&gt; &lt;button type="button" class="btn btn-default" data-dismiss="modal"&gt;Close&lt;/button&gt; &lt;/div&gt; 但它不显示...
    • 我编辑了我的答案,包括下载按钮,快乐编码!
    • 再次感谢您!我首先以这种方式尝试过,但没有奏效。我用你的代码再试一次,它工作得很好......我应该在上面的代码中有一个错误......现在解决了!谢谢。
    • 太棒了,这可以做到,而且答案很好。知道如何让它也适用于plotly_click 事件吗?似乎无法让它工作。
    • @Gopala,也许你可以提出一个问题,我可以用你的例子来研究它。请说清楚你想要达到什么,我也可以写一点Javascript来完成它
    【解决方案2】:

    使用原生 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)
    

    【讨论】:

      【解决方案3】:

      您可以使用conditional panel 来显示/隐藏包含您的情节的absolute panel,将条件设置为某个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#)
                              )
                              )
      

      然后在js中切换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>'),
      

      【讨论】:

      • 非常感谢您的回答。但是,我是闪亮/javascript/等方面的新手。我尝试添加HTML('&lt;head&gt;&lt;script&gt; function myFunction(){popupActive=1;} &lt;/script&gt;&lt;/head&gt;'), HTML('&lt;button id="go" type="button" class="btn btn-default action-button" onclick="myFunction()"&gt;Go&lt;/button&gt;') ,但它仍然不起作用。当弹出窗口关闭时,popupActive 的值应该切换为 0...原谅我的无知,但我可以问你一个更明确(即用 js)的答案吗?
      • 猪排答案是要走的路; shinyBS 看起来是一个非常有用的软件包。为了完整起见,尽管我已经编辑了我的答案,以包括一些可行的 js 示例。您需要的唯一修改是创建 popActive 全局变量,然后在单击按钮时切换它...popupActive=!popupActive
      猜你喜欢
      • 2019-09-05
      • 2016-02-08
      • 1970-01-01
      • 1970-01-01
      • 2020-09-29
      • 1970-01-01
      • 1970-01-01
      • 2021-06-07
      • 1970-01-01
      相关资源
      最近更新 更多