I would like to display data in two plots (plotly
) and want to be able to display the selected points of one plot in the other plot by using crosstalk. Sadly nothing I tried is working. The solution with defining the shared data outside the server function is not an option, since the data in my app is coming from other reactives and inputs. Below is a reprex.
library(shiny)
library(plotly)
ui <- fluidPage(
sliderInput("rows", label = "# Rows", min = 50, max = 150, value = 100),
plotlyOutput("scatter1"),
plotlyOutput("scatter2")
)
server <- function(input, output, session) {
iris_new <- reactive({
iris[1:as.numeric(input$rows),]
})
sd <- SharedData$new(iris_new)
output$scatter1 <- renderPlotly({
plot_ly(
sd,
x = ~Sepal.Length,
y = ~Sepal.Width,
color = ~Species,
type = "scatter",
mode = "markers"
)
})
output$scatter2 <- renderPlotly({
plot_ly(
sd,
x = ~Petal.Length,
y = ~Petal.Width,
color = ~Species,
type = "scatter",
mode = "markers"
)
})
}
shinyApp(ui, server)
I also tried making the SharedData$new(iris_new)
a reactive expression like
iris_new <- reactive({
SharedData$new(iris[1:as.numeric(input$rows),])
})
and using iris_new()
in plot_ly(...)
but it isn't working aswell. I also tried sd$data(withSelection = T)
with no luck. Strangely when I select a single point, it works (though I cannot deselect anymore). But when I try to select multiple points (which I actually want), the other plot doesn't react.
I need this to work with plotly (and not d3scatter, scatterD3, etc.)!
try this
library(shiny)
library(plotly)
ui <- fluidPage(
sliderInput("rows", label = "# Rows", min = 50, max = 150, value = 100),
plotlyOutput("scatter1"),
plotlyOutput("scatter2")
)
server <- function(input, output, session) {
iris_new <- reactive({
highlight_key(iris[1:as.numeric(input$rows),])
})
output$scatter1 <- renderPlotly({
plot_ly(
iris_new(),
x = ~Sepal.Length,
y = ~Sepal.Width,
color = ~Species,
type = "scatter",
mode = "markers"
) %>%
highlight("plotly_selected")
})
output$scatter2 <- renderPlotly({
plot_ly(
iris_new(),
x = ~Petal.Length,
y = ~Petal.Width,
color = ~Species,
type = "scatter",
mode = "markers"
) %>%
highlight("plotly_selected")
})
}
shinyApp(ui, server)
Hold your mouse to select an area, double click on the plot to deselect.