R 闪亮的传单不';无法使用动态菜单正常工作

R 闪亮的传单不';无法使用动态菜单正常工作,r,dynamic,shiny,leaflet,dynamic-data-display,R,Dynamic,Shiny,Leaflet,Dynamic Data Display,我为这个问题挣扎了一段时间,但没有成功。我想建立一个闪亮的互动应用程序表和地图。它结合了不同的数据集。其思想是能够选择所需的数据集,过滤该数据集中的数据,并将其渲染到地图上 我设法建立了一个交互式菜单和反应过滤功能,但我有问题的传单地图。当我在一些数据集之间切换时,它会崩溃。我只能在localisations macro或localisations micro之间切换,但在micro和macro之间切换时它不起作用(见下图) 问题与错误有关: 警告:总和错误:参数的“类型”(列表)无效,但我现

我为这个问题挣扎了一段时间,但没有成功。我想建立一个闪亮的互动应用程序表和地图。它结合了不同的数据集。其思想是能够选择所需的数据集,过滤该数据集中的数据,并将其渲染到地图上

我设法建立了一个交互式菜单和反应过滤功能,但我有问题的传单地图。当我在一些数据集之间切换时,它会崩溃。我只能在localisations macro或localisations micro之间切换,但在micro和macro之间切换时它不起作用(见下图)

问题与错误有关:
警告:总和错误:参数的“类型”(列表)无效
,但我现在知道如何修复它

我还在
observerEvent
:

if(inpud$data.type==“本地化”){}。。。。。
else if(输入$data.type==“Micro”){}。。。。。
else{}

但也不起作用

以下是该应用程序的示例:

library(shiny)
library(leaflet)
library(dplyr)


#### UI
ui <- fluidPage(

  titlePanel("Map"),

  leafletOutput("map"),

  fluidRow(
    column(2, offset = 0, style='padding:10px;',
           radioButtons("data.type", "Type of  data", c("Localisations", "Micro", "Macro"))),
    column(2, offset = 0, style='padding:10px;',
           uiOutput("position")),
    column(2, offset = 0, style='padding:10px;',
           uiOutput("kind"))
  ),

  dataTableOutput("table") ## to check the filtering

)


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

  ##### data #####


  sites <- data.frame(Site=c("Site1", "Site2"),
                      Lat=c(54, 56), 
                      Long=c(16, 18)) 


  micro <- data.frame(Site=c(rep("Site1",4), rep("Site2", 4)),
                      Position=c("Micro_pos1","Micro_pos1", "Micro_pos2", "Micro_pos2"),
                      Kind=rep(c("blue_fiber", "red_fiber"), 4),
                      Amount=c(5, 46, 64, 32, 54, 38, 29, 31) )  

  micro <- full_join(micro, sites)

  macro <- data.frame(Site=c(rep("Site1",4), rep("Site2", 4)),
                      Position=c("Macro_pos1","Macro_pos1", "Macro_pos2", "Macro_pos2"),
                      Kind=rep(c("Cigarretes", "Pellets"), 4),
                      Amount=c(3, 16, 4, 12, 14, 18, 19, 21) )  

  macro <- full_join(macro, sites)    


  #### dynamic menu ####

  ### position
  output$position <- renderUI({


    switch(input$data.type,
           "Micro"=radioButtons("position", "Micro position:", 
                                                       choices = c("Micro_pos1", "Micro_pos2")),
           "Macro"=radioButtons("position", "Macro position:", 
                                               choices = c("Macro_pos1", "Macro_pos2"))
    )
  })

  ## kind

  output$kind <- renderUI({


    switch(input$data.type,
           "Micro"=checkboxGroupInput("kind", "kind of micro:", 
                                                             choices = c("blue_fiber", "red_fiber"),
                                                             selected = c("blue_fiber", "red_fiber")),
           "Macro"=checkboxGroupInput("kind", "kind of macro:", 
                                                     choices = c("Cigarretes", "Pellets"),
                                                     selected=c("Cigarretes", "Pellets"))
    )
  })


#### reactive table to filter data to map ####

table <- reactive({


    if(input$data.type=="Localisations"){

      return(sites)
    }  

    else if (input$data.type=="Micro") {

      if (is.null(input$position))
        return(NULL)
      if (!is.null(input$position))

        micro <-micro[micro$Position==input$position,]
      micro<-micro[micro$Kind %in% input$kind,]

      micro <- micro %>% 
        group_by(Site, Lat, Long, Position)%>%
        summarise(Amount=sum(Amount))
      micro
      }

  else if (input$data.type=="Macro") {

    if (is.null(input$position))
      return(NULL)
    if (!is.null(input$position))

      macro <-macro[macro$Position==input$position,]
    macro<-macro[macro$Kind %in% input$kind,]

     macro <- macro %>%
      group_by(Site, Lat, Long, Position)%>%
      summarise(Amount=sum(Amount))
    macro
    }
  })


#### table with filtered data ####
output$table <- renderDataTable({
  table()
})



#### base map ####


  output$map <- renderLeaflet({
    leaflet(sites) %>%
      setView(lat=55, lng=17, zoom=6) %>%
      addProviderTiles(providers$Esri.WorldImagery) %>%
      addCircleMarkers(lng=~Long, lat=~Lat, label=~Site, 
                       labelOptions = labelOptions(noHide =T))
  })




  ##### and now it gets complicated :( ####

   observeEvent( c( input$data.type, input$position, input$kind), {

    if(input$data.type=="Localisations"){

      leafletProxy("map", data=sites) %>%
        clearMarkers() %>%
        clearShapes()%>%
        addCircleMarkers(lng=~Long, lat=~Lat, label=~Site,
                         labelOptions = labelOptions(noHide =T), fillColor = "red")
    }


     else {

        if (is.null(input$position))
           return(NULL)
        if (is.null(input$kind))
          return(NULL)


        leafletProxy("map", data=table()) %>%
        clearMarkers() %>%
        clearShapes()%>%

        addCircles(lng=~Long, lat=~Lat, label=~Site, color="white", fill="white",
                   labelOptions = labelOptions(noHide =T),
                   radius = ~Amount*1000) %>%
        addLabelOnlyMarkers(lng=~Long, lat=~Lat, label=~as.character(Amount),
                            labelOptions = labelOptions(noHide = T, direction = 'top', textOnly = T, textsize="20px"))

     }



  })

  ### server end
}

# Run the application 
shinyApp(ui = ui, server = server)
库(闪亮)
图书馆(单张)
图书馆(dplyr)
####用户界面

ui问题在于时间:

  • 从micro切换到macro时,input$kind仍设置为仅适用于micro的值
  • 然后调用table()来创建映射。它过滤宏[macro$Kind%在%input$Kind,]
中,这将导致一个空集,因为input$Kind包含的仍然是红色和蓝色的纤维,而不是“Cigarretes”和“小球”
  • 您可以使用更新选择框

    output$kind <- renderUI({
    
    
    switch(input$data.type,
           "Micro"=checkboxGroupInput("kind", "kind of micro:", 
                                      choices = c("blue_fiber", "red_fiber"),
                                      selected = c("blue_fiber", "red_fiber")),
           "Macro"=checkboxGroupInput("kind", "kind of macro:", 
                                      choices = c("Cigarretes", "Pellets"),
                                      selected=c("Cigarretes", "Pellets"))
    )
    })
    
    然后更改表格功能:

      table <- function(){
    
    
        if(input$data.type=="Localisations"){
    
          return(sites)
        }  
    
        else if (input$data.type=="Micro") {
    
          if (is.null(input$position))
            return(NULL)
          if (!is.null(input$position))
    
            micro <-micro[micro$Position==input$position,]
          micro<-micro[micro$Kind %in% input$kind,]
    
          micro <- micro %>% 
            group_by(Site, Lat, Long, Position)%>%
            summarise(Amount=sum(Amount))
          micro
        }
    
        else if (input$data.type=="Macro") {
    
          if (is.null(input$position))
            return(NULL)
          if (!is.null(input$position))
    
            macro <-macro[macro$Position==input$position,]
          macro<-macro[macro$Kind %in% input$kind,]
    
          macro <- macro %>%
            group_by(Site, Lat, Long, Position)%>%
            summarise(Amount=sum(Amount))
          macro
        }
      }
    
    要使数据表具有反应性,可以执行以下操作:

      output$table <- renderDataTable({
        input$button
        table()
      })
    

    output$table非常感谢@ge.org的努力,但是带有summit按钮的解决方案不适合我的应用程序,因为在真实应用程序中,我有更多的类别,用户每次更改summit都会很尴尬。但是多亏了你关于时间安排的评论,我通过编辑
    observeEvent
    绕过了这个问题。对于每个数据类型,我将其分为三个单独的条件。在负责微观和宏观的条件中,我添加了新条件,以省略
    input$kind
    中的错误类别。这会在控制台中发出一些警告,但似乎不会影响整个应用程序

    以下是新代码:

    library(shiny)
    library(leaflet)
    library(dplyr)
    
    
    
    #### UI
    ui <- fluidPage(
    
      titlePanel("Map"),
    
      leafletOutput("map"),
    
      fluidRow(
        column(2, offset = 0, style='padding:10px;',
               radioButtons("data.type", "Type of  data", c("Localisations", "Micro", "Macro"))),
        column(2, offset = 0, style='padding:10px;',
               uiOutput("position")),
        column(2, offset = 0, style='padding:10px;',
               uiOutput("kind"))
      ),
    
      dataTableOutput("table"), ## to check the filtering
      textOutput("kind.of")
    )
    
    
    server <- function(input, output, session) {
    
      ##### data #####
    
    
      sites <- data.frame(Site=c("Site1", "Site2"),
                          Lat=c(54, 56), 
                          Long=c(16, 18)) 
    
    
      micro <- data.frame(Site=c(rep("Site1",4), rep("Site2", 4)),
                          Position=c("Micro_pos1","Micro_pos1", "Micro_pos2", "Micro_pos2"),
                          Kind=rep(c("blue_fiber", "red_fiber"), 4),
                          Amount=c(5, 46, 64, 32, 54, 38, 29, 31) )  
    
      micro <- full_join(micro, sites)
    
      macro <- data.frame(Site=c(rep("Site1",4), rep("Site2", 4)),
                          Position=c("Macro_pos1","Macro_pos1", "Macro_pos2", "Macro_pos2"),
                          Kind=rep(c("Cigarretes", "Pellets"), 4),
                          Amount=c(3, 16, 4, 12, 14, 18, 19, 21) )  
    
      macro <- full_join(macro, sites)    
    
    
      #### dynamic menu ####
    
      ### position
      output$position <- renderUI({
    
    
        switch(input$data.type,
               "Micro"=radioButtons("position", "Micro position:", 
                                                           choices = c("Micro_pos1", "Micro_pos2")),
               "Macro"=radioButtons("position", "Macro position:", 
                                                   choices = c("Macro_pos1", "Macro_pos2"))
        )
      })
    
      ## kind
    
      output$kind <- renderUI({
    
    
        switch(input$data.type,
               "Micro"=checkboxGroupInput("kind", "kind of micro:", 
                                                                 choices = c("blue_fiber", "red_fiber"),
                                                                 selected = c("blue_fiber", "red_fiber")),
               "Macro"=checkboxGroupInput("kind", "kind of macro:", 
                                                         choices = c("Cigarretes", "Pellets"),
                                                         selected=c("Cigarretes", "Pellets"))
        )
      })
    
    
    #### reactive table to filter data to map ####
    
    table <- reactive({
    
    
        if(input$data.type=="Localisations"){
    
          return(sites)
        }  
    
        else if (input$data.type=="Micro") {
    
          if (is.null(input$position))
            return(NULL)
          if (!is.null(input$position))
    
            micro <-micro[micro$Position==input$position,]
          micro<-micro[micro$Kind %in% input$kind,]
    
          micro <- micro %>% 
            group_by(Site, Lat, Long, Position)%>%
            summarise(Amount=sum(Amount))
          micro
          }
    
      else if (input$data.type=="Macro") {
    
        if (is.null(input$position))
          return(NULL)
        if (!is.null(input$position))
    
          macro <-macro[macro$Position==input$position,]
        macro<-macro[macro$Kind %in% input$kind,]
    
         macro <- macro %>%
          group_by(Site, Lat, Long, Position)%>%
          summarise(Amount=sum(Amount))
        macro
        }
      })
    
    
    #### table with filtered data ####
    output$table <- renderDataTable({
      table()
    })
    
    
    #### base map ####
    
    
      output$map <- renderLeaflet({
        leaflet(sites) %>%
          setView(lat=55, lng=17, zoom=6) %>%
          addProviderTiles(providers$Esri.WorldImagery) %>%
          addCircleMarkers(lng=~Long, lat=~Lat, label=~Site, 
                           labelOptions = labelOptions(noHide =T))
      })
    
    
      #### updating the map #####
    
       observeEvent( c( input$data.type, input$position, input$kind), {
    
    
    
      if(input$data.type=="Localisations"){
    
          leafletProxy("map", data=sites) %>%
            clearMarkers() %>%
            clearShapes()%>%
            addCircleMarkers(lng=~Long, lat=~Lat, label=~Site,
                             labelOptions = labelOptions(noHide =T), fillColor = "red")
        }
    
    
         else if (input$data.type=="Micro") {
    
            if (is.null(input$position))
               return(NULL)
             if (is.null(input$kind))
              return(NULL)
    
    ###########################################################
      ####### and here 4 new line that did all the job #########
           if (input$position %in% c("Macro_pos1", "Macro_pos2")) 
             return(NULL) ## new line
           if (input$kind %in% c("Cigarretes", "Pellets"))
             return(NULL)
      #################################################### 
    
    
    
            leafletProxy("map", data=table()) %>%
            clearMarkers() %>%
            clearShapes()%>%
    
            addCircles(lng=~Long, lat=~Lat, label=~Site, color="white", fill="white",
                       labelOptions = labelOptions(noHide =T),
                       radius = ~Amount*1000) %>%
            addLabelOnlyMarkers(lng=~Long, lat=~Lat, label=~as.character(Amount),
                                labelOptions = labelOptions(noHide = T, direction = 'top', textOnly = T, textsize="20px"))
    
               }
    
         else if(input$data.type=="Macro") {
    
           if (is.null(input$position))
             return(NULL)
           if (is.null(input$kind))
             return(NULL)
    
    ###########################################################
      ####### and here 4 new line that did all the job #########
           if (input$position %in% c("Micro_pos1", "Micro_pos2"))
             return(NULL)
           if (input$kind %in% c("blue_fiber", "red_fiber"))
             return(NULL)
      ##############################################
    
           leafletProxy("map", data=table()) %>%
             clearMarkers() %>%
             clearShapes()%>%
    
             addCircles(lng=~Long, lat=~Lat, label=~Site, color="white", fill="white",
                        labelOptions = labelOptions(noHide =T),
                        radius = ~Amount*1000) %>%
             addLabelOnlyMarkers(lng=~Long, lat=~Lat, label=~as.character(Amount),
                                 labelOptions = labelOptions(noHide = T, direction = 'top', textOnly = T, textsize="20px"))
    
         }
    
          })
    
      ### server end
    }
    
    # Run the application 
    shinyApp(ui = ui, server = server)
    
    库(闪亮)
    图书馆(单张)
    图书馆(dplyr)
    ####用户界面
    ui%
    addLabelOnlyMarkers(lng=~Long,lat=~lat,label=~as.character(Amount),
    labelOptions=labelOptions(noHide=T,direction='top',textOnly=T,textsize=“20px”))
    }
    })
    ###服务器端
    }
    #运行应用程序
    shinyApp(用户界面=用户界面,服务器=服务器)
    
      output$table <- renderDataTable({
        input$button
        table()
      })
    
    library(shiny)
    library(leaflet)
    library(dplyr)
    
    
    
    #### UI
    ui <- fluidPage(
    
      titlePanel("Map"),
    
      leafletOutput("map"),
    
      fluidRow(
        column(2, offset = 0, style='padding:10px;',
               radioButtons("data.type", "Type of  data", c("Localisations", "Micro", "Macro"))),
        column(2, offset = 0, style='padding:10px;',
               uiOutput("position")),
        column(2, offset = 0, style='padding:10px;',
               uiOutput("kind"))
      ),
    
      dataTableOutput("table"), ## to check the filtering
      textOutput("kind.of")
    )
    
    
    server <- function(input, output, session) {
    
      ##### data #####
    
    
      sites <- data.frame(Site=c("Site1", "Site2"),
                          Lat=c(54, 56), 
                          Long=c(16, 18)) 
    
    
      micro <- data.frame(Site=c(rep("Site1",4), rep("Site2", 4)),
                          Position=c("Micro_pos1","Micro_pos1", "Micro_pos2", "Micro_pos2"),
                          Kind=rep(c("blue_fiber", "red_fiber"), 4),
                          Amount=c(5, 46, 64, 32, 54, 38, 29, 31) )  
    
      micro <- full_join(micro, sites)
    
      macro <- data.frame(Site=c(rep("Site1",4), rep("Site2", 4)),
                          Position=c("Macro_pos1","Macro_pos1", "Macro_pos2", "Macro_pos2"),
                          Kind=rep(c("Cigarretes", "Pellets"), 4),
                          Amount=c(3, 16, 4, 12, 14, 18, 19, 21) )  
    
      macro <- full_join(macro, sites)    
    
    
      #### dynamic menu ####
    
      ### position
      output$position <- renderUI({
    
    
        switch(input$data.type,
               "Micro"=radioButtons("position", "Micro position:", 
                                                           choices = c("Micro_pos1", "Micro_pos2")),
               "Macro"=radioButtons("position", "Macro position:", 
                                                   choices = c("Macro_pos1", "Macro_pos2"))
        )
      })
    
      ## kind
    
      output$kind <- renderUI({
    
    
        switch(input$data.type,
               "Micro"=checkboxGroupInput("kind", "kind of micro:", 
                                                                 choices = c("blue_fiber", "red_fiber"),
                                                                 selected = c("blue_fiber", "red_fiber")),
               "Macro"=checkboxGroupInput("kind", "kind of macro:", 
                                                         choices = c("Cigarretes", "Pellets"),
                                                         selected=c("Cigarretes", "Pellets"))
        )
      })
    
    
    #### reactive table to filter data to map ####
    
    table <- reactive({
    
    
        if(input$data.type=="Localisations"){
    
          return(sites)
        }  
    
        else if (input$data.type=="Micro") {
    
          if (is.null(input$position))
            return(NULL)
          if (!is.null(input$position))
    
            micro <-micro[micro$Position==input$position,]
          micro<-micro[micro$Kind %in% input$kind,]
    
          micro <- micro %>% 
            group_by(Site, Lat, Long, Position)%>%
            summarise(Amount=sum(Amount))
          micro
          }
    
      else if (input$data.type=="Macro") {
    
        if (is.null(input$position))
          return(NULL)
        if (!is.null(input$position))
    
          macro <-macro[macro$Position==input$position,]
        macro<-macro[macro$Kind %in% input$kind,]
    
         macro <- macro %>%
          group_by(Site, Lat, Long, Position)%>%
          summarise(Amount=sum(Amount))
        macro
        }
      })
    
    
    #### table with filtered data ####
    output$table <- renderDataTable({
      table()
    })
    
    
    #### base map ####
    
    
      output$map <- renderLeaflet({
        leaflet(sites) %>%
          setView(lat=55, lng=17, zoom=6) %>%
          addProviderTiles(providers$Esri.WorldImagery) %>%
          addCircleMarkers(lng=~Long, lat=~Lat, label=~Site, 
                           labelOptions = labelOptions(noHide =T))
      })
    
    
      #### updating the map #####
    
       observeEvent( c( input$data.type, input$position, input$kind), {
    
    
    
      if(input$data.type=="Localisations"){
    
          leafletProxy("map", data=sites) %>%
            clearMarkers() %>%
            clearShapes()%>%
            addCircleMarkers(lng=~Long, lat=~Lat, label=~Site,
                             labelOptions = labelOptions(noHide =T), fillColor = "red")
        }
    
    
         else if (input$data.type=="Micro") {
    
            if (is.null(input$position))
               return(NULL)
             if (is.null(input$kind))
              return(NULL)
    
    ###########################################################
      ####### and here 4 new line that did all the job #########
           if (input$position %in% c("Macro_pos1", "Macro_pos2")) 
             return(NULL) ## new line
           if (input$kind %in% c("Cigarretes", "Pellets"))
             return(NULL)
      #################################################### 
    
    
    
            leafletProxy("map", data=table()) %>%
            clearMarkers() %>%
            clearShapes()%>%
    
            addCircles(lng=~Long, lat=~Lat, label=~Site, color="white", fill="white",
                       labelOptions = labelOptions(noHide =T),
                       radius = ~Amount*1000) %>%
            addLabelOnlyMarkers(lng=~Long, lat=~Lat, label=~as.character(Amount),
                                labelOptions = labelOptions(noHide = T, direction = 'top', textOnly = T, textsize="20px"))
    
               }
    
         else if(input$data.type=="Macro") {
    
           if (is.null(input$position))
             return(NULL)
           if (is.null(input$kind))
             return(NULL)
    
    ###########################################################
      ####### and here 4 new line that did all the job #########
           if (input$position %in% c("Micro_pos1", "Micro_pos2"))
             return(NULL)
           if (input$kind %in% c("blue_fiber", "red_fiber"))
             return(NULL)
      ##############################################
    
           leafletProxy("map", data=table()) %>%
             clearMarkers() %>%
             clearShapes()%>%
    
             addCircles(lng=~Long, lat=~Lat, label=~Site, color="white", fill="white",
                        labelOptions = labelOptions(noHide =T),
                        radius = ~Amount*1000) %>%
             addLabelOnlyMarkers(lng=~Long, lat=~Lat, label=~as.character(Amount),
                                 labelOptions = labelOptions(noHide = T, direction = 'top', textOnly = T, textsize="20px"))
    
         }
    
          })
    
      ### server end
    }
    
    # Run the application 
    shinyApp(ui = ui, server = server)