繁体   English   中英

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

[英]Convert Pajek file to CSV in R Shiny

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

这是我要导入的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中执行此操作:

到目前为止,这是我所做的:

用户界面

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

                           ) 

               )

  )
))

服务器

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

暂无
暂无

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

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