R 在传单制作的地图上生成AddPolyline时发生冲突

R 在传单制作的地图上生成AddPolyline时发生冲突,r,shiny,leaflet,R,Shiny,Leaflet,朋友们,你能帮我解决以下问题吗:我在插入addPolylines函数生成第二张传单地图时遇到冲突。通常,图1涉及显示所有簇,图2涉及特定簇。对于这个特定的簇,我插入了一个特性,以附着与在map1上形成的簇相同的颜色。第一个代码正确地完成了上述描述。但是,我还插入了第二个代码,该代码引用了第二个贴图的addPolylines。但是,当我在第一个代码中插入第二个代码时,在与生成Map2有关的部分,它给出了一个错误:警告:eval中的错误:找不到对象“m2”。你能帮我解决这个问题吗 library(s

朋友们,你能帮我解决以下问题吗:我在插入addPolylines函数生成第二张传单地图时遇到冲突。通常,图1涉及显示所有簇,图2涉及特定簇。对于这个特定的簇,我插入了一个特性,以附着与在map1上形成的簇相同的颜色。第一个代码正确地完成了上述描述。但是,我还插入了第二个代码,该代码引用了第二个贴图的addPolylines。但是,当我在第一个代码中插入第二个代码时,在与生成Map2有关的部分,它给出了一个错误:
警告:eval中的错误:找不到对象“m2”
。你能帮我解决这个问题吗

library(shiny)
library(ggplot2)
library(rdist)
library(geosphere)
library(shinythemes)
library(leaflet)

function.cl<-function(df,k,Filter1,Filter2){

  #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), 
                     Waste = c(526, 350, 526, 469, 285, 433, 456)), class = "data.frame", row.names = c(NA, -7L))


  #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 

  #specific cluster and specific propertie
  df1<-df[c("Latitude","Longitude")]
  df1$cluster<-as.factor(clusters)
  df_spec_clust <- df1[df1$cluster == Filter1,]
  df_spec_prop<-df[df$Properties==Filter2,]

  #Table to join df and df1
  data_table <- Reduce(merge, list(df, df1))

  #Color and Icon for map
  ai_colors <-c("red","gray","blue","orange","green","beige","darkgreen","lightgreen", "lightred", "darkblue","lightblue",
                "purple","darkpurple","pink", "cadetblue","white","darkred", "lightgray","black")
  clust_colors <- ai_colors[df$cluster]
  icons <- awesomeIcons(
    icon = 'ios-close',
    iconColor = 'black',
    library = 'ion',
    markerColor =  clust_colors)

  leafIcons <- icons(
    iconUrl = ifelse(df1$Properties,
                     "https://image.flaticon.com/icons/svg/542/542461.svg"
    ),
    iconWidth = 45, iconHeight = 40,
    iconAnchorX = 25, iconAnchorY = 12)
  html_legend <- "<img src='https://image.flaticon.com/icons/svg/542/542461.svg'>"

  # Map for all clusters:
  m1<-leaflet(df1) %>% addTiles() %>%
    addMarkers(~Longitude, ~Latitude, icon = leafIcons) %>%
    addAwesomeMarkers(lat=~df$Latitude, lng = ~df$Longitude, icon=icons, label=~as.character(df$cluster)) %>% 
    addPolylines(lat=~df$Latitude, lng = ~df$Longitude,color="red") %>% 
    addLegend( position = "topright", title="Cluster", colors = ai_colors[1:max(df$cluster)],labels = unique(df$cluster))

  plot1<-m1

  # Map for specific cluster and propertie
  if(nrow(df_spec_clust)>0){
    clust_colors <- ai_colors[df_spec_clust$cluster]
    icons <- awesomeIcons(
      icon = 'ios-close',
      iconColor = 'black',
      library = 'ion',
      markerColor =  clust_colors)
    m2<-leaflet(df_spec_clust) %>% addTiles() %>%
      addAwesomeMarkers(lat=~Latitude, lng = ~Longitude, icon=icons, label=~cluster) 
    plot2<-m2} else plot2 <- NULL   

  return(list(
    "Plot1" = plot1,
    "Plot2" = plot2,
    "Data" = data_table
  ))
}

ui <- bootstrapPage(
  navbarPage(theme = shinytheme("flatly"), collapsible = TRUE,
             "Cl", 
             tabPanel("Solution",
                      sidebarLayout(
                        sidebarPanel(
                          tags$b(h3("Choose the cluster number?")),
                          sliderInput("Slider", h5(""),
                                      min = 2, max = 5, value = 3),
                        ),
                        mainPanel(
                          tabsetPanel(      
                            tabPanel("Solution", (leafletOutput("Leaf1",width = "95%", height = "600")))))

                      ))),
  tabPanel("",
           sidebarLayout(
             sidebarPanel(
               selectInput("Filter1", label = h4("Select just one cluster to show"),""),
               selectInput("Filter2",label=h4("Select the cluster property designated above"),""),
             ),
             mainPanel(
               tabsetPanel(
                 tabPanel("Map", (leafletOutput("Leaf2",width = "95%", height = "600")))))
           )))

server <- function(input, output, session) {

  Modelcl<-reactive({
    function.cl(df,input$Slider,input$Filter1,input$Filter2)
  })

  output$Leaf1 <- renderLeaflet({
    Modelcl()[[1]]
  })

  output$Leaf2 <- renderLeaflet({
    Modelcl()[[2]]
  })

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

  observeEvent(input$Filter1,{
    abc <- req(Modelcl()$Data) %>% filter(cluster == as.numeric(input$Filter1))
    updateSelectInput(session,'Filter2',
                      choices=sort(unique(abc$Properties)))
  }) 


}

shinyApp(ui = ui, server = server)
库(闪亮)
图书馆(GG2)
图书馆(rdist)
图书馆(地球圈)
图书馆(shinythemes)
图书馆(单张)

function.cl@Jovani SouzA@Jose m2当您将对象传递到方法链中时,对象不存在,您的意思是将m1传递到方法链中,以便添加多段线来创建m2

library(shiny)
library(ggplot2)
library(rdist)
library(geosphere)
library(shinythemes)
library(leaflet)

function.cl<-function(df,k,Filter1,Filter2){

  #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), 
                     Waste = c(526, 350, 526, 469, 285, 433, 456)), class = "data.frame", row.names = c(NA, -7L))


  #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 

  #specific cluster and specific propertie
  df1<-df[c("Latitude","Longitude")]
  df1$cluster<-as.factor(clusters)
  df_spec_clust <- df1[df1$cluster == Filter1,]
  df_spec_prop<-df[df$Properties==Filter2,]

  #Table to join df and df1
  data_table <- Reduce(merge, list(df, df1))

  #Color and Icon for map
  ai_colors <-c("red","gray","blue","orange","green","beige","darkgreen","lightgreen", "lightred", "darkblue","lightblue",
                "purple","darkpurple","pink", "cadetblue","white","darkred", "lightgray","black")
  clust_colors <- ai_colors[df$cluster]
  icons <- awesomeIcons(
    icon = 'ios-close',
    iconColor = 'black',
    library = 'ion',
    markerColor =  clust_colors)

  leafIcons <- icons(
    iconUrl = ifelse(df1$Properties,
                     "https://image.flaticon.com/icons/svg/542/542461.svg"
    ),
    iconWidth = 45, iconHeight = 40,
    iconAnchorX = 25, iconAnchorY = 12)
  html_legend <- "<img src='https://image.flaticon.com/icons/svg/542/542461.svg'>"

  # Map for all clusters:
  m1<-leaflet(df1) %>% addTiles() %>%
    addMarkers(~Longitude, ~Latitude, icon = leafIcons) %>%
    addAwesomeMarkers(lat=~df$Latitude, lng = ~df$Longitude, icon=icons, label=~as.character(df$cluster)) %>% 
    addPolylines(lat=~df$Latitude, lng = ~df$Longitude,color="red") %>% 
    addLegend( position = "topright", title="Cluster", colors = ai_colors[1:max(df$cluster)],labels = unique(df$cluster))

  plot1<-m1

  # Map for specific cluster and propertie
  if(nrow(df_spec_clust)>0){
    clust_colors <- ai_colors[df_spec_clust$cluster]
    icons <- awesomeIcons(
      icon = 'ios-close',
      iconColor = 'black',
      library = 'ion',
      markerColor =  clust_colors)
    m2<-leaflet(df_spec_clust) %>% addTiles() %>%
      addAwesomeMarkers(lat=~Latitude, lng = ~Longitude, icon=icons, label=~cluster) 
    plot2<-m2} else plot2 <- NULL


  for(i in 1:nrow(df_spec_clust)){
    df_line <- rbind(df_spec_prop[,c("Latitude","Longitude")],
                     df_spec_clust[i,c("Latitude","Longitude")])
    m2 <- m1 %>%
      addPolylines(data = df_line, 
                   lat=~Latitude, 
                   lng = ~Longitude,
                   color="red")
  }
  plot2<-m2


  return(list(
    "Plot1" = plot1,
    "Plot2" = plot2,
    "Data" = data_table
  ))
}

ui <- bootstrapPage(
  navbarPage(theme = shinytheme("flatly"), collapsible = TRUE,
             "Cl", 
             tabPanel("Solution",
                      sidebarLayout(
                        sidebarPanel(
                          tags$b(h3("Choose the cluster number?")),
                          sliderInput("Slider", h5(""),
                                      min = 2, max = 5, value = 3),
                        ),
                        mainPanel(
                          tabsetPanel(      
                            tabPanel("Solution", (leafletOutput("Leaf1",width = "95%", height = "600")))))

                      ))),
  tabPanel("",
           sidebarLayout(
             sidebarPanel(
               selectInput("Filter1", label = h4("Select just one cluster to show"),""),
               selectInput("Filter2",label=h4("Select the cluster property designated above"),""),
             ),
             mainPanel(
               tabsetPanel(
                 tabPanel("Map", (leafletOutput("Leaf2",width = "95%", height = "600")))))
           )))

server <- function(input, output, session) {

  Modelcl<-reactive({
    function.cl(df,input$Slider,input$Filter1,input$Filter2)
  })

  output$Leaf1 <- renderLeaflet({
    Modelcl()[[1]]
  })

  output$Leaf2 <- renderLeaflet({
    Modelcl()[[2]]
  })

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

  observeEvent(input$Filter1,{
    abc <- req(Modelcl()$Data) %>% filter(cluster == as.numeric(input$Filter1))
    updateSelectInput(session,'Filter2',
                      choices=sort(unique(abc$Properties)))
  }) 


}

shinyApp(ui = ui, server = server)
库(闪亮)
图书馆(GG2)
图书馆(rdist)
图书馆(地球圈)
图书馆(shinythemes)
图书馆(单张)

function.cl您必须在
if
语句中插入代码:

# Map for specific cluster and propertie
if(nrow(df_spec_clust)>0){
    clust_colors <- ai_colors[df_spec_clust$cluster]
    icons <- awesomeIcons(
        icon = 'ios-close',
        iconColor = 'black',
        library = 'ion',
        markerColor =  clust_colors)
    m2<-leaflet(df_spec_clust) %>% addTiles() %>%
        addAwesomeMarkers(lat=~Latitude, lng = ~Longitude, icon=icons, label=~cluster) 

    for(i in 1:nrow(df_spec_clust)){
        df_line <- rbind(df_spec_prop[,c("Latitude","Longitude")],
                         df_spec_clust[i,c("Latitude","Longitude")])
        m2 <- m2 %>%
            addPolylines(data = df_line, 
                         lat=~Latitude, 
                         lng = ~Longitude,
                         color="red")
    }
    plot2<-m2} else plot2 <- NULL   
#特定集群和属性的映射
如果(nrow(df_spec_clust)>0){

clust_colors感谢friend的回答。但是你的代码,地图上的第二个显示的图像与第一个地图相同,但这不是我想要的。第二个是指一个特定的群集。为了更好地理解,我调整了问题和代码。@JovaniSouza这就是你想要的吗?请参阅我的解决方案。
library(shiny)
library(ggplot2)
library(rdist)
library(geosphere)
library(shinythemes)
library(leaflet)

function.cl<-function(df,k,Filter1,Filter2){

  #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), 
                     Waste = c(526, 350, 526, 469, 285, 433, 456)), class = "data.frame", row.names = c(NA, -7L))


  #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 

  #specific cluster and specific propertie
  df1<-df[c("Latitude","Longitude")]
  df1$cluster<-as.factor(clusters)
  df_spec_clust <- df1[df1$cluster == Filter1,]
  df_spec_prop<-df[df$Properties==Filter2,]

  #Table to join df and df1
  data_table <- Reduce(merge, list(df, df1))

  #Color and Icon for map
  ai_colors <-c("red","gray","blue","orange","green","beige","darkgreen","lightgreen", "lightred", "darkblue","lightblue",
                "purple","darkpurple","pink", "cadetblue","white","darkred", "lightgray","black")
  clust_colors <- ai_colors[df$cluster]
  icons <- awesomeIcons(
    icon = 'ios-close',
    iconColor = 'black',
    library = 'ion',
    markerColor =  clust_colors)

  leafIcons <- icons(
    iconUrl = ifelse(df1$Properties,
                     "https://image.flaticon.com/icons/svg/542/542461.svg"
    ),
    iconWidth = 45, iconHeight = 40,
    iconAnchorX = 25, iconAnchorY = 12)
  html_legend <- "<img src='https://image.flaticon.com/icons/svg/542/542461.svg'>"

  # Map for all clusters:
  m1<-leaflet(df1) %>% addTiles() %>%
    addMarkers(~Longitude, ~Latitude, icon = leafIcons) %>%
    addAwesomeMarkers(lat=~df$Latitude, lng = ~df$Longitude, icon=icons, label=~as.character(df$cluster)) %>% 
    addPolylines(lat=~df$Latitude, lng = ~df$Longitude,color="red") %>% 
    addLegend( position = "topright", title="Cluster", colors = ai_colors[1:max(df$cluster)],labels = unique(df$cluster))

  plot1<-m1

  # Map for specific cluster and propertie
  if(nrow(df_spec_clust)>0){
    clust_colors <- ai_colors[df_spec_clust$cluster]
    icons <- awesomeIcons(
      icon = 'ios-close',
      iconColor = 'black',
      library = 'ion',
      markerColor =  clust_colors)
    m2<-leaflet(df_spec_clust) %>% addTiles() %>%
      addAwesomeMarkers(lat=~Latitude, lng = ~Longitude, icon=icons, label=~cluster) 
    plot2<-m2} else plot2 <- NULL


  for(i in 1:nrow(df_spec_clust)){
    df_line <- rbind(df_spec_prop[,c("Latitude","Longitude")],
                     df_spec_clust[i,c("Latitude","Longitude")])
    m2 <- m1 %>%
      addPolylines(data = df_line, 
                   lat=~Latitude, 
                   lng = ~Longitude,
                   color="red")
  }
  plot2<-m2


  return(list(
    "Plot1" = plot1,
    "Plot2" = plot2,
    "Data" = data_table
  ))
}

ui <- bootstrapPage(
  navbarPage(theme = shinytheme("flatly"), collapsible = TRUE,
             "Cl", 
             tabPanel("Solution",
                      sidebarLayout(
                        sidebarPanel(
                          tags$b(h3("Choose the cluster number?")),
                          sliderInput("Slider", h5(""),
                                      min = 2, max = 5, value = 3),
                        ),
                        mainPanel(
                          tabsetPanel(      
                            tabPanel("Solution", (leafletOutput("Leaf1",width = "95%", height = "600")))))

                      ))),
  tabPanel("",
           sidebarLayout(
             sidebarPanel(
               selectInput("Filter1", label = h4("Select just one cluster to show"),""),
               selectInput("Filter2",label=h4("Select the cluster property designated above"),""),
             ),
             mainPanel(
               tabsetPanel(
                 tabPanel("Map", (leafletOutput("Leaf2",width = "95%", height = "600")))))
           )))

server <- function(input, output, session) {

  Modelcl<-reactive({
    function.cl(df,input$Slider,input$Filter1,input$Filter2)
  })

  output$Leaf1 <- renderLeaflet({
    Modelcl()[[1]]
  })

  output$Leaf2 <- renderLeaflet({
    Modelcl()[[2]]
  })

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

  observeEvent(input$Filter1,{
    abc <- req(Modelcl()$Data) %>% filter(cluster == as.numeric(input$Filter1))
    updateSelectInput(session,'Filter2',
                      choices=sort(unique(abc$Properties)))
  }) 


}

shinyApp(ui = ui, server = server)
# Map for specific cluster and propertie
if(nrow(df_spec_clust)>0){
    clust_colors <- ai_colors[df_spec_clust$cluster]
    icons <- awesomeIcons(
        icon = 'ios-close',
        iconColor = 'black',
        library = 'ion',
        markerColor =  clust_colors)
    m2<-leaflet(df_spec_clust) %>% addTiles() %>%
        addAwesomeMarkers(lat=~Latitude, lng = ~Longitude, icon=icons, label=~cluster) 

    for(i in 1:nrow(df_spec_clust)){
        df_line <- rbind(df_spec_prop[,c("Latitude","Longitude")],
                         df_spec_clust[i,c("Latitude","Longitude")])
        m2 <- m2 %>%
            addPolylines(data = df_line, 
                         lat=~Latitude, 
                         lng = ~Longitude,
                         color="red")
    }
    plot2<-m2} else plot2 <- NULL