我试图让闪亮在用户单击操作按钮后使用滑动条值进行计算。这些滑动条代表五个维度,用户可以拖动这些维度来设置每个变量的首选权重值。单击操作按钮后,应用程序应显示荷兰地图,其中包含基于这些权重值具有最高“分数”的城市。目前我已经运行了以下代码,但是在单击操作按钮后速度非常慢,因为当反应性滑动条发生变化时它会自动重新执行。是否有人建议让我的代码更快并且操作按钮 eventReactive 以便必须单击它才能让闪亮知道进行新的计算?
因为我想不出一种更简单的方法来共享我的数据(spatialpolygonsdataframe),所以我在主脚本中包含了一个要点链接:https ://gist.github.com/anonymous/602948bce91e61f36fd3eb0c4259d26c
Shiny 应用截图:https ://i.stack.imgur.com/k0Vg9.png
应用程序.R
library(shiny)
library(leaflet)
ui <- fluidPage(
titlePanel("Quality-of-life-o-meter of The Netherlands"),
sidebarLayout(
sidebarPanel(
h3("Dimensions"),
h6("Assign a weight value for every dimension and press the 'find!' button"),
sliderInput("housingslider",
label = h4("Housing"),
min = 1,
max = 5,
value = 1),
sliderInput("populationslider",
label = h4("Population"),
min = 1,
max = 5,
value = 1),
sliderInput("provisionsslider",
label = h4("Provisions"),
min = 1,
max = 5,
value = 1),
sliderInput("safetyslider",
label = h4("Safety"),
min = 1,
max = 5,
value = 1),
sliderInput("physicalenvslider",
label = h4("Physical Environment"),
min = 1,
max = 5,
value = 1),
actionButton("action", label = "Find!")
),
mainPanel(
leafletOutput("mymap", height = "800")
)
)
)
server <- function(input, output, session) {
output$mymap <-renderLeaflet({
# Create interactive map of Total Score of Muncipalities in 2016 to display first
leaflet(data = MunScores2016) %>% addTiles() %>%
addPolygons(fillColor = ~pal(Total_Score_2016),
fillOpacity = 1,
color = 'white',
weight = 1,
popup = popup_dat) %>%
addLegend("bottomright", # Legend position
pal=pal, # color palette
values=~Total_Score_2016, # legend values
opacity = 0.7,
title="Percentage difference from national average")
})
observeEvent(input$action,
output$mymap <-renderLeaflet({
Total_Score <- NA
Total_Score <- ((input$housingslider * MunScores2016$Housing_Score_2016 +
input$populationslider * MunScores2016$Population_Score_2016 +
input$provisionsslider * MunScores2016$Provisions_Score_2016 +
input$safetyslider * MunScores2016$Safety_Score_2016 +
input$physicalenvslider * MunScores2016$PhysicalEnvironment_Score_2016)/
(input$housingslider + input$populationslider + input$provisionsslider + input$safetyslider + input$physicalenvslider))
#Create interactive map
leaflet(data = MunScores2016) %>% addTiles() %>%
addPolygons(fillColor = ~pal(Total_Score),
fillOpacity = 1,
color = 'white',
weight = 1,
popup = paste0("<strong>Municipality:</strong>",
MunScores2016$Municipality_Name,
"<br><strong>Quality-of-life-o-meter says: </strong>",
Total_Score)) %>%
addLegend("bottomright", # Legend position
pal=pal, # color palette
values=~Total_Score_2016, # legend values
opacity = 0.7,
title="Weighted percentage difference from national average")
})
)
}
shinyApp(ui = ui, server = server)