Может быть, это может показать вам один из способов:
library(plotly)
library(shiny)
ui <- fluidPage(
plotlyOutput("plot"),
verbatimTextOutput("hover"),
verbatimTextOutput("click") )
df <- data.frame(
x = c(-1, -1, -1, -1, -1, -1, -1, -1, -1,
0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 1, 1, 1, 1, 1),
y = c(-1, -1, -1, 0, 0, 0, 1, 1, 1,
-1, -1, -1, 0, 0, 0, 1, 1, 1,
-1, -1, -1, 0, 0, 0, 1, 1, 1),
z = c(-1, 0, 1, -1, 0, 1, -1, 0, 1,
-1, 0, 1, -1, 0, 1, -1, 0, 1,
-1, 0, 1, -1, 0, 1, -1, 0, 1)
)
li <- data.frame(
x = c( 1, 0, -1, -1, -1),
y = c( 1, 1, 1, 1, 1),
z = c(-1, -1, -1, 0, 1)
)
server <- function(input, output, session) {
output$plot <- renderPlotly({
plot_ly() %>%
add_markers(data = df, x=~x, y=~y, z=~z, mode = 'markers', type = 'scatter3d') %>%
add_markers(data = li, x = ~x, y=~y, z=~z, type = 'scatter3d', mode = 'lines',
line = list(width = 6, color = "red"), inherit = F)
})
output$hover <- renderPrint({
d <- event_data("plotly_hover")
if (is.null(d)) "Hover events appear here (unhover to clear)" else d
})
output$click <- renderPrint({
d <- event_data("plotly_click")
if (is.null(d)) "Click events appear here (double-click to clear)" else d
})
}
shinyApp(ui, server)