如何在R Shiny中显示plotly_click中的许多点? [英] How to display many points from plotly_click in R Shiny?
问题描述
我在R Shiny中有一个密谋.我希望能够单击许多点并将它们显示在表格中.该绘图工作得很好,我可以得到1 plotly_click(通过event_data())显示在表中.如何增长许多event_data点的向量.这是一些示例代码.我试图将事件保存在d_save中.谢谢.
I have a plotly plot in 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. How can a grow a vector of many event_data points. Here is some sample code. I was trying to save the event in 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)
推荐答案
这没有什么大不了的,而且很奇怪,它没有得到回答.这不是纯plotly(不使用ggplot)的一个好例子.
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).
我通过以下方式解决了此问题:
I fixed it by:
- 将
d_save <- c(...)
分配更改为d_save <<- c(...)
(在这里使用reactiveValues
会更干净). - 将plotly调用更改为管道,这似乎可以保留某些设置(例如
type=scatter
的默认设置)-消除了警告:
- changing the
d_save <- c(...)
assignment to ad_save <<- c(...)
(using areactiveValues
here would be cleaner). - changing the plotly call to be a pipe, which seemingly allows some settings to carry over (like the
type=scatter
default) - eliminating the warning:
未指定跟踪类型:根据提供的信息,分散"跟踪 似乎合适.
No trace type specified: Based on info supplied, a 'scatter' trace seems appropriate.
- 修复了
d_save
分配中的不一"索引错误. - 添加了
layout(...)
以为其命名(这在很多情况下都很有用). - fixed an "off-by-one" indexing error in the
d_save
assignment. - added a
layout(...)
to give it a title (this is useful for a lot of things).
结果代码:
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)
图片:
这篇关于如何在R Shiny中显示plotly_click中的许多点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!