问题:假设一个巧妙的子图,其中包含多个情节。除了plotly_click和pointNumber之外,有没有一种方法可以通过curveNumber事件识别子图号?例如(例如,下面的代码),p1得到指定的数字0,p2得到分配的数字1,p3得到分配的编号2,我可以使用与plotly_click事件类似的命令获得这些数字?
期望输出:
curveNumber pointNumber x y plotNumber
4 1 Top 15 2代码示例
library(shiny)
library(plotly)
library(data.table)
dt <- data.table(
quarter = c("2020 Q1", "2020 Q1", "2019 Q4", "2019 Q4", "2019 Q3", "2019 Q3"),
type = c("Hop", "Top", "Hop", "Top", "Hop", "Top"),
count2 = c(3, 4, 5, 6, 0, 1),
count = c(10, 20, 30, 40, 0, 15)
)
dt[, type := as.factor(type)]
ui <- fluidPage(
mainPanel(
plotlyOutput("plot")
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
output$plot <- renderPlotly({
p1 <- plot_ly(dt[quarter == "2020 Q1"],
x = ~type,
y = ~count,
source = "plot_y") %>%
add_bars(y = ~count) %>%
add_bars(y = ~count2) %>%
layout(barmode = "stack")
p2 <- plot_ly(dt[quarter == "2019 Q4"],
x = ~type,
# y = ~count,
source = "plot_y")%>%
add_bars(y = ~count) %>%
add_bars(y = ~count2)%>%
layout(barmode = "stack")
p3 <- plot_ly(dt[quarter == "2019 Q3"],
x = ~type,
y = ~count,
source = "plot_y")%>%
add_bars(y = ~count) %>%
add_bars(y = ~count2)%>%
layout(barmode = "stack")
list_p <- list(p1, p2, p3)
subplot(list_p, shareY = TRUE)
})
observe({
click <- event_data("plotly_click", source = "plot_y")
req(click)
print(click)
})
}
# Run the application
shinyApp(ui = ui, server = server)发布于 2020-03-23 12:33:47
我认为您不能将类似plotNumber的内容添加到event_data中,但是可以使用customdata或key属性向event_data传递额外的信息。
下面是使用customdata更新的服务器功能
# Define server logic required to draw a histogram
server <- function(input, output) {
output$plot <- renderPlotly({
p1 <- plot_ly(dt[quarter == "2020 Q1"],
x = ~type,
y = ~count,
customdata='p1',
source = "plot_y") %>%
add_bars(y = ~count) %>%
add_bars(y = ~count2) %>%
layout(barmode = "stack")
p2 <- plot_ly(dt[quarter == "2019 Q4"],
x = ~type,
customdata='p2',
# y = ~count,
source = "plot_y")%>%
add_bars(y = ~count) %>%
add_bars(y = ~count2)%>%
layout(barmode = "stack")
p3 <- plot_ly(dt[quarter == "2019 Q3"],
x = ~type,
y = ~count,
customdata='p3',
source = "plot_y")%>%
add_bars(y = ~count) %>%
add_bars(y = ~count2)%>%
layout(barmode = "stack")
list_p <- list(p1, p2, p3)
subplot(list_p, shareY = TRUE)
})
observe({
click <- event_data("plotly_click", source = "plot_y")
req(click)
print(click)
})
}输出:
curveNumber pointNumber x y customdata
1 4 1 Top 15 p3https://stackoverflow.com/questions/60810540
复制相似问题