【问题标题】:isolate vs reactive in shiny在闪亮中隔离与反应
【发布时间】: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))

  }


})
} 
)

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    我的回答(也感谢 Nicola):

    UI.R

    shinyUI(fluidPage(
    titlePanel("Visualize and compare centrality measures"),
    
    sidebarLayout(
    sidebarPanel(
    
      selectInput("format", label = h4("Graph format"), 
                  choices = c("gml", "graphml", "edgelist", "pajek", "ncol", "lgl",
                              "dimacs", "graphdb", "dl")),
    
      br(),
    
      fileInput("graph", label = h4("Graph file")),
    
      br(),
    
    
      selectInput("centrality", label = h4("Centrality measure"), 
                  choices = c("degree", "eigenvector", "Katz", "pagerank")),
    
      br(),
    
      conditionalPanel(
        condition = "input.centrality == 'Katz' || input.centrality == 'pagerank'",
        numericInput("alpha", 
                     label = h4("Damping"), min=0, max = 1, step = 0.05, 
                     value = 0.85), br()),
    
    
      actionButton("plot", "Visualize")
    
    
      ),
    mainPanel(plotOutput("net"))
    )
    ))
    

    Server.R

    library(igraph)
    map = function(k,x,y,w,z) {if (abs(x - y) > 10^-3) w + (z-w) * (k-x)/(y-x) else (w+z)/2}
    
    shinyServer(
    function(input, output) {
    
    # read and cache the graph
    inputGraph = reactive({
      inFile = input$graph
      if (!is.null(inFile))
        read.graph(file=inFile$datapath, format=input$format)
    })
    
    # read and cache the layout
    inputCoords = reactive({layout.fruchterman.reingold(inputGraph())});
    
    # render the plot
    output$net = renderPlot({
      # viualize button
      input$plot
      # isolate input graph
      g = isolate(inputGraph())
    
      if (!is.null(g)) {      
        coords = isolate(inputCoords())
        cent = isolate(switch(input$centrality, 
                      "degree" = degree(g),
                      "eigenvector" = evcent(g)$vector,
                      "Katz" = alpha.centrality(g, alpha = input$alpha),
                      "pagerank" = page.rank(g, damping = input$alpha)$vector))
    
        top = which.max(cent)
        V(g)$color = "skyblue"
        V(g)[top]$color = "red"
    
        plot(g, layout=coords, vertex.label=NA, vertex.size = map(cent, min(cent), max(cent), 1, 10))
      }      
    })
    }
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-16
      • 1970-01-01
      • 2020-12-18
      相关资源
      最近更新 更多