## pre-process the data
x <- split(iris, iris$Species)
library(shiny)
library(plotly, warn.conflicts = FALSE)
# this behavior is independent of the table implementation
# I observe the same issue with DT
library(reactable, warn.conflicts = FALSE)
ui <- fluidPage(
selectInput("species", "Species", names(x), selected = names(x)[[1]]),
plotlyOutput("p1"),
reactableOutput("d1"),
verbatimTextOutput('t1')
)
server <- function(input, output, session) {
rvalues <- reactiveValues()
rvalues$key <- reactive({
x[[input$species]]
})
prev_selected <- reactive(getReactableState("d1", "selected"))
output$p1 <- renderPlotly({
s <- prev_selected()
if(!is.null(s)){
col <- rep('rgba(0,0,0,.10)', nrow(rvalues$key()))
col[s] <- 'rgba(255,0,0,1)'
} else {
col <- 'rgb(0,0,0)'
}
p <- rvalues$key() %>%
plot_ly(., showlegend = FALSE) %>%
add_markers(
x = ~Sepal.Length,
y = ~Sepal.Width,
type = "scatter",
hoverinfo = "text",
text = ~ paste0(
"<b>", Species, "</b>",
"<br><i>Sepal.Length</i>= ", Sepal.Length,
"<br><i>Sepal.Width</i> = ", Sepal.Width,
"<br><i>Petal.Length</i> = ", Petal.Length)
)
p
})
output$d1 <- renderReactable({
rvalues$key() %>%
reactable(
.,
selection = "single",
onClick = "select"
)
})
output$t1 <- renderPrint({
s <- prev_selected()
if(!is.null(s)){
cat('These rows were selected:\n\n')
cat(s, sep = ', ')
}
})
}
shinyApp(ui, server)
The issue occurs once changing
speciesinput. It leads to a breakdown on crosstalk javascript that is hard to track. It seems crosstalk JS is not cleaned upon option selection, which results in the problem.