我正在尝试构建一个 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)
应用程序截图:
重命名每个滑块
satisfaction
和 ranking
后,您必须在同一过滤器中使用这两个范围,以便应用所有条件:
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])
})