首先,我將做一個簡單的例子讓你理解這個想法。
library(googleway)
set_key( "API KEY")
df<-structure(list(Properties = c(1,2,3,4),
Latitude = c(-24.930473, -24.95575,-24.990473, -24.99575),
Longitude = c(-49.994889, -49.990162,-49.999889, -49.999162),
cluster = c(1,2,1,2)), class = "data.frame", row.names = c(NA, -4L))
df1<-structure(list(Latitude = c(-24.924361,-24.95575),
Longitude = c(-50.004343, -50.007371),
cluster = c(1,2)), class = "data.frame", row.names = c(NA, -2L))
# considering `Filter1= 1`, that is, cluster 1 that I want to see, and `Filter2= 3`, that is property number 3, so:
Filter1=1
Filter2=3
data_table1<-df1[df1$cluster==Filter1,c(1:2)]
data_table2<-df[df$Properties==Filter2,c(2:3)]
#Generate the map with routes
df2<-google_directions(origin = data_table1, destination = data_table2,
mode = "driving")
df_routes <- data.frame(polyline = direction_polyline(df2))
m1<-google_map() %>%
add_polylines(data = df_routes, polyline = "polyline")

現在,我的想法是使用閃亮的。從這個意義上說,我創建了兩個selecInput,第一個對應Filter 1(你想看到哪個集群),另一個對應Filter 2(你想看到哪個屬性)。選擇這兩條資訊會生成路線,就像我在上面的示例中所做的那樣。但是,我無法解決這個問題。
這個問題非常相似,可能會有所幫助:
uj5u.com熱心網友回復:
您作為三個輸入的閃亮示例:
Sliderk,在函式簽名中傳遞給Filter1,這似乎與(即選擇一個集群)相同,并且當前在函式簽名中k傳遞給Filter1Filter2,用于選擇屬性,但永遠不會傳遞給函式。
我認為你想要做的只是包括Slider和Filter2(或Filter1和Filter2)。
然后更改Modelcl為
Modelcl<-reactive({
function.cl(df,df1,input$Slider,input$Filter2)
})
然后,在您的實際函式中,重命名簽名中的輸入,以便它們與您傳入的內容更好地對齊。我建議這樣:
function.cl<-function(df,df1,cluster,property){
那么你就可以
data_table1<-df1[df1$cluster==cluster,c(1:2)]
data_table2<-df[df$Properties==property,c(2:3)]
最后,請注意您在呼叫google_directions(). 您需要確保origin和destination引數不同。目前,它們都設定為data_table1
這是一個示例,進行了更改:
library(shiny)
library(rdist)
library(geosphere)
library(shinythemes)
library(googleway)
function.cl<-function(df,df1,cluster,property){
#database df
df<-structure(list(Properties = c(1,2,3,4),
Latitude = c(-24.930473, -24.95575,-24.990473, -24.99575),
Longitude = c(-49.994889, -49.990162,-49.999889, -49.999162),
cluster = c(1,2,1,2)), class = "data.frame", row.names = c(NA, -4L))
df1<-structure(list(Latitude = c(-24.924361,-24.95575),
Longitude = c(-50.004343, -50.007371),
cluster = c(1,2)), class = "data.frame", row.names = c(NA, -2L))
data_table1<-df1[df1$cluster==cluster,c(1:2)]
data_table2<-df[df$Properties==property,c(2:3)]
#Generate the map with routes
df2<-google_directions(origin = data_table1, destination = data_table2,
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" = df
))
}
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 = 1, max = 2, value = 1,step = 1),
selectInput("Filter2", label = h4("Select the properties"),choices =c(1,2,3,4)),
),
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$Filter2)
})
output$G2 <- renderGoogle_map({
Modelcl()[[1]]
})
observeEvent(input$Slider, {
abc <- req(Modelcl()$Data)
updateSelectInput(session,'Filter1',
choices=sort(unique(abc$cluster)))
})
# observeEvent(c(input$Filter1 ),{
# abc <- req(Modelcl()$Data) %>% filter(cluster == as.numeric(input$Filter1))
# updateSelectInput(session,'Filter2',
# choices = unique(abc$Propertie))
#
# })
}
shinyApp(ui = ui, server = server)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/451364.html
上一篇:自定義谷歌地圖示記顫振
下一篇:合并谷歌方向服務和方向渲染結果
