在 shinyBS 包中使用 bsModal 和 plotly R plotly_click 在弹出窗口中生成新图 [英] Use bsModal in the shinyBS package with plotly R plotly_click to generate new plot in pop up
问题描述
这是我使用 plotly_click
事件选择性地显示另一个图的基本闪亮应用程序的代码.我希望该侧箱图在模式弹出窗口中呈现,而不是在页面内的一侧.
Here is my code for a basic shiny app using plotly_click
event to optionally show another plot. I would like that side box plot to render in a modal pop up instead of on the side within the page.
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)
我能够关注这个问题(Shiny: 在弹出窗口中绘制结果)和响应,并尝试将其与 plotly_click
的 trigger
一起使用,但未成功.知道如何通过巧妙的悬停点击事件来完成同样的事情吗?
I was able to follow this question (Shiny: plot results in popup window) and response, and tried to use it with the trigger
of plotly_click
without success. Any idea how to pull the same thing off with a plotly hover click event?
更新:我可以清楚地看到 plotly
绘图可以在 shinyBS
模态弹出窗口中呈现,如此代码所示.
UPDATE: I can clearly see that a plotly
plot can be rendered in a shinyBS
modal pop up window as demonstrated by this code.
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)
我希望 plotly_click
或 plotly_hover
作为触发器,而不是 actionButton
作为触发器(在原始示例中).
Instead of an actionButton
as the trigger, I want the plotly_click
or plotly_hover
as there trigger (in the original example).
推荐答案
您可以使用 toggleModal
,只需将其添加到您的服务器:
You can use toggleModal
, just add this to your server:
observeEvent(event_data("plotly_click", source = "scatter"), {
toggleModal(session, "boxPopUp", toggle = "toggle")
})
并将方框 Plot 放在 bsModal 中(标题和触发器为空):
and put the box Plot in an bsModal (Title and trigger is empty):
ui <- fluidPage(
column(6, plotlyOutput('scatter')),
bsModal('boxPopUp', '', '', plotlyOutput('box'))
)
更新:具有闪亮的内置模态功能(自闪亮 0.14 起),只需要添加服务器:
UPDATE: with shiny-build-in Modal functionality (since Shiny 0.14), only the server addition is needed:
observeEvent(event_data("plotly_click", source = "scatter"), {
showModal(modalDialog(
renderPlotly({
plot_ly(df2, x = ~x, y = ~y, type = 'box')
})
))
})
这篇关于在 shinyBS 包中使用 bsModal 和 plotly R plotly_click 在弹出窗口中生成新图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!