在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?
更新:我可以清楚地看到,可以在shinyBS
模态弹出窗口中呈现plotly
图,如此代码所示.
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'))
)
更新:具有闪亮的内置模式功能(自Shiny 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屋!