Search code examples
rshinyr-leaflet

Filter and action (run an algorithm)


I am trying to do an interactive map, but I am can not do the filter when the input variables are actioned. After this, I would like to active a action buttom for run an personal algorithm such that add some points on the map. First, this my ui.R

# My directory
setwd("C:\\testesh")

# Loads my choices
load("turno.rda")
load("dia_semana.rda")
load("Bairro_revisado.rda")

ui <- fluidPage(

  titlePanel("my app"),

  sidebarLayout(position = "right",
                sidebarPanel( 
                  selectInput("select_bairro", label = h3("Bairro"), choices = Bairro_revisado, selected = "Centro"),
                  checkboxGroupInput("select_diasemana", label = h3("Dia da semana"), choices = dia_semana, selected = "Sun" , inline   = FALSE),
                  checkboxGroupInput("select_turno", label = h3("Turno"), choices = turno, selected = "Manhã"),

                  # Parameter for my algorithm 
                  numericInput("select_numviaturas", label = h3("Enter the P parameter"), value = 1) ,

                  # run my algorithm
                  actionButton("otimizar", label = "OTIMIZAR")

                ), 
                mainPanel(
                  leafletOutput("mymap", width = "100%", height = 800)
                )
  )

)

Now, this my Server.R. There is no action when I try to plot the points after the filter data

# Persnals functions
source("C:\\dir_functions\\funcoes.R")


# Install the packages
install_load("ReporteRs", "plotrix",  "sqldf", "rgeos", "maptools" , "rgdal" , "bit64", "descr", "reshape", "survey", "Cairo","RColorBrewer" , 
             "dplyr" , "sm" , "weights" , "XML",  "ggplot2", "stringr" , "ggthemes" , "ggrepel" , "data.table" , "lazyeval" , "reshape2", "plotly" , "ineq", 
             "sqlsurvey", "MonetDB.R" , "rvest" ,"lubridate" , "qdap" , "igraph" , "leaflet" , "geosphere" , "purrr", "tidyr", "shiny")


# Load my dates
load("C:\\testesh\\resumo_crimes.rda")
load("C:\\testesh\\pontos_v.rda")

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

  # HERE I WOULD LIKE TO FILTER MY DATA 
  filteredData <- reactive({
    turno  <- input$select_turno #"Noite/Madrugada"
    bairro <- input$select_bairro #"Centro"
    dia    <- input$select_diasemana
    resumo_crimes<- resumo_crimes[resumo_crimes$turno %in% turno ,]
    resumo_crimes<- resumo_crimes[resumo_crimes$bai %in% bairro , ]
    resumo_crimes<- resumo_crimes[resumo_crimes$dia_semana %in% dia,]

    # wEIGHTS
    resumo_crimes <- as.data.frame(resumo_crimes %>% group_by( Latitude, Longitude) %>% summarise( w =  sum(pena_dias) ))
    resumo_crimes$w  <- log(resumo_crimes$w ) # , base = 1.09  : mais perto de 1 mais espalhado fica
    resumo_crimes$w[is.infinite(resumo_crimes$w)]<- 0
    resumo_crimes$idcrimes <- paste("cr", 1:nrow(resumo_crimes))
  })

  # HERE I WOULD LIKE TO PLOT THE RESULTING OF THE FILTER DATA. BUT THIS DOESNT WORK
  output$mymap <- renderLeaflet({


      m <-leaflet() %>%
        addTiles() %>%  
        addCircleMarkers( lng=  resumo_crimes$Longitude,  lat = resumo_crimes$Latitude , radius = resumo_crimes$w,  color = "red" ,  stroke = FALSE, fillOpacity = 2) #%>%

      m  # Print the map

  })


   # PARAMETER OF MY ALGORITHM
   P <- reactive({
     p<- input$select_numviaturas
     # RUNNING my algorithm: with output new p points  to add to my map
     new_points <- AL_function(resumo_crimes)

})

   # How to ADD (NO NEW MAP) the P new points to the m map?
   observe({
        #SCRIPT PSEUDOCODE by clicking an action button
        M %>% ADD NEW P POINTS

       )
   })



}

Solution

  • return filtered data at the end of the reactive

    filteredData <- reactive({
      ...
      return(resumo_crimes) 
    })
    

    And call the reactive function in your plot:

    output$mymap <- renderLeaflet({
      m <- ...
        addCircleMarkers( data=filteredData(),
                          lng= ~Longitude,  lat = ~Latitude , radius = ~w,...) 
      m
    })