R:textConnection中出错:所有连接都在使用中

时间:2014-05-07 21:34:19

标签: r

我有一个用户定义的功能:

xml2csv = function(inputFile,outputFile) {
  X <- read.table(inputFile, header = FALSE, fill = TRUE,sep=" ")

  #step1: seperate cell by one  or more space
  Y <- concat.split.multiple(X, as.vector(colnames(X)), "")

  #step2: seperate cell by ":"
  Z <- concat.split.multiple(Y, as.vector(colnames(Y))[-c(1:2)], ":")

  #delete repeat rows
  U=Z[!Z[,1] == "__REPEAT__", ]

  #convert factor column as character
  V <- data.frame(lapply(U, as.character), stringsAsFactors=FALSE)
  W = V
  W[is.na(W)] = 0
  write.csv(W, outputFile, quote = F, row.names = F)
}

对于小输入文件它完全正常;当输入文件很大(> 2000kb)时,会出现以下错误:

Error in textConnection(text, encoding = "UTF-8") :   all connections are in use

有什么建议吗?

1 个答案:

答案 0 :(得分:3)

很难确切地知道发生了什么(你的错误是不可重复的!),但是如果concat.split.multiple中有太多列,我的钱就是X的错误/限制,因为

  • 它会在concat.split
  • 中的每一列上调用lapply
  • 内部使用textConnection
  • 创建一个临时文件
  • 使用文件处理程序
  • 这是大多数操作系统中的有限资源:)。