调整闪亮的新功能
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了调整闪亮的新功能相关的知识,希望对你有一定的参考价值。
下面的可执行代码正在生成簇,并显示在传单所形成的地图上(我在下面插入了我闪亮的视觉结构图)。但是,我需要有关我的Filter2的帮助,它只是为了显示在Filter1中选择的群集中插入的属性。但是,例如,将sliderInput视为3。群集1具有3个属性,但是显示了7个属性的选择(下面的错误)。因此,我希望您能帮助我调整Filter2,以便只能从Filter1中选择的群集中显示地图上的属性。
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<-df1[df1$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)) %>%
addLegend( position = "topright", title="Cluster", colors = ai_colors[1:max(df$cluster)],labels = unique(df$cluster))
plot1<-m1
# Map for specific cluster and propertie
m2<-leaflet(df_spec_clust) %>% addTiles() %>%
addMarkers(~Longitude, ~Latitude, icon = leafIcons) %>%
addAwesomeMarkers(lat=~df_spec_prop$Latitude, lng = ~df_spec_prop$Longitude, icon=icons, label=~as.character(df$cluster))
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)
updateSelectInput(session,'Filter2',
choices=sort(unique(abc$Properties)))
)
shinyApp(ui = ui, server = server)
我闪亮的视觉结构
错误
非常感谢!
答案
这是因为未过滤该函数返回的data_table。
您可以在observeEvent
中对其进行过滤:
abc <- req(Modelcl()$Data) %>% filter(cluster == as.numeric(input$Filter1))
如果这样,您可以使用observe
进行简化:
observe(
abc <- req(Modelcl()$Data) %>% filter(cluster == as.numeric(input$Filter1))
updateSelectInput(session,'Filter2',
choices=sort(unique(abc$Properties)))
)
以上是关于调整闪亮的新功能的主要内容,如果未能解决你的问题,请参考以下文章