'How can I save previous reactive values in R shiny?

Hi and thank you for your answer,

How can I save previous reactive values in R shiny? I want to extract the coordiantes of the last two marker locations (leaflet). I tried it with the isolate function (without success). In it's current form my code can store the currend reactive variable but it can't store the last one.

Here is my code:

# Define UI 
ui <- fluidPage(
  leafletOutput("mymap",height=800)
)

# Define server logic 
server <- function(input, output) {
  
  # Leaflet
  output$mymap <- renderLeaflet(
    leaflet() %>%
      addTiles() %>%
      setView(lng = 8.53918, lat = 47.36864, zoom = 11) %>%
      addDrawToolbar(
        targetGroup='draw',
        polylineOptions = F,
        polygonOptions = F,
        rectangleOptions = F,
        circleOptions = F,
        circleMarkerOptions = F,
        editOptions = editToolbarOptions(selectedPathOptions = selectedPathOptions()))  %>%
      addLayersControl(overlayGroups = c('draw'), options =
                         layersControlOptions(collapsed=FALSE))
  )
  
  reactive({
    
    PosData <- data.frame(PosX = c(NA,NA),
                          PosY = c(NA,NA),
                          PosDegree = c(NA,NA))
    
  })
 
  
  # Extract coordinates
  observeEvent(input$mymap_draw_new_feature,{
    if(input$mymap_draw_new_feature$geometry$type == "Point"){

      PosData$PosX[1] <- input$mymap_draw_new_feature$geometry$coordinates[[1]]
      PosData$PosY[1] <- input$mymap_draw_new_feature$geometry$coordinates[[2]]
      
      PosData$PosX[2] <- PosData$PosX[1]
      PosData$PosY[2] <- PosData$PosY[1]
      
      print(PosData)

    }else{f <- 2}

  })
}

# Run the application
shinyApp(ui = ui, server = server)```



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source