使用flashBS包中的bsModal和plotly R plotly_click在弹出窗口中生成新的图

Gop*_*ala 2 r shiny plotly shinybs

这是我使用plotly_click事件的基本闪亮应用程序的代码,可选择显示另一个图.我希望那个侧框图在模式弹出窗口中呈现,而不是在页面内侧面.

library(shiny)
library(plotly)

df1 <- data.frame(x = 1:10, y = 1:10)
df2 <- data.frame(x = c(rep('a', 10), rep('b', 10)),
                  y = c(rnorm(10), rnorm(10, 3, 1)))

ui <- fluidPage(
  column(6, plotlyOutput('scatter')),
  column(6, plotlyOutput('box'))
)

server <- function(input, output) {
  output$scatter <- renderPlotly({
    plot_ly(df1, x = x, y = y, mode = 'markers', source = 'scatter')
  })

  output$box <- renderPlotly({
    eventdata <- event_data('plotly_click', source = 'scatter')
    validate(need(!is.null(eventdata),
                  'Hover over the scatter plot to populate this boxplot'))


    plot_ly(df2, x = x, y = y, type = 'box')
  })
}

shinyApp(ui = ui, server = server)
Run Code Online (Sandbox Code Playgroud)

我能够遵循这个问题(闪亮:剧情结果在弹出的窗口)和响应,并试图与使用它triggerplotly_click没有成功.任何想法如何通过一个阴谋悬停点击事件拉出相同的东西?

更新:我可以清楚地看到plotly可以在shinyBS模式弹出窗口中呈现绘图,如此代码所示.

df1 <- data.frame(x = 1:10, y = 1:10)
ui <- fluidPage(
  actionButton('go', 'Click Go'),
  bsModal('plotlyPlot', 'Here is a Plot', 'go', plotlyOutput('scatter1'))
)

server <- function(input, output) {
  output$scatter1 <- renderPlotly({
    plot_ly(df2, x = x, y = y, mode = 'markers', source = 'scatter1')
  })
}

shinyApp(ui = ui, server = server)
Run Code Online (Sandbox Code Playgroud)

而不是actionButton作为触发器,我想要plotly_clickplotly_hover作为触发器(在原始示例中).

sho*_*aco 5

您可以使用toggleModal,只需将其添加到您的服务器:

observeEvent(event_data("plotly_click", source = "scatter"), {
 toggleModal(session, "boxPopUp", toggle = "toggle")
})
Run Code Online (Sandbox Code Playgroud)

并将方框Plot放入bsModal(标题和触发器为空):

ui <- fluidPage(
  column(6, plotlyOutput('scatter')),
  bsModal('boxPopUp', '', '', plotlyOutput('box'))
)
Run Code Online (Sandbox Code Playgroud)

更新:具有闪亮的内置模态功能(自Shiny 0.14起),只需添加服务器:

 observeEvent(event_data("plotly_click", source = "scatter"), {
                showModal(modalDialog(
                        renderPlotly({
                                plot_ly(df2, x = ~x, y = ~y, type = 'box')
                        })
                ))
        })
Run Code Online (Sandbox Code Playgroud)