如何 Link 在闪亮的应用程序中选择集群
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我想看哪个集群的路由。如何在下面的代码中对此进行调整?
这个问题可以帮助:
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_table1 只有与集群 k
关联的两行
df2<-google_directions(
origin = data_table1[1,],
destination = data_table1[2,],
mode = "driving")
最后,我不确定您是否想要 return 全部 data_table
。也许您进行调整,使其 return 仅 data_table1
(即您感兴趣的集群的子集)?:
return(list(
"Plot1" = plot1,
"Data" = data_table1
))
首先,我将在下面提供一个示例,让您了解它是如何工作的。下面的代码生成两个坐标之间的路线。
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我想看哪个集群的路由。如何在下面的代码中对此进行调整?
这个问题可以帮助:
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_table1 只有与集群 k
df2<-google_directions(
origin = data_table1[1,],
destination = data_table1[2,],
mode = "driving")
最后,我不确定您是否想要 return 全部 data_table
。也许您进行调整,使其 return 仅 data_table1
(即您感兴趣的集群的子集)?:
return(list(
"Plot1" = plot1,
"Data" = data_table1
))