在带有 plotly R plotly_click 的 shinyBS 包中使用 bsModal 在弹出窗口中生成新图
Use bsModal in the shinyBS package with plotly R plotly_click to generate new plot in pop up
这是我使用 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)
我能够理解这个问题 () 和响应,并尝试将它与 plotly_click
的 trigger
一起使用,但没有成功。知道如何通过巧妙的悬停点击事件实现同样的效果吗?
更新:我可以清楚地看到 plotly
绘图可以在 shinyBS
模式弹出 window 中呈现,如此代码所示。
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)
而不是 actionButton
作为触发器,我想要 plotly_click
或 plotly_hover
作为触发器(在原始示例中)。
使用CSS
您可以使用 HTML builder
包含绘图并使用样式表添加动态效果。
ui <- fluidPage(
includeCSS(path_to_css_file),
div( class='mainchart',
column(6, plotlyOutput('scatter')),
div( class='popup',
column(6, plotlyOutput('box'))
)
)
)
CSS
div.popup {
display : none;
position: absolute;
}
div.mainchart : focus > div.popup {
display : block;
}
div.mainchart {
position: relative;
}
使用Javascript
您可以使用 plotly embeded-API 来设置侧框的可见性。
闪亮BS
既然你想坚持使用shinyBS,那么你可以使用bsPopover
这个函数,有一个小技巧。我假设您已经知道如何使用类似于下面示例的 bsModel
。
将以下参数传递给 fluidPage
bsTooltip(id, title, placement = "bottom", trigger = "click", content=column(6, plotlyOutput('box')) )
这将创建带有 Popover
包装器的绘图。我还没有测试它。万一出错,也可以试试
options = list()
options$content = column(6, plotlyOutput('box'))
options$html = T # otherwise the conent will be converted to text
bsTooltip(id, title, placement = "bottom", trigger = "click", options=options )
请访问 bootstrap
的这个 source file of shinyBS and the popover(options) 功能了解更多信息。
您可以使用 toggleModal
,只需将此添加到您的服务器:
observeEvent(event_data("plotly_click", source = "scatter"), {
toggleModal(session, "boxPopUp", toggle = "toggle")
})
并将箱线图放入 bsModal(标题和触发器为空):
ui <- fluidPage(
column(6, plotlyOutput('scatter')),
bsModal('boxPopUp', '', '', plotlyOutput('box'))
)
更新: 具有 shiny-build-in 模态功能(自 Shiny 0.14 起),只需要添加服务器:
observeEvent(event_data("plotly_click", source = "scatter"), {
showModal(modalDialog(
renderPlotly({
plot_ly(df2, x = ~x, y = ~y, type = 'box')
})
))
})
这是我使用 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)
我能够理解这个问题 (plotly_click
的 trigger
一起使用,但没有成功。知道如何通过巧妙的悬停点击事件实现同样的效果吗?
更新:我可以清楚地看到 plotly
绘图可以在 shinyBS
模式弹出 window 中呈现,如此代码所示。
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)
而不是 actionButton
作为触发器,我想要 plotly_click
或 plotly_hover
作为触发器(在原始示例中)。
使用CSS
您可以使用 HTML builder
包含绘图并使用样式表添加动态效果。
ui <- fluidPage(
includeCSS(path_to_css_file),
div( class='mainchart',
column(6, plotlyOutput('scatter')),
div( class='popup',
column(6, plotlyOutput('box'))
)
)
)
CSS
div.popup {
display : none;
position: absolute;
}
div.mainchart : focus > div.popup {
display : block;
}
div.mainchart {
position: relative;
}
使用Javascript
您可以使用 plotly embeded-API 来设置侧框的可见性。
闪亮BS
既然你想坚持使用shinyBS,那么你可以使用bsPopover
这个函数,有一个小技巧。我假设您已经知道如何使用类似于下面示例的 bsModel
。
将以下参数传递给 fluidPage
bsTooltip(id, title, placement = "bottom", trigger = "click", content=column(6, plotlyOutput('box')) )
这将创建带有 Popover
包装器的绘图。我还没有测试它。万一出错,也可以试试
options = list()
options$content = column(6, plotlyOutput('box'))
options$html = T # otherwise the conent will be converted to text
bsTooltip(id, title, placement = "bottom", trigger = "click", options=options )
请访问 bootstrap
的这个 source file of shinyBS and the popover(options) 功能了解更多信息。
您可以使用 toggleModal
,只需将此添加到您的服务器:
observeEvent(event_data("plotly_click", source = "scatter"), {
toggleModal(session, "boxPopUp", toggle = "toggle")
})
并将箱线图放入 bsModal(标题和触发器为空):
ui <- fluidPage(
column(6, plotlyOutput('scatter')),
bsModal('boxPopUp', '', '', plotlyOutput('box'))
)
更新: 具有 shiny-build-in 模态功能(自 Shiny 0.14 起),只需要添加服务器:
observeEvent(event_data("plotly_click", source = "scatter"), {
showModal(modalDialog(
renderPlotly({
plot_ly(df2, x = ~x, y = ~y, type = 'box')
})
))
})