I create a shiny app that generates a cluster map based on the VIKOR multicriteria method. In this sense, I made two selecInput
for the user to select whether to maximize (max)
or minimize (min)
a certain criterion. In this case I have two criteria. The code below works as it is, as I didn't remove maxmin <- c('min','max')
from the code. However, if I withdraw, I cannot generate the map. But the idea is that max
and min
are selected by selecInput
. How to adjust this?
Executable code below:
library(shiny)
library(rdist)
library(geosphere)
library(shinythemes)
library(leaflet)
library(shinyjs)
library(MCDM)
function.cl<-function(df,k,maxmin){
#database df
df<-structure(list(Properties = c(1,2,3,4,5,6,7),
Latitude = c(-23.8, -23.8, -23.9, -23.9, -23.9,-23.4,-23.5),
Longitude = c(-49.6, -49.3, -49.4, -49.8, -49.6,-49.4,-49.2),
Coverage = c (1526, 2350, 3526, 2469, 1285, 2433, 2456),
Production = c(526, 350, 526, 469, 285, 433, 456)), class = "data.frame", row.names = c(NA, -7L))
#Vikor
df1 <- df[c(4:5)]
df1<-data.matrix(df1)
weights <- c(0.5,0.5)
maxmin <- c('min','max')
v <- 0.5
scaled<-VIKOR(df1,weights,maxmin,v)
k<-subset(scaled, Ranking==2)$Alternatives #cluster number
#clusters
coordinates<-df[c("Latitude","Longitude")]
d<-as.dist(distm(coordinates[,2:1]))
fit.average<-hclust(d,method="average")
clusters<-cutree(fit.average, k)
nclusters<-matrix(table(clusters))
df$cluster <- clusters
df1<-df[c("Latitude","Longitude")]
#Color and Icon for map
ai_colors <-c("red","gray","blue","orange","green","beige")
clust_colors <- ai_colors[df$cluster]
icons <- awesomeIcons(
icon = 'ios-close',
iconColor = 'black',
library = 'ion',
markerColor = clust_colors)
# Map for all clusters:
m1<-leaflet(df1) %>% addTiles() %>%
addMarkers(~Longitude, ~Latitude) %>%
addAwesomeMarkers(lat=~df$Latitude, lng = ~df$Longitude, icon=icons, label=~as.character(df$cluster)) %>%
addLegend( position = "topright", title="Cluster", colors = ai_colors[1:max(df$cluster)],labels = unique(df$cluster))
plot1<-m1
return(list(
"Plot1" = plot1
))
}
ui <- bootstrapPage(
useShinyjs(),
navbarPage(theme = shinytheme("flatly"), collapsible = TRUE,
"Cl",
tabPanel("Solution",
sidebarLayout(
sidebarPanel(
selectInput("maxmin1", label = h5("Maximize or Minimize?"),
choices = list("","max " = "1", "min" = "2"), selected = "1"),
selectInput("maxmin2", label = h5("Maximize or Minimize?"),
choices = list("","max " = "1", "min" = "2"), selected = "2")),
mainPanel(
tabsetPanel(
tabPanel("Solution", (leafletOutput("Leaf1",width = "95%", height = "600")))))
))))
server <- function(input, output, session) {
Modelcl<-reactive({
function.cl(df,k,maxmin=c(input$maxmin1, input$maxmin2))
})
output$Leaf1 <- renderLeaflet({
req(maxmin=c(input$maxmin1, input$maxmin2))
Modelcl()[[1]]
})
}
shinyApp(ui = ui, server = server)