簡體   English   中英

使用leafletProxy()在Shiny中使用多個反應滑塊

[英]Multiple Reactive Sliders in Shiny using leafletProxy()

我是R map的初學者,我正在嘗試構建一個Shiny應用程序,以顯示英國所有大學的學生滿意度和大學排名。

通過傳單,我已經用標記繪制了大學位置,並添加了帶彈出窗口的滑塊,以查看學生滿意度分數和排名(見截圖)。

我們的想法是能夠在滑塊上選擇一組值(例如“滿意度從80到90”和“從1到30排名”,應用程序只會顯示符合這兩個標准的值。

問題在於有多個反應滑塊。 如果我按照與“滿意”滑塊相同的方式對“排名”滑塊進行編碼,則“滿意”滑塊將采用“排名”值而不是兩個獨立工作的滑塊。

到目前為止,您可以在下面看到我的代碼,其中包含它的外觀和數據的截圖(“排名”滑塊的實驗部分已注釋,因此它們不會干擾)。

任何提示如何繼續,以便兩個滑塊不相互取值?

非常感謝,如果問題非常基本,那就很抱歉。

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


        mapData <- read.csv("~/Desktop/Shiny app/Csv Shiny Data Clean.csv") %>%
          filter(!is.na(Latitude) & !is.na(Longitude))


     ui <- bootstrapPage(
          tags$style(type = "text/css", "html, 
                     body {width:100%;height:100%}"),

          leafletOutput("uniSmap", width = "100%", height = "100%"),


     #slider for student satisfaction

      absolutePanel(
            top = 50,
            right = 50,
            sliderInput(
              "range",
              "Satisfaction Score",
              min = 1,
              max = 100,
              value = round(range(mapData$Satisfaction.....2016.Registered,   na.rm = TRUE), 1),
              step = 1
            )
          ),


     #slider for Ranking

      absolutePanel(
            top = 200,
            right = 50,
            sliderInput(
              "range",
              "QS University Ranking",
              min = 1,
              max = 128,
              value = round(range(mapData$QS.Ranking, na.rm = TRUE), 1),
              step = 1
            )
          ),

          #bottom right title
          absolutePanel(
            bottom = 10,
            left = 10,
            "Satisfaction Map 2016"
          )
          )


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

          filteredData <- reactive({
            mapData %>%
              filter(Satisfaction.....2016.Registered >= input$range[1] &
                       Satisfaction.....2016.Registered <= input$range[2]) 
          })


            #question here: can I just do the same for Ranking Data (as below)?

            # filteredDataRanking <- reactive({
            #   mapData %>%
            #     filter(QS.Ranking >= input$range[1] &
            #              QS.Ranking <= input$range[2]) 
            # })


      output$uniSmap <- renderLeaflet({
            # as the map is only drawn once
            # use non-reactive dataframe, mapData 
            leaflet(mapData) %>%
              addTiles() %>%
              fitBounds(~min(Longitude), ~min(Latitude), 
                        ~max(Longitude), ~max(Latitude))
          })

          # Incremental changes to the map performed in an observer.

          observe({

            leafletProxy("uniSmap", data = filteredData()) %>%

              clearShapes() %>% 
              clearPopups() %>% 
              clearMarkers() %>%

              addMarkers(lat = ~Latitude,
                         lng = ~Longitude,
                         popup = ~paste(
                           Institution,
                           "<br>",
                           "Overall Satisfaction:",
                           Satisfaction.....2016.Registered,
                           "<br>"
                         )
              )

            }) #end of observe for satisfaction


          #would I have to create another observe for ranking data (as below)?

          # observe({
          #   
          #   leafletProxy("uniSmap", data = filteredDataRanking()) %>%
          #     
          #     clearShapes() %>% 
          #     clearPopups() %>% 
          #     clearMarkers() %>%
          #     
          #     addMarkers(lat = ~Latitude,
          #                lng = ~Longitude,
          #                popup = ~paste(
          #                  Institution,
          #                  "<br>",
          #                  "QS University Ranking",
          #                  QS.Ranking,
          #                  "<br>"
          #                )
          #     )
          #   
          # }) #end of observe for Ranking






        } #end of server description

    shinyApp(ui = ui, server = server)






#License: thanks to Stephen McDaniel, from whom a substantial portion of this code is Copyright by ((c) 2017 Stephen McDaniel)

該應用的屏幕截圖

鏈接到已使用的數據

重命名每個滑塊satisfactionranking您必須在同一過濾器中使用這兩個范圍,以便應用所有條件:

filteredData <- reactive({
    mapData %>%
        filter(Satisfaction.....2016.Registered >= input$satisfaction[1] &
               Satisfaction.....2016.Registered <= input$satisfaction[2]) &
               QS.Ranking >= input$ranking[1] &
               QS.Ranking <= input$ranking[2]) 
    })

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM