I'd like to select (via box or lasso) specific samples and variables from a plotly graph with multiple lines.
Each line of the attached graph is a sample and I'd like to allow the user to dynamically select multiple samples (as well as the selected range of variables) and store them in a different dataframe to be used for future evaluations.
I tried to use the following code but, despite it works for traditional plot like a scatter plot, I can't deal with it when using plotly with multiple lines.
library(shiny)
library(plotly)
ui <- fluidPage(
plotlyOutput("p"),
tableOutput("table")
)
server <- function(input, output, session) {
data <- reactiveVal()
observeEvent(event_data("plotly_selected"), {
dat <- event_data("plotly_selected")$customdata
data_old_new <- c(data(), dat)
data(unique(data_old_new))
})
observeEvent(event_data("plotly_doubleclick"), {
data(NULL)
})
output$p <- renderPlotly({
cols <- ifelse(ee$samples %in% data(), "red","black")
meltR = reshape2::melt(bb)
aa <- as.factor(as.character(row.names(bb)))
samples = rep(aa,ncol(bb))
meltR$samples = samples
ee <<- meltR
p <- plot_ly(data = ee)
L = nlevels(as.factor(as.character(ee$samples)))
for(k in 1:length(L)) {
p <- add_trace(p, y=ee$value, x=ee$variable,
color = as.factor(as.character(ee$samples)),
type="scatter", mode="lines+markers")%>%
layout(dragmode="lasso")
}
p
})
output$table <- renderTable({
aa<<-filter(ee, ee$samples %in% data())
filter(ee, ee$samples %in% data())
})
}
shinyApp(ui, server)
The dataset I deal with is attached here, too:
ee<-data.frame(
value = c(0.02,0.09,0.05,0.01,0.02,0.1,0.05,
0.01,0.02,0.09,0.04,0.01,0.03,0.17,0.07,0.02,0.03,0.12,
0.05,0.02,0.02,0.07,0.04,0.02,0.02,0.04,0.03,0.01,
0.01,0.04,0.03,0.01,0.01,0.04,0.03,0.01,0.01,0.04,0.03,
0.01,0.02,0.05,0.03,0.01,0.02,0.05,0.03,0.01,0.02,
0.05,0.03,0.01,0.02,0.06,0.03,0.01,0.02,0.06,0.03,0.01,
0.02,0.07,0.03,0.01,0.02,0.07,0.03,0.01,0.02,0.08,
0.04,0.01,0.02,0.09,0.04,0.01,0.02,0.08,0.04,0.01,0.02,
0.08,0.03,0.01),
variable = as.factor(c("2838.34","2838.34",
"2838.34","2838.34","2862.93","2862.93","2862.93",
"2862.93","2887.52","2887.52","2887.52",
"2887.52","2911.63","2911.63","2911.63","2911.63",
"2936.22","2936.22","2936.22","2936.22","2960.8",
"2960.8","2960.8","2960.8","2984.91","2984.91",
"2984.91","2984.91","3009.5","3009.5","3009.5",
"3009.5","3034.09","3034.09","3034.09","3034.09",
"3058.68","3058.68","3058.68","3058.68","3082.78",
"3082.78","3082.78","3082.78","3107.37","3107.37",
"3107.37","3107.37","3131.96","3131.96","3131.96",
"3131.96","3156.07","3156.07","3156.07","3156.07",
"3180.66","3180.66","3180.66","3180.66",
"3205.25","3205.25","3205.25","3205.25","3229.84",
"3229.84","3229.84","3229.84","3253.94","3253.94",
"3253.94","3253.94","3278.53","3278.53","3278.53",
"3278.53","3303.12","3303.12","3303.12","3303.12",
"3327.23","3327.23","3327.23","3327.23")),
samples = as.factor(c("s1","s2","s3","s4",
"s1","s2","s3","s4","s1","s2","s3","s4","s1",
"s2","s3","s4","s1","s2","s3","s4","s1",
"s2","s3","s4","s1","s2","s3","s4","s1","s2",
"s3","s4","s1","s2","s3","s4","s1","s2","s3",
"s4","s1","s2","s3","s4","s1","s2","s3","s4",
"s1","s2","s3","s4","s1","s2","s3","s4","s1",
"s2","s3","s4","s1","s2","s3","s4","s1","s2",
"s3","s4","s1","s2","s3","s4","s1","s2",
"s3","s4","s1","s2","s3","s4","s1","s2","s3",
"s4"))
)
An example of what I'd like to achieve (without the missing table I can't obtain) is reported here , too:
I'd be very grateful if anybody could attend to this matter.