[英]How to Link selected cluster in shiny app
首先,我將在下面提供一個示例,讓您了解它是如何工作的。 下面的代碼生成兩個坐標之間的路線。
library(googleway)
set_key( "API KEY")
#databases
df<-structure(list(Properties = c(1,2),
Latitude = c(-24.930473, -24.95575),
Longitude = c(-49.994889, -49.990162),
cluster = c(1,2)), class = "data.frame", row.names = c(NA, -2L))
df1<-structure(list(Properties = c(3,4),Latitude = c(-24.924361,-24.95575),
Longitude = c(-50.004343, -50.007371),
cluster = c(1,2)), class = "data.frame", row.names = c(NA, -2L))
#Table to join df and df1
data_table <- rbind(df,df1)
data_table<-data_table[c(2:3)]
df2<-google_directions(origin = data_table[1,], destination = data_table[3,],
mode = "driving") #I specified properties 1 and 3 which are from cluster 1
df_routes <- data.frame(polyline = direction_polyline(df2))
m1<-google_map() %>%
add_polylines(data = df_routes, polyline = "polyline")
現在,我嘗試在 Shiny 中執行此操作,但沒有像我在上面的示例中那樣指定屬性的坐標。 在這個意義上,我創建了一個selecInput
到 select 我想查看路由的哪個集群。 如何在下面的代碼中對此進行調整?
這個問題可以幫助: Adjust new features in shiny
library(shiny)
library(rdist)
library(geosphere)
library(shinythemes)
library(googleway)
set_key( "API KEY")
function.cl<-function(df,df1,k,Filter1){
#database df
df<-structure(list(Properties = c(1,2),
Latitude = c(-24.930473, -24.95575),
Longitude = c(-49.994889, -49.990162),
cluster = c(1,2)), class = "data.frame", row.names = c(NA, -2L))
df1<-structure(list(Properties = c(3,4),Latitude = c(-24.924361,-24.95575),
Longitude = c(-50.004343, -50.007371),
cluster = c(1,2)), class = "data.frame", row.names = c(NA, -2L))
#Table to join df and df1
data_table <- rbind(df,df1)
data_table1<-data_table[c(2:3)]
#Generate the map with routes
df2<-google_directions(origin = data_table1[1,], destination = data_table1[3,],
mode = "driving")
df_routes <- data.frame(polyline = direction_polyline(df2))
m1<-google_map() %>%
add_polylines(data = df_routes, polyline = "polyline")
plot1<-m1
return(list(
"Plot1" = plot1,
"Data" = data_table
))
}
ui <- bootstrapPage(
navbarPage(theme = shinytheme("flatly"), collapsible = TRUE,
"Cl",
tabPanel("Map of all clusters",
sidebarLayout(
sidebarPanel(
tags$b(h3("Choose the cluster number?")),
sliderInput("Slider", h5(""),
min = 2, max = 2, value = 2),
selectInput("Filter1", label = h4("Select just one cluster to show"),""),
),
mainPanel(
tabsetPanel(
tabPanel("Solution", (google_mapOutput("G2",width = "95%", height = "600")))))
))))
server <- function(input, output, session) {
Modelcl<-reactive({
function.cl(df,df1,input$Slider,input$Filter1)
})
output$G2 <- renderGoogle_map({
Modelcl()[[1]]
})
observeEvent(input$Slider, {
abc <- req(Modelcl()$Data)
updateSelectInput(session,'Filter1',
choices=sort(unique(abc$cluster)))
})
}
shinyApp(ui = ui, server = server)
如果k
表示您要定位的集群編號,並且cluster
是數據集中的一列(我不明白為什么您有df
和df1
,然后將它們行綁定在一起),那么您可以簡單地執行類似這樣的操作來限制google_directions()
調用的輸入僅是對應於簇k
的行
data_table1<-data_table[data_table$cluster==k,c(2:3)]
然后,在對google_directions()
的調用中,您將執行此操作(注意我現在調用第 1 行和第 2 行(而不是您示例中的第 1 行和第 3 行),因為對data.table$cluster==k
的過濾data.table$cluster==k
上面確保 data.table1 只有與集群k
關聯的兩行
df2<-google_directions(
origin = data_table1[1,],
destination = data_table1[2,],
mode = "driving")
最后,我不確定您是否要返回所有data.table
。 也許您進行調整以使其僅返回data.table1
(即您感興趣的集群的子集)?:
return(list(
"Plot1" = plot1,
"Data" = data_table1
))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.