R:多个下拉菜单

问题描述 投票:0回答:1

我在 R 中制作了以下网络:

library(igraph)
library(reshape2)

north_american_cities <- c("New York", "Los Angeles", "Chicago", "Houston", "Phoenix")
european_cities <- c("London", "Berlin", "Madrid", "Rome", "Paris")
asian_cities <- c("Tokyo", "Delhi", "Shanghai", "Beijing", "Mumbai")

n <- 30

set.seed(123) 

na_cities_sample <- sample(north_american_cities, n, replace = TRUE)
eu_cities_sample <- sample(european_cities, n, replace = TRUE)
as_cities_sample <- sample(asian_cities, n, replace = TRUE)

df <- data.frame(NorthAmerica = na_cities_sample,
                 Europe = eu_cities_sample,
                 Asia = as_cities_sample,
                 stringsAsFactors = FALSE)

df <- df[!duplicated(df), ]

edges_df <- data.frame(from = c(df$NorthAmerica, df$Europe),
                       to = c(df$Europe, df$Asia))

edge_list <- as.matrix(edges_df)

g <- graph_from_edgelist(edge_list, directed = TRUE)
plot(g, vertex.size=10, vertex.label.cex=0.8, edge.arrow.size=0.5)

enter image description here

然后我添加了一个下拉菜单,用于使用 visNetwork 进行导航:

    library(visNetwork)
nodes_df <- data.frame(id = unique(c(df$NorthAmerica, df$Europe, df$Asia)), 
                       group = c(rep("North America", length(unique(df$NorthAmerica))),
                                 rep("Europe", length(unique(df$Europe))),
                                 rep("Asia", length(unique(df$Asia)))))

edges_df <- data.frame(from = c(df$NorthAmerica, df$Europe),
                       to = c(df$Europe, df$Asia))

colors <- c('North America' = 'red', 'Europe' = 'green', 'Asia' = 'blue')

visNetwork(nodes_df, edges_df) %>%   
    visNodes(color = list(background = colors[nodes_df$group])) %>%
    visEdges(arrows = 'to') %>%
    visOptions(highlightNearest = TRUE, nodesIdSelection = TRUE) %>%
    visInteraction(navigation = "zoom") %>%
    visInteraction(navigation = "drag")

enter image description here

我的问题:我正在尝试修改上面的代码以创建 3 个下拉菜单,这将允许某人选择一个北美城市并查看可以采取的所有可能的旅程 - 然后单击欧洲城市以进一步缩小范围下来:

enter image description here

我尝试在线搜索示例,但 R 中的 visNetwork 似乎不允许使用多个下拉菜单的选项。

但我不确定如何实现其中任何一个。

有人可以告诉我如何在 R 中添加这 3 个下拉菜单吗?

谢谢!

r shiny igraph visnetwork
1个回答
0
投票

从文档中,您可能会发现通过设置组并在 visOptions 中使用 selectedBy 在 visNetwork 中取得了一些成功。

对于闪亮来说,visNetworkProxy 是值得考虑的,因为它看起来也可以轻松重绘。

自从我在闪亮中使用它以来已经有一段时间了,但是为了让你的问题更好,建立一个简单的闪亮应用程序作为起点可能是值得的。也许在第一个下拉列表中选择特定 ID 时将 visNetworkProxy 与 ObserveEvent 配对将启用第二个下拉列表的选项过滤,依此类推。

© www.soinside.com 2019 - 2024. All rights reserved.