简体   繁体   English

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

[英]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? 有谁知道如何闪亮地读取pajek文件,然后找到每个顶点的度并将其以降序输出到CSV文件?

Here's the Pajek file i want to import in and export the degree into CSV. 这是我要导入的Pajek文件,并将度导出为CSV。

In R, i know how to code it normally like this: 在R中,我知道通常如何这样编码:

#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: 但是我不确定如何在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. 我不确定如何从读取图的filedata()方法中获取文件,然后获取pajek文件的度数,然后将其输出到CSV文件中,其中度数最高的是底部,最低的是。

and the desired CSV file columns should have: 1. Vertex id 2. Degree of that vertex 并且所需的CSV文件列应具有:1.顶点ID 2.该顶点的度

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. 我可能会误读您的脚本,但是我认为您需要将所有数据提取和操作都从“ server.R”文件的shinyServer()部分移到它之前的空间。 Generally, the only stuff that goes in the shinyServer() slot is the code that renders the reactive elements in your app. 通常, shinyServer()插槽中唯一shinyServer()东西是在应用程序中呈现反应式元素的代码。 All of the prep for those reactive elements should come before the call to shinyServer() . 这些反应性元素的所有准备工作都应在调用shinyServer() See this part of the Shiny tutorial for more info on the recommended structure of that 'server.R' script. 有关“ server.R”脚本的推荐结构的更多信息,请参见Shiny教程的这一部分

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

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