如果绘图的source 参数设置为XXX(默认为A),那么您必须将输入plotly_selected-XXX 设置为NULL。这可以在shinyjs的帮助下完成:
library(shiny)
library(plotly)
library(shinyjs)
ui <- fluidPage(
useShinyjs(),
actionButton("reset", "Reset"),
plotlyOutput("plot")
)
server <- function(input, output){
output[["plot"]] <- renderPlotly({
df <- data.frame(
x = c(1,2,1),
y = c(1,2,1)
)
df %>%
plot_ly(
x = ~x,
y = ~y,
source = "A",
type = 'scatter',
mode = 'markers',
marker = list(size = 20),
showlegend = FALSE
)
})
observeEvent(input[["reset"]], {
runjs("Shiny.setInputValue('plotly_selected-A', null);")
})
observe({ # just to test
print(event_data("plotly_selected", source = "A"))
})
}
shinyApp(ui, server)