在R中,使用闪亮和传单,过滤和动作(运行算法)

时间:2017-05-16 22:04:42

标签: r shiny leaflet

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

)

现在,这是我的Server.R。当我尝试在过滤数据之后绘制点时没有动作

# 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

       )
   })



}

1 个答案:

答案 0 :(得分:2)

return在被动

结束时过滤了数据
filteredData <- reactive({
  ...
  return(resumo_crimes) 
})

并在你的情节中调用反应函数:

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