[英]How to display many points from plotly_click in R Shiny?
I have a plotly plot in R Shiny.我在 R Shiny 中有一个情节。 I want to be able to click many points and have them displayed in a table.我希望能够单击许多点并将它们显示在表格中。 The plot is working great and I can get 1 plotly_click (via event_data()) to show in a table.该图运行良好,我可以获得 1 个 plotly_click(通过 event_data())以显示在表格中。 How can a grow a vector of many event_data points.如何增长许多 event_data 点的向量。 Here is some sample code.这是一些示例代码。 I was trying to save the event in d_save.我试图将事件保存在 d_save 中。 Thanks.谢谢。
library(shiny)
library(plotly)
data1 <- data.frame(cbind(seq(1,1000,1),seq(1,1000,1)*5))
colnames(data1) <- c('index','data')
data_points <- data.frame(cbind(seq(1,1000,5),seq(1,1000,5)*5))
colnames(data_points) <- c('index','data')
ui <- fluidPage(
plotlyOutput("plot1"),
tableOutput("dataTable")
)
d_save <- vector()
server <- function(input, output, session) {
# make plotly plot
output$plot1 <- renderPlotly({
p <- plot_ly(data1, x = data1$index, y = data1$data,mode = "lines")
add_trace(p, x = data_points$index, y = data_points$data, mode = "markers")
})
# show table of stances
output$dataTable <- renderTable({
d <- event_data("plotly_click")
d_save <- c(d_save,d$pointNumber[2]+1)
data.frame(d_save)
})
}
shinyApp(ui, server)
There is nothing seriously wrong with this and it was weird that it never got answered.这没有什么严重的错误,奇怪的是它从未得到回答。 It is not a bad example of pure plotly (without using ggplot).这不是纯 plotly 的一个坏例子(不使用 ggplot)。
I fixed it by:我通过以下方式修复它:
d_save <- c(...)
assignment to a d_save <<- c(...)
(using a reactiveValues
here would be cleaner).将d_save <- c(...)
赋值更改为d_save <<- c(...)
(此处使用reactiveValues
会更清晰)。type=scatter
default) - eliminating the warning:将 plotly 调用更改为管道,这似乎允许保留某些设置(例如type=scatter
默认值) - 消除警告:No trace type specified: Based on info supplied, a 'scatter' trace seems appropriate.未指定跟踪类型:根据提供的信息,“分散”跟踪似乎是合适的。
d_save
assignment.修复了d_save
分配中的“一对一”索引错误。layout(...)
to give it a title (this is useful for a lot of things).添加了一个layout(...)
给它一个标题(这对很多事情都很有用)。The resulting code:结果代码:
library(shiny)
library(plotly)
data1 <- data.frame(cbind(seq(1,1000,1),seq(1,1000,1)*5))
colnames(data1) <- c('index','data')
data_points <- data.frame(cbind(seq(1,1000,5),seq(1,1000,5)*5))
colnames(data_points) <- c('index','data')
ui <- fluidPage(
plotlyOutput("plot1"),
tableOutput("dataTable")
)
d_save <- vector()
server <- function(input, output, session) {
# make plotly plot
output$plot1 <- renderPlotly({
plot_ly(data1, x=data1$index, y=data1$data,mode = "lines") %>%
add_trace(x = data_points$index, y=data_points$data, mode = "markers") %>%
layout(title="Plotly_click Test")
})
# show table of point markers clicked on by number
output$dataTable <- renderTable({
d <- event_data("plotly_click")
d_save <<- c(d_save,d$pointNumber[1]+1)
data.frame(d_save)
})
}
shinyApp(ui, server)
The image:图像:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.