首页 > 解决方案 > 循环写入时无法打开连接

问题描述

我正在尝试遍历文件夹,读取文件,删除第二行,然后将文件写入新文件夹。我以后会做更多。我只是将其作为初始步骤运行。

我使用以下方法输入文件:

files <- list.files(path="c:/users/~/Data/Test", pattern="CRG01*", full.names=TRUE, recursive=FALSE)

然后我尝试使用 lapply。

lapply(files, function(x) {
  #t <- read.table(x, header=TRUE,fill=TRUE) # load file
  # apply function


  #Manipulate the Power Factors Files
  mydata <-read.csv(x, header = T, stringsAsFactors=FALSE)
  mydata<- mydata[,1:4]
  headers<- read.csv(x, header = F, nrows = 1, as.is = T, stringsAsFactors=FALSE)
  headers<- headers[,1:4]
  x_in <- read.csv(x, skip=2,header = F, stringsAsFactors=FALSE)
  x_in<- x_in[,1:4]
  colnames(x_in)= headers


  # write to file
  write.table(x_in, "c:/Users/~/Data/Test/Output",  sep="\t",
              row.names=FALSE, col.names=TRUE)
})

当我运行它时,我得到以下错误。

Error in file(file, ifelse(append, "a", "w")) : 
  cannot open the connection
In addition: Warning message:
In file(file, ifelse(append, "a", "w")) :

 Error in file(file, ifelse(append, "a", "w")) : 
  cannot open the connection

起初我以为它与管理员权限有关,但我被允许以管理员身份运行作为测试并得到相同的错误。现在我不知道原因是什么。

谢谢是提前!

标签: rlapply

解决方案


推荐阅读