I have a complex shiny app that plots coloured lines on a leaflet map. I am having problems with reactivity, with the lines being updated twice in some instances because they are dependent on two reactive chains. I created a toy example below. The user changes an input (here x/y), which triggers a replot. However the input also changes another option (colour) which then triggers another replot. The replots are slow and look ugly. I suspects there's some use of observeEvent that solves it but I've spent several days and can't get it. Any help appreciated.
library(shiny)
library(leaflet)
dlat <- 1 / 111000 * 100 # degrees per metre
n <- 10000 # number of circles
mylng <- 175.322 + (runif(n) * 2 - 1) * dlat * 6
mylat <- -37.789 + (runif(n) * 2 - 1) * dlat * 1.5
ui <- fluidRow(
tags$h2("Avoid double reaction in leaflet"),
leafletOutput("map"),
selectInput("which", label = "Axis to collapse", choices = c("x","y")),
selectInput("colour", label = "Choose colour", choices = c("red","blue"))
)
server <- function(input, output, session) {
observeEvent(input$which, {
# colour choices are dependent on input$which
if (input$which == "x"){
updateSelectInput(session, "colour", label = "Choose colour", choices = c("red","blue"))
} else {
updateSelectInput(session, "colour", label = "Choose colour", choices = c("red","yellow"))
}
})
makepoints <- reactive({
# data is dependent on input$which
if (input$which == "x"){
x <- mylng
y <- -37.789 + (mylat - -37.789) / 10
} else {
x <- 175.322 + (mylng - 175.322) / 10
y <- mylat
}
list(x = x, y = y)
})
output$map <- renderLeaflet({
cat("renderLeaflet\n")
leaflet() %>%
addTiles() %>%
setView(175.322, -37.789, zoom = 17)
})
observe({
# plot is dependent on data and also on colour, so triggers twice
cat(paste("replot", input$which, input$colour, "\n"))
leafletProxy("map") %>%
clearShapes() %>%
addCircles(
lng = makepoints()$x,
lat = makepoints()$y,
radius = 1,
color = input$colour)
})
}
shinyApp(ui = ui, server = server)