简体   繁体   English

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

[英]In R, using shiny and 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 首先,这是我的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. 现在,这是我的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

       )
   })



}

return filtered data at the end of the reactive 在反应式结束时return过滤的数据

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
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM