我创建了一个闪亮的应用程序,它基于VIKOR多准则方法生成一个集群地图。在这个意义上,我做了两个selecInput,供用户选择是maximize (max)还是minimize (min)一个特定的标准。在这种情况下,我有两个标准。下面的代码按原样工作,因为我没有从代码中删除maxmin <- c('min','max')。但是,如果我退出,我就无法生成地图。但是max和min是由selecInput选择的。如何调整这个?
可执行代码如下:
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)发布于 2022-03-18 11:36:53
您的selectInput必须是这样的。
selectInput("maxmin1", label = h5("Maximize or Minimize?"),
choices = list("", "max", "min"), selected = "max"),
selectInput("maxmin2", label = h5("Maximize or Minimize?"),
choices = list("", "max", "min"), selected = "min")),你在做"max" = "1" "min" = "2"。这意味着您要将"1" "2"传递给VIKOR函数中的cb参数。但是cb将接受c('min', 'max')作为参数值。
https://stackoverflow.com/questions/71518961
复制相似问题