【问题标题】:igraph data manipulation within Shiny reactiveShiny 反应式中的 igraph 数据操作
【发布时间】:2015-05-18 00:23:33
【问题描述】:

我正在尝试在 Shiny 中处理数据以生成可由 igraph 处理的图形数据框,但我正在努力处理嵌套数据转换。

我会收到以下错误消息

“plot.igraph(g3, layout = layout.mds) 中的错误:不是图形对象”

有什么想法吗?

这是服务器

library(shiny)
library(igraph)


# Define a server for the Shiny app
shinyServer(function(input, output) {


  g4 <- reactive({

  g2 = gtest[gtest[3]==input$article,]
  g2 = g2[order(g2[3],decreasing = TRUE), ]
  g2 = graph.data.frame(g2[1:5,2:3], directed=TRUE)
  return(g2)

 })



  # Fill in the spot we created for a plot

  output$g3plot <- renderPlot({

    #render network graph

    plot.igraph(g4,layout=layout.mds)
  })
})

这是用户界面

library(shiny)
gtest<-as.data.frame(cbind(c(1:10),c(11:20), c(21:30))

# Define the overall UI
shinyUI(

  # Use a fluid Bootstrap layout
  fluidPage(    

    # Give the page a title
    titlePanel("Articles by similarities"),

    # Generate a row with a sidebar
    sidebarLayout(      
      # Define the sidebar with one input
      sidebarPanel(
        selectInput("article", "Article:", choice=gtest[1])),
        hr()
      ),

    # Create a spot for the plot
      mainPanel(
      plotOutput("g3plot")  
      )

    )
  )

【问题讨论】:

  • 您应该阅读reactivity in Shiny 上的文章。 g2input$article 有反应,但 g3g4 不会监听对 g2 的更改。为什么不把整个东西放在一个反应​​块中?
  • 是的,我试过了,但是不行 g4
  • 您已尝试编辑您的问题,但您的代码发生了任意更改,您现在使用isolate - 为什么?您遇到的错误以及您在我的回答中看到的错误与 Shiny 无关。您传递给plot.igraph 的对象不是igraph 对象,因此plot.igraph 无法处理。
  • graph.data.frame(g2[1:5,2:3],directed=TRUE) 不是 igraph 对象?
  • 如果您阅读了我链接到的文章,您就会理解这个问题。我的代码中的g3 不是igraph 对象,但g3() 是。检查以下答案中的代码是否解决了您的问题。

标签: r shiny


【解决方案1】:

您的代码在语法上确实存在很多问题,并且在某些地方我无法理解逻辑。我已经清理了代码,使其成为 MWE。

library(shiny)
library(igraph)

gtest = data.frame(cbind(Article = c(1:10), from = c(11:20), to = c(21:30)))
runApp(list(
  ui = shinyUI(
    fluidPage(    
      titlePanel("Articles by similarities"),
      sidebarLayout(      
        sidebarPanel(
          selectInput("article", "Article:", choice = gtest$Article)
        ),
        mainPanel(
          plotOutput("g3plot")  
        )
      )
    )),

    # Define a server for the Shiny app
    server = function(input, output) {
      g3 = reactive({
        g2 = gtest[gtest$Article==input$article,]
        g2 = g2[order(g2[[3]],decreasing = TRUE), ]
        graph.data.frame(g2[1:5,2:3], directed=TRUE)
      })


      # Fill in the spot we created for a plot

      output$g3plot = renderPlot({
        plot.igraph(g3(), layout=layout.mds)
      })
    })
)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-17
    • 2019-12-02
    • 2019-04-07
    • 2018-06-23
    • 1970-01-01
    • 2015-08-07
    • 1970-01-01
    相关资源
    最近更新 更多