2017-05-16 32 views
0

我正在尝试做一个交互式地图,但当输入变量被执行时,我无法执行过滤器。在此之后,我想要启动一个动作按钮来运行个人算法,以便在地图上添加一些点。首先,这是我的UI.R在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) 
       ) 
) 

) 

现在,这是我的Server.R。有没有动作,当我尝试的反应

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

末过滤数据后,绘制点

# 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 

     ) 
    }) 



} 

回答

2

return过滤数据和调用的活性功能在你的情节:

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