在 R Shiny 中将 Pajek 文件转换为 CSV

Convert Pajek file to CSV in R Shiny

有谁知道如何读取闪亮的pajek文件然后找到每个顶点的度数并按降序输出到CSV文件?我不确定如何从读取图形的 filedata() 方法中获取文件,然后获取 pajek 文件的度数,然后将它们输出到 CSV 文件中,其中最高度数在顶部,最低度数在底部。

Here's 我要导入的 Pajek 文件并将学位导出到 CSV。

在 R 中,我知道如何正常编码:

#read the pajek file in igraph
reponetwork <- read.graph("network.net", format = "pajek")

#Inspect the data:
degree(reponetwork)
sort(degree(reponetwork), decreasing = TRUE)

但我不确定如何在 Shiny 中做到这一点:

这是我目前所做的:

ui.R

shinyUI(fluidPage(
  titlePanel("Finding most influential vertex in a network"),

  sidebarLayout(
    sidebarPanel(

     fileInput("graph", label = h4("Pajek file")),

      downloadButton('downloadData', 'Download')

    ),
    mainPanel( tabsetPanel(type = "tabs", 
                           tabPanel("Table", tableOutput("view")) 

                           ) 

               )

  )
))

server.R

library(igraph)
options(shiny.maxRequestSize=100*1024^2) 

shinyServer(
  function(input, output) {

    filedata <- reactive({
      inFile = input$graph
      if (!is.null(inFile))
      data <- read.graph(file=inFile$datapath, format="pajek")
      return(data)
    })

  #get the pajek file, get the degree from it using degree(),
  #display the output(only degree with respect to vertex ids) in the view tab panel
   output$view <- renderTable({
  if(is.null(filedata())) {
    return()
  }
  df <- filedata()
  vorder <-sort(degree(df), decreasing=TRUE)
  DF <- data.frame(ID=as.numeric(V(df)[vorder]), degree=degree(df)[vorder])
})

    output$downloadData <- downloadHandler(
  filename = function() {
    paste(input$graph, '.csv', sep='')
  },

  # Not sure how to write to csv file 
  content = function(file) {
  write.csv(DF, file)
      } 

    )
      }) 

并且所需的 CSV 文件列应具有:1. 顶点 ID 2.那个顶点的度数

我不知道您尝试了什么或什么对您不起作用,但这应该可以做到:

g <-read.graph(file=inFile$datapath, format='pajek')
vorder <- order(degree(g), decreasing=TRUE)
DF <- data.frame(ID=as.numeric(V(g)[vorder]), degree=degree(g)[vorder])
write.csv(DF, file='foo.txt')