【发布时间】:2020-08-24 17:32:14
【问题描述】:
朋友们可以帮助我编写下面的闪亮代码。它是用于操作的可执行代码。我设法正常生成散点图,它根据我的 SliderInput 变化。就我而言,我正在生成集群。如果滑块输入选择为 5,散点图将生成 5 个聚类,依此类推。这里一切都很好。我还在滑块输入下方做了一个 selectInput 来显示特定集群的地图。但是,我无法为特定集群生成散点图,也就是说,如果它在我的 selectInput 中选择了 2,我希望它只显示集群 2 的地图。您能帮我解决这个问题吗?
library(shiny)
library(ggplot2)
library(rdist)
library(geosphere)
library(kableExtra)
library(readxl)
library(tidyverse)
library(DT)
library(shinythemes)
function.cl<-function(df,k,Filter1,Filter2,Filter3){
#database df
df<-structure(list(Properties = c(1,2,3,4,5),
Latitude = c(-23.8, -23.8, -23.9, -23.9, -23.9),
Longitude = c(-49.6, -49.6, -49.6, -49.6, -49.6),
Waste = c(526, 350, 526, 469, 285)), class = "data.frame", row.names = c(NA, -5L))
#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
#database df1
df1<-df[c("Latitude","Longitude")]
df1$cluster<-clusters
#Table to join df and df1
data_table <- Reduce(merge, list(df, df1))
#Scatter Plot for all
suppressPackageStartupMessages(library(ggplot2))
g<-ggplot(data=df1, aes(x=Longitude, y=Latitude, color=factor(clusters))) + geom_point(aes(x=Longitude, y=Latitude), size = 4)
plotGD<-g
#Scatter Plot for specific cluster
suppressPackageStartupMessages(library(ggplot2))
g<-ggplot(data=df1[df1$cluster == Filter3,], aes(x=Longitude, y=Latitude, color=factor(clusters))) + geom_point(aes(x=Longitude, y=Latitude), size = 4)
plotGD1<-g
return(list(
"Plot" = plotGD,
"Plot1" = plotGD1,
"Data"=data_table
))
}
ui <- bootstrapPage(
navbarPage(theme = shinytheme("flatly"), collapsible = TRUE,
"Cl",
tabPanel("Solution",
sidebarLayout(
sidebarPanel(
radioButtons("filter1", h3("Select properties"),
choices = list("All properties" = 1,
"Exclude properties" = 2),
selected = 1),
radioButtons("filter2", h3("Select properties"),
choices = list("All properties" = 1,
"Exclude properties" = 2),
selected = 1),
tags$hr(),
tags$b(h3("Satisfied?")),
tags$b(h5("(a) Choose other filters")),
tags$b(h5("(b) Choose clusters")),
sliderInput("Slider", h5(""),
min = 2, max = 5, value = 3),
),
mainPanel(
tabsetPanel(
tabPanel("Solution", plotOutput("ScatterPlot"))))
))),
tabPanel("",
sidebarLayout(
sidebarPanel(
selectInput("Filter3", label = h4("Select just one cluster to show"),""),
),
mainPanel(
tabsetPanel(
tabPanel("Map", plotOutput("ScatterPlot1"))))
)))
server <- function(input, output, session) {
Modelcl<-reactive(function.cl(df,input$Slider,1,1,input$Filter3))
output$ScatterPlot <- renderPlot({
Modelcl()[[1]]
})
output$ScatterPlot1 <- renderPlot({
Modelcl()[[2]]
})
observeEvent(c(df,input$Slider,1,1),{
abc <- req(Modelcl()$Data)
updateSelectInput(session,'Filter3',
choices=sort(unique(abc$cluster)))
})
}
shinyApp(ui = ui, server = server)
非常感谢!
【问题讨论】:
-
谢谢本!我会试着做你说的调整,看看我能不能得到一些东西。抱歉,在这种情况下,我没有做一个最小的例子,我没有太多的闪亮技能,但我会尝试根据你的建议进行调整。非常感谢
-
本,我尽可能地进步了。该代码是可执行的。你可以看看。谢谢朋友
-
对不起,本,我错了。这是df1。我更新了上面的代码,我相信这是你想让我测试的。我为所有人制作了一个ggplot,为一个特定的集群制作了一个ggplot。但是,为特定集群生成地图时出现错误:错误:美学必须是长度 1 或与数据相同 (2):颜色。