How do I make my r codes work inside shiny?

I have two maps in the dashboard and first map can plot data directly from the dataframe. But the second map need some modifications to the dataframe before plotting. I need to make a distance matrix and then cluster the data based on the distance and then plot each cluster with the route using leaflet. The codes are working outside Shiny. Could someone please tell me how to make the script run in the shiny app?

state<- c(State1,State1,State1,State1,State1,State1,State1,State1,State1,State1,State1,State1,State1,State1,State1,State1,State1,State1,State1)
district<- c(D1,D2,D3,D4,D5,D6,D7,D1,D2,D3,D1,D1,D2,D3,D4,D5,D6,D7,D4)
block<- c(B1,B2,B3,B4,B5,B6,B7,B1,B2,B3,B4,B5,B6,B7,B4,B5,B6,B7,B4)
person<- c(dr1,dr2,dr3,dr4,dr5,dr6,dr1,dr2,dr3,dr4,dr5,dr6,dr1,dr2,dr3,dr4,dr5,dr6,dr6)
village<- c(V1,V2,V3,V4,V5,V6,V7,V8,V9,V10,V11,V12,V13,V14,V15,V16,V17,V18,V19)
Store<- c(S1,S2,S3,S4,S5,S6,S7,S8,S9,S10,S11,S12,S13,S14,S15,S16,S17,S18,S19)
ID<- c(165,633,634,637,638,640,641,643,644,645,646,648,649,651,652,653,655,657,658)
lon<- c(25.823541,25.828866,25.806883,25.837276,25.877259,25.915215,25.843175,25.68656,25.856703,25.853131,25.834961,25.797713,25.695643,25.762921,25.747861,25.745232,25.796351,25.840904,25.793371)
lat<- c(84.103431,84.416118,84.477473,84.106071,84.147636,84.20334,84.300451,84.185715,84.026747,84.007323,83.921804,84.184888,84.222116,84.171704,84.209929,84.236606,84.252738,84.06844,84.049946)

ba<- data.frame(state,district,block,person,village,Store,ID,lon,lat)

district<- c(d1,d2,d3,d4,d5,d6)
village<- c(V1,V2,V3,V4,V5,V6)
driver<- c(dr1,dr2,dr3,dr4,dr5,dr6)
lat<- c(84.141826,84.216667,84.104059,83.868195,84.155507,84.210588)
lon<- c(25.759589,25.877103,25.86034,25.972437,25.766596,25.882357)

driver <-data.frame(district,village,driver,lat,lon)



library(shiny)
library(sf)
library(sp)
library(tidyverse)
library(osrm)
library(leaflet)
library(dplyr)
library(geosphere)

Name.Person = c("dr1",
            "dr2",
            "dr3",
            "dr4")
Name.cluster = c('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15')



ui <- fluidPage(
  
  titlePanel("Mapping"),
  
  sidebarLayout(
    sidebarPanel(
      selectInput("Person",
                  "Please Pick the Name of the Person:",
                  choices = Name.Person), 
      selectInput("cluster",
                  "Please Pick the Cluster:",
                  choices = Name.cluster),
       
    
    mainPanel(
      
      
      fluidRow(column(12,
                      "Total Places by Person)",
                      leafletOutput("full"))
      ),
      
      fluidRow(column( 12,
                       "Route for the Selected Cluster",
                       leafletOutput("by_cluster")
      ),
      
      fluidRow(column( 12,
                       "Route for the Selected Cluster",
                       
      )
      
      
      ))
  )
)) 



server <- function(input, output) {
  
  person <- reactive({
    dplyr::filter(ba, person == input$Person)
    
  })
 
  output$cluster <- reactive({
    (input$cluster )
    
    
  }) 
  
 
  size <- reactive({
    (input$size )
    
  }) 
  
  
 # script that is not working in the shiny 
  df00 <- ba
  Distance<- distm(df00[9:10],df00[9:10],fun = distHaversine)
  Distance<- as.data.frame(Distance)
  Distance[is.na(Distance)]<-0
  DMat<- as.dist(Distance)
  hc1 <- hclust(as.dist(DMat), method="complete")
  df00$Clusters<- cutree(hc1, k=15) 
  
# creating table by cluster
  df1 <- df00 %>% filter(Clusters == 15)
  df1a <- df1[,c(5,9,10)]
  colnames(df1a) <- c('name',"lon","lat")
  
  #load location file with clusre and person details
  driver <- driver
  driver <- driver [,c(4,6,7)]
  driver <- driver[driver$name == "Driver1", ]
  
  #output
  
  df.f <- rbind(driver,df1a)
  df.f <- df.f[,c(1,3,2)]
  trip <- osrmTrip(df.f)
  trip_sp <- trip[[1]]$trip
  st_geometry(trip_sp) <- NULL
  
  
  
  
  
  output$full <- renderLeaflet({
    fc <- person()
    
    factpal <- colorFactor(topo.colors(2),  domain = df00$telehealth)
    leaflet(data = fc ) %>% 
      addTiles() %>% 
      addCircleMarkers(lng = fc$lon, lat = fc$lat,
                       color = ~factpal(telehealth),
                       label = fc$Clusters,
                       popup = fc$ID,
                       labelOptions = labelOptions(noHide = T)) %>% 
      addLegend("bottomright", pal = factpal, values = ~telehealth,
                title = "Clustering",
                labFormat = labelFormat(prefix = "No. of "))
  })
  
  output$by_cluster <- renderLeaflet({
    
    
    leaflet(data = trip_sp) %>% 
      
      addTiles() %>% 
      addMarkers(lng = cluster$lon, lat = cluster$lat, label =cluster$CHE.Village, labelOptions = labelOptions(noHide = T)) %>%
      addPolylines()
  })
  
  }    


shinyApp(ui = ui, server = server)
1 Like

Hello.
Thanks for providing code , but you could take further steps to make it more convenient for other forum users to help you.

Share some representative data that will enable your code to run and show the problematic behaviour.

You might use tools such as the library datapasta, or the base function dput() to share a portion of data in code form, i.e. that can be copied from forum and pasted to R session.

Reprex Guide

Thank you for the replay. I have made the changes.

is the problem that your app wont start at all ? or that it starts, but gives wrong results ?
I ask because the UI is broken and so wont be defined.

it starts, but not getting output for the second map.
error Polygon data not found; please provide addPolylines with data and/or lng/lat arguments

output$by_cluster <- renderLeaflet({
    
    
    leaflet(data = trip_sp) %>% 
      
      addTiles() %>% 
      addMarkers(lng = cluster$lon, lat = cluster$lat, label =cluster$CHE.Village, labelOptions = labelOptions(noHide = T)) %>%
      addPolylines()
  })

The codes used to prepare the map is

df00 <- ba
  Distance<- distm(df00[9:10],df00[9:10],fun = distHaversine)
  Distance<- as.data.frame(Distance)
  Distance[is.na(Distance)]<-0
  DMat<- as.dist(Distance)
  hc1 <- hclust(as.dist(DMat), method="complete")
  df00$Clusters<- cutree(hc1, k=15) 
  
# creating table by cluster
  df1 <- df00 %>% filter(Clusters == 15)
  df1a <- df1[,c(5,9,10)]
  colnames(df1a) <- c('name',"lon","lat")
  
  #load location file with clusre and person details
  driver <- driver
  driver <- driver [,c(4,6,7)]
  driver <- driver[driver$name == "Driver1", ]
  
  #output
  
  df.f <- rbind(driver,df1a)
  df.f <- df.f[,c(1,3,2)]
  trip <- osrmTrip(df.f)
  trip_sp <- trip[[1]]$trip
  st_geometry(trip_sp) <- NULL

Sorry, you haven't provided a reprex, so I can't begin to try to help you.
The first error in your reprex is here

state<- c(State1,State1,State1,State1,State1,State1,State1,State1,State1,State1,State1,State1,State1,State1,State1,State1,State1,State1,State1)
Error: object 'State1' not found

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.