简体   繁体   中英

Convert Pajek file to CSV and Display it in R Shiny

Does anyone know how to read pajek file in shiny and then find the degree of each vertex and output it to CSV file in descending order?

Here's the Pajek file i want to import in and export the degree into CSV.

In R, i know how to code it normally like this:

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

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

But I'm not sure how to do it in Shiny:

Here's what I've done so far:

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


   output$view <- renderTable({
  if(is.null(filedata())) {
    return()
  }
  df <- filedata()
  vorder <-sort(degree(df), decreasing=TRUE)
  DF <- data.frame(ID=V(df)[vorder], degree=degree(df)[vorder])
})

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

  # This function should write data to a file given to it by
  # the argument 'file'.
  content = function(file) {
  write.csv(DF, file)
      } 

    )
      }) 

I'm not sure how to take the file from filedata() method where it reads the graph, then take it's degree of the pajek file then output them in CSV file where highest degree is at the top and lowest at the bottom.

and the desired CSV file columns should have: 1. Vertex id 2. Degree of that vertex

I might be misreading your script, but I think you need to move all of your data ingestion and manipulation out of the shinyServer() section of your 'server.R' file to the space preceding it. Generally, the only stuff that goes in the shinyServer() slot is the code that renders the reactive elements in your app. All of the prep for those reactive elements should come before the call to shinyServer() . See this part of the Shiny tutorial for more info on the recommended structure of that 'server.R' script.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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