rgoogle-mapsshinygoogleway

How to Link selected cluster in shiny app


First, I'll present an example below for you to see how it works. The code below generates the route between two coordinates.

library(googleway)

set_key( "API KEY")


#databases
  df<-structure(list(Properties = c(1,2), 
                     Latitude = c(-24.930473, -24.95575), 
                     Longitude = c(-49.994889, -49.990162), 
                     cluster = c(1,2)), class = "data.frame", row.names = c(NA, -2L))
  
  df1<-structure(list(Properties = c(3,4),Latitude = c(-24.924361,-24.95575), 
                       Longitude = c(-50.004343, -50.007371), 
                       cluster = c(1,2)), class = "data.frame", row.names = c(NA, -2L))

 #Table to join df and df1
  data_table <- rbind(df,df1)
  data_table<-data_table[c(2:3)]

  df2<-google_directions(origin = data_table[1,], destination = data_table[3,], 
                        mode = "driving") #I specified properties 1 and 3 which are from cluster 1
  
  df_routes <- data.frame(polyline = direction_polyline(df2))
  
  
  m1<-google_map() %>%
    add_polylines(data = df_routes, polyline = "polyline")

enter image description here

Now, I'm trying to do this in Shiny, but without specifying the coordinates of the properties like I did in the example above. In this sense, I created a selecInput to select which cluster I want to see the route. How to adjust this in this code below?

This question can help: Adjust new features in shiny

library(shiny)
library(rdist)
library(geosphere)
library(shinythemes)
library(googleway)


set_key( "API KEY")



function.cl<-function(df,df1,k,Filter1){
  
  #database df
  df<-structure(list(Properties = c(1,2), 
                     Latitude = c(-24.930473, -24.95575), 
                     Longitude = c(-49.994889, -49.990162), 
                     cluster = c(1,2)), class = "data.frame", row.names = c(NA, -2L))
  
  df1<-structure(list(Properties = c(3,4),Latitude = c(-24.924361,-24.95575), 
                      Longitude = c(-50.004343, -50.007371), 
                      cluster = c(1,2)), class = "data.frame", row.names = c(NA, -2L))
  
  #Table to join df and df1
  data_table <- rbind(df,df1)
  data_table1<-data_table[c(2:3)]
  
  #Generate the map with routes
  
  df2<-google_directions(origin = data_table1[1,], destination = data_table1[3,], 
                         mode = "driving")
  
  df_routes <- data.frame(polyline = direction_polyline(df2))
  
  
  m1<-google_map() %>%
    add_polylines(data = df_routes, polyline = "polyline")
  
  plot1<-m1 
  
  return(list(
    "Plot1" = plot1,
    "Data" = data_table
  ))
}

ui <- bootstrapPage(
  
  navbarPage(theme = shinytheme("flatly"), collapsible = TRUE,
             "Cl", 
             tabPanel("Map of all clusters",
                      sidebarLayout(
                        sidebarPanel(
                          tags$b(h3("Choose the cluster number?")),
                          sliderInput("Slider", h5(""),
                                      min = 2, max = 2, value = 2),
                          selectInput("Filter1", label = h4("Select just one cluster to show"),""),
                        ),
                        
                        mainPanel(
                          tabsetPanel(      
                            tabPanel("Solution", (google_mapOutput("G2",width = "95%", height = "600")))))
                        
                      ))))


server <- function(input, output, session) {
  
  Modelcl<-reactive({
    function.cl(df,df1,input$Slider,input$Filter1)
  })

    output$G2 <- renderGoogle_map({
    Modelcl()[[1]]
  })
  
  
  observeEvent(input$Slider, {
    abc <- req(Modelcl()$Data)
    updateSelectInput(session,'Filter1',
                      choices=sort(unique(abc$cluster)))
  }) 
  
}

shinyApp(ui = ui, server = server)

enter image description here


Solution

  • If k represents the cluster number that you are targeting, and cluster is a column in your dataset (I don't understand why you have df and df1, and then row bind them together), then you can simply do something like this to limit the input to the google_directions() call to only be the rows that correspond to cluster k

    data_table1<-data_table[data_table$cluster==k,c(2:3)]
    

    Then, in the call to google_directions(), you would do this (notice that I'm now calling row 1 and row 2 (instead of row 1 and row 3 in your example), because the filtering on data_table$cluster==k above has ensured that data_table1 has only the two rows associated with cluster k

    df2<-google_directions(
      origin = data_table1[1,],
      destination = data_table1[2,],
      mode = "driving")
    

    Finally, I'm not sure you want to return all of data_table. Perhaps you adjust so that it returns only data_table1 (i.e. the subset wih your cluster of interest)?:

    return(list(
      "Plot1" = plot1,
      "Data" = data_table1
    ))