文件错误(con,“rb”):无法打开连接外部硬盘驱动器R

时间:2018-04-29 01:56:35

标签: r bigdata dicom imaging

我有以下代码块:

# Obtain records from all patients
patientDir <- sort(list.dirs(path = "sample_images", full.names = TRUE, recursive = FALSE))

dataframes <- list()
i = 1
while(i<19){
  # Strip the patient out
  patient <- coreHist(patientDir[i])

  print("1")
  setwd("/Volumes/HUGE storage drive/")
  exists<- file.exists(patientDir[i])

  print(exists)

  # Extract the relevant information from the patient
  dicom <- readDICOM(patientDir[i])
  dicomdf <- dicomTable(dicom$hdr)  
  patient_id <- dicomdf$`0010-0020-PatientID`[1] 

  print("2")

  # Normalize their VX's
  sum<- sum(patient$histData$finalFreq)

  print("3")

  # Create the new VX's
  patient$histData$finalFreq_scaled <- (patient$histData$finalFreq/sum)

  print("4")

  # Add their ID
  patient$histData$patientid <- patient_id

  print("5")

  # Keep only the important columns
  patient$histData <- patient$histData[c("patientid", "Var1", "finalFreq_scaled")]

  print("6")

  # Add these dataframes to a list for better recall afterwards
  dataframes[[i]] <- patient$histData

  print("7")

  # Additional code to transpose and merge dataframes
  if(i == 1){
    wide_df <- patient$histData
  }else{
    wide_df <- rbind(wide_df,patient$histData )
  }

  print("8")

  print(paste(c("Patient", i), sep ="", collapse = "-"))

  i = i+1
}

然而,在(看似随机的)迭代次数之后,代码在“print(”1“)行之后立即失败,并出现以下错误:

Error in file(con, "rb") : cannot open the connection

工作目录设置为外部硬盘驱动器,因为“sample_images”文件夹大62GB。我想也许与R studio和我的外部硬盘驱动器有超时连接,所以我试图在我的计算机上“保持活动状态”,我也尝试在每次迭代后重置工作目录以确保它可以找到该文件。

当某个病人失败时,我会手动检查该文件是否确实存在,而且确实存在。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我实际上不确定错误发生的原因,但为了解决这个问题,我只是添加了一个&#34;尝试&#34;语句:

  attempt <- 1
  while(is.null(dicom) && attempt <= 3){
    attempt <- attempt + 1
    try(
      dicom <- readDICOM(patientDir[i])
    )  
  }

确实有效。