首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >仅在选择selectInput时才生成映射

仅在选择selectInput时才生成映射
EN

Stack Overflow用户
提问于 2022-03-17 20:57:55
回答 1查看 41关注 0票数 0

我创建了一个闪亮的应用程序,它基于VIKOR多准则方法生成一个集群地图。在这个意义上,我做了两个selecInput,供用户选择是maximize (max)还是minimize (min)一个特定的标准。在这种情况下,我有两个标准。下面的代码按原样工作,因为我没有从代码中删除maxmin <- c('min','max')。但是,如果我退出,我就无法生成地图。但是maxmin是由selecInput选择的。如何调整这个?

可执行代码如下:

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-03-18 11:36:53

您的selectInput必须是这样的。

代码语言:javascript
复制
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')作为参数值。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71518961

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档