【问题标题】:Convert Pajek file to CSV in R Shiny在 R Shiny 中将 Pajek 文件转换为 CSV
【发布时间】:2015-07-09 06:34:02
【问题描述】:

有谁知道如何以闪亮的方式读取 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")) 

                           ) 

               )

  )
))

服务器.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.那个顶点的度数

【问题讨论】:

  • 在 R shiny 中,您具有与 R 中相同的功能,有什么问题?
  • 问题是我不确定如何将文件作为变量获取,以便我可以在我的 degree() 函数中使用。他们不允许我使用我在 read.graph() 行中编写的数据变量并收到错误消息:“不是图形对象”@ExpectoPatronum
  • 因为你的数据变量是全局的(你通过使用
  • 我已经用return(data) 编辑了我的问题,但我仍然收到一条错误消息“度数错误(df):不是图形对象”你知道为什么吗? @ExpectoPatronum
  • 我很难调试,因为我没有你所有的方法和数据,但我们可以一起做:) 请在 df

标签: r csv shiny igraph


【解决方案1】:

我不知道你尝试了什么或什么对你不起作用,但应该这样做:

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

【讨论】:

  • 这在普通 R 中很容易。但我不确定如何在 R Shiny 中做到这一点?不过感谢您的帮助。
  • 尝试使用&lt;&lt;- 运算符返回图形对象。
  • 我把这个放在哪里?在文件数据()中?
  • 我已经像 output$view &lt;- renderTable({ vorder &lt;- order(degree(data), decreasing=TRUE) DF &lt;- data.frame(ID=V(data)[vorder], degree=degree(data)[vorder]) }) 这样编辑了我的 server.R,但是当我输入 data &lt;&lt;-read.graph(file=inFile$datapath, format='pajek') 时,我收到一条错误消息“不是图形对象”
  • 如果我通常在 R 中使用您的代码执行此操作,则表示我无法将“igraph”强制转换为数据框。你知道吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-07-05
  • 1970-01-01
  • 2018-07-04
  • 1970-01-01
  • 1970-01-01
  • 2015-07-14
  • 2019-02-22
相关资源
最近更新 更多