【发布时间】:2014-11-28 15:42:33
【问题描述】:
我正在构建一个简单的 闪亮 应用程序来显示用户选择的网络上的中心性度量。用户选择网络格式、网络文件和要计算的中心度度量,然后单击按钮来布局和可视化图形,其中节点的大小与节点的中心度成正比。在renderPlot 输出中,我隔离了除按钮之外的所有输入,因此只有在用户选择了相关信息(格式、文件、度量)并单击可视化按钮时才完成计算。它可以工作,但存在以下问题:每次单击可视化按钮时,都会重新计算布局并以不同的方式可视化网络。我想避免这种行为。我试图在reactive 调用中包含布局计算(请参阅注释代码),但随后隔离失败。
因此,我能够隔离,但不能使代码反应性,或者使其反应性而不隔离。
界面代码如下:
shinyUI(fluidPage(
titlePanel("Visualize and compare centrality measures"),
sidebarLayout(
sidebarPanel(
selectInput("format", label = h4("Select graph format"),
choices = c("gml", "graphml", "edgelist", "pajek", "ncol", "lgl",
"dimacs", "graphdb", "dl")),
br(),
fileInput("graph", label = h4("Load graph")),
br(),
selectInput("centrality", label = h4("Select centrality measure"),
choices = c("degree", "eigenvector", "Katz")),
br(),
conditionalPanel(
condition = "input.centrality == 'Katz'",
numericInput("alpha",
label = h4("Alpha"), min=0, step = 0.05,
value = 1)),
actionButton("plot", "Visualize")
),
mainPanel(plotOutput("net"))
)
))
服务器代码如下:
library(igraph)
# mapping k from [x,y] in [w,z] with f(k) = w + (z-w) * (k-x)/(y-x)
map = function(k,x,y,w,z) {if (x != y) w + (z-w) * (k-x)/(y-x) else (w+z)/2}
shinyServer(
function(input, output) {
# read input graph
# g = reactive({read.graph(file=input$graph$datapath, format=input$format)});
# compute layout
# coords = reactive({layout.fruchterman.reingold(g)});
output$net = renderPlot({
if (input$plot > 0) {
g = isolate(read.graph(file=input$graph$datapath, format=input$format))
coords = layout.fruchterman.reingold(g)
cent = isolate(switch(input$centrality,
"degree" = degree(g),
"eigenvector" = evcent(g)$vector,
"Katz" = alpha.centrality(g, alpha = input$alpha)))
plot(g, layout=coords, vertex.label=NA, vertex.size = map(cent, min(cent), max(cent), 1, 10))
}
})
}
)
【问题讨论】: