我正在设计一个包含plotly散点图的Shiny应用程序.我希望用户能够使用该event_data功能单击图表来记录事件,但是能够通过单击来清除该事件actionButton.下面是一些示例代码:
library(shiny)
library(plotly)
ui <- fluidPage(
actionButton("clearEvent", label = "clear event"),
verbatimTextOutput("plotVal"),
plotlyOutput('plot1')
)
server <- function(input, output, session) {
output$plot1 <- renderPlotly({
d <- diamonds[sample(nrow(diamonds), 1000), ]
plot_ly(d, x = ~carat, y = ~price, color = ~carat,
size = ~carat, text = ~paste("Clarity: ", clarity))
})
output$plotVal <- renderPrint({
e <- event_data("plotly_click")
if (is.null(e)) {
NULL
} else {
e
}
})
observeEvent(input[["clearEvent"]], {
e <- NULL
})
}
shinyApp(ui = ui, server …Run Code Online (Sandbox Code Playgroud) event_data("plotly_click")用户单击散点图中的标记后,我正在使用Plotly的东西(打开模式)。之后(例如,关闭模态),event_data("plotly_click")当然不会改变,因此单击相同的标记不会再次触发相同的动作。
最小示例:
library(plotly)
ui <- fluidPage(
plotlyOutput("plot")
)
server <- function(input, output, session) {
output$plot <- renderPlotly({
mtcars %>% plot_ly(x=~disp, y=~cyl)
})
# Do stuff after clicking on a marker in the plot
observeEvent(event_data("plotly_click"), {
print("do some stuff now") # this is not executed after second click on same marker
})
}
shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)
我已经尝试了使用Shinyjs的变通办法onclick,但无济于事(它在绘图的空白区域工作良好,但在单击标记时无效):
shinyjs::onclick(id="plot", print("clicked"))
Run Code Online (Sandbox Code Playgroud)
我也尝试过使用反应性值来存储最后的点击,然后立即将其重置(例如,通过event_data("plotly_hover")),但是所有尝试均会失败,因为它event_data("plotly_click")仍然保持其旧值。
有人可以帮忙吗?