简体   繁体   English

闪亮的 visNetwork 条件布局 - 绕过不兼容的选项

[英]Shiny visNetwork conditional layout - getting around incompatible options

I'm building a Shiny app displaying interactive networks with visNetwork and I'd like to give users the option to choose between two igraph layouts.我正在构建一个使用visNetwork显示交互式网络的 Shiny 应用程序,我想让用户可以选择在两个igraph布局之间进行选择。 The problem is the additional argument required for one layout is an unused argument for the other and I can't seem to get rid of it conditionally.问题是一个布局所需的附加参数是另一个未使用的参数,我似乎无法有条件地摆脱它。

library(igraph)
library(shiny)
library(visNetwork)

# generate data
iGr <- make_graph("Meredith")
visGr <- toVisNetworkData(iGr)

# ui
ui <- fluidPage(
  # layout options
  radioButtons(
    inputId="grLayout",
    label="View in:",
    choices=list(
      "Graph layout"="layout_with_graphopt",
      "Ellipse layout"="layout_in_circle"
    ),
    selected="layout_with_graphopt"
  ),
  visNetworkOutput("netPlot")
)

# server
server <- function(input, output){
  output$netPlot <- renderVisNetwork({
    visNetwork(visGr$nodes, visGr$edges) %>%
      visIgraphLayout(physics=FALSE, type="square", layout=input$grLayout,
                      charge=0.1 # <- here's my problem
                      )
  })  
}

shinyApp(ui=ui, server=server)

I need the charge argument for the layout_with_graphopt option with my real data to make the the visualization readable, but it throws an "unused argument (charge = 0.1)" error with the layout_in_circle option.我需要我的真实数据的layout_with_graphopt选项的charge参数以使可视化可读,但它会引发“未使用的参数 (charge = 0.1)”错误与layout_in_circle选项。 I've tried the two variations without success.我试过这两种变体都没有成功。

This one won't run at all and throws an 'argument "graph" is missing, with no default' error:这个根本不会运行并抛出“缺少参数“图形”,没有默认错误:

  output$netPlot <- renderVisNetwork({
    visNetwork(visGr$nodes, visGr$edges) %>%
      {if (input$grLayout=="layout_with_graphopt") {
        visIgraphLayout(physics=FALSE, type="square", layout=input$grLayout,
                        charge=0.1)
      } else {
        visIgraphLayout(physics=FALSE, type="square", layout=input$grLayout)
      }}
  })

This one throws an "unused argument (charge = NULL)" error for the layout_in_circle option.这会为layout_in_circle选项引发“未使用的参数(charge = NULL)”错误。 (I get something similar if I try NA ): (如果我尝试NA我会得到类似的结果):

  output$netPlot <- renderVisNetwork({
    chargeVal <- if (input$grLayout=="layout_with_graphopt") {0.1} else {NULL}
    visNetwork(visGr$nodes, visGr$edges) %>%
      visIgraphLayout(physics=FALSE, type="square", layout=input$grLayout,
                      charge=chargeVal)
  })

I'd rather not do an if else on the whole renderVisNetwork block because the real one is quite lengthy with a fair number of bells and whistles, and the charge argument would be the only difference between them.我宁愿不在整个renderVisNetwork块上执行if else ,因为真正的块相当冗长,有相当多的花里胡哨,而且charge论点将是它们之间的唯一区别。

I figured it out.我想到了。 The trick is to stick everything except the visIgraphLayout lines into a visNetwork object, then pipe conditional versions of visIgraphLayout onto the back of it:诀窍是将visIgraphLayout以外的所有内容visIgraphLayoutvisNetwork对象中,然后将visNetwork条件版本通过管道visIgraphLayout到其背面:

  output$netPlot <- renderVisNetwork({
    plot1 <- visNetwork(visGr$nodes, visGr$edges)
    # can add whatever other bells and whistles needed before if/else if
    # conditional layout:
    if (input$grLayout=="layout_with_graphopt"){
      plot2 <- plot1 %>%
        visIgraphLayout(physics=FALSE, type="square", layout=input$grLayout,
                        charge=0.1)
    } else if (input$grLayout=="layout_in_circle") {
      plot2 <- plot1 %>%
        visIgraphLayout(physics=FALSE, type="square", layout=input$grLayout)
    }
    return(plot2)
  })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM