0

I'm trying to generate a dataframe that will later be used to plot a series of coordinates on a leaflet map. The idea is that the selected input from a dropdown menu creates two variables with the corresponding latitud and longitude. These variables are later inserted as columns to a dataframe and distances calculated. The distance between the selected lat lon are also added as a column to the same dataframe.

What I need is to be able to create a new dataframe by filtering the previously mentioned one. The problem that I am facing is that the first dataframe is now a function, given its reactive nature. I provide some code to ilustrate the situation:

server <- function(input, output,session) {

  estacion<-reactive(input$estacion)
  lonOBS <- reactive(estaciones$lon[estaciones$nombres == estacion()])
  latOBS <- reactive(estaciones$lat[estaciones$nombres == estacion()])

  globals<- reactiveValues(mydf=df_mapas)
  
  observe({
    globals$mydf$lonOBS <- lonOBS()
    globals$mydf$latOBS <- latOBS()
    globals$mydf$distOBS <- distHaversine(cbind(globals$mydf$lonOBS,
                                                globals$mydf$latOBS),
                                      cbind(globals$mydf$longitud,
                                            globals$mydf$latitud))
    })
  data_to_map1<-globals$mydf %>% filter(distOBS <= "100" &
                                          velocidad >= 50)

Error in .getReactiveEnvironment()$currentContext() : Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)

What I'm looking for is to have data_to_map1 as a dataframe to then use to plot the coordinates on a map just like it works outside shiny but updated according to the input.

Thanks in advance.

  • When do you expect the `data_to_map1<-globals$mydf %>% filter(distOBS <= "100" & velocidad >= 50)` line to run? You have that sitting outside of a reactive context. Presumably you want `data_to_map1` to be reactive as well. Try `data_to_map1<-reactive({globals$mydf %>% filter(distOBS <= "100" & velocidad >= 50)})` and make sure you use `data_to_map1()` to get the value. – MrFlick Sep 23 '20 at 05:06
  • Hi @MrFlick, yes this helped, thank you. – Cefe Alvelo Sep 24 '20 at 15:57
  • I am now having trouble with a label when I try to pass on to a leaflet map: `addCircles(data= data_to_map1(),lat = ~ latitud,lng = ~ longitud, label = ~ label)` it's giving me the following Error message: **Error in sum: invalid 'type' (list) of argument** I find it strange that lat and lng work fine if I comment label. – Cefe Alvelo Sep 24 '20 at 18:26

0 Answers0