在Shiny反应中的igraph数据操作

时间:2015-05-17 13:19:55

标签: r shiny

我正在尝试处理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)
  })
})

这是UI

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")  
      )

    )
  )

1 个答案:

答案 0 :(得分:4)

你的代码在语法上确实存在很多问题,在某些地方我无法理解逻辑。我已经清理了代码,因此它是一个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)
      })
    })
)