You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

通过R循环调用REDCap API导出报告时遇读取输入错误求助

REDCap API批量下载报告报错:read.table无可用输入行

运行脚本时触发错误:

Error in read.table(file = file, header = header, sep = sep, quote = quote, : no lines available in input

我需要用R的for循环遍历多列表格,每列对应token、report_id、保存路径三个变量,调用REDCap API批量下载273份报告并保存到桌面文件夹,但运行时出现上述错误。以下是我编写的脚本:

##Call server
library(RCurl)
library(tidyverse)
library(stringr)
library(redcapAPI)

TableForR<-read.csv("//usershare/users$/aweible1/Profile_Data/Desktop/REDCap Monthly Check/TableForR.csv")


for(j in seq_len(nrow(TableForR))){    
    labelFile <- RCurl::postForm(    
        uri = "https://redcap.uhhospitals.org/redcap/api/",    
        token = TableForR[j,1],    
        content = 'report',    
        report_id = TableForR[j,2],    
        format = 'csv',    
        type = 'flat',    
        rawOrLabel = 'label',    
        exportDataAccessGroups = 'true',    
        .opts = curlOptions(ssl.verifypeer=FALSE)    
    )        
    backup <- read.csv(text = labelFile, stringsAsFactors = FALSE)            
    write.csv(backup, file = TableForR[j,3], row.names=FALSE)
}

排查与修复步骤

  1. 先验证API返回内容
    在read.csv前添加调试代码,检查每一次API请求的返回值:
print(labelFile)
cat("当前循环行:", j, "\n")

如果返回是空字符串、HTML错误页面(比如401未授权、404报告不存在),就会触发no lines available错误。

  1. 检查TableForR数据有效性
  • 确认token列的API令牌是否有效,有没有过期或权限不足
  • 确认report_id列的报告ID是否存在,且对应令牌有权限访问该报告
  • 确认保存路径列的路径是否合法,有没有写入权限,路径格式是否正确(比如Windows路径用\\或/,避免转义问题)
  1. 改用redcapAPI原生方法
    既然已经加载了redcapAPI,可以用更简洁且自带错误处理的方法替代RCurl::postForm:
for(j in seq_len(nrow(TableForR))){    
    # 初始化连接
    rcon <- redcapConnection(
        url = "https://redcap.uhhospitals.org/redcap/api/",
        token = TableForR[j,1],
        ssl_verifypeer = FALSE
    )
    
    # 下载报告
    backup <- exportReport(
        rcon,
        report_id = TableForR[j,2],
        raw_or_label = "label",
        export_data_access_groups = TRUE,
        format = "csv"
    )
    
    # 保存文件
    write.csv(backup, file = TableForR[j,3], row.names = FALSE)
    
    # 关闭连接
    close(rcon)
}

redcapAPI会自动处理API响应的解析,若请求失败会抛出更明确的错误信息,方便定位问题。

  1. 添加错误捕获机制
    给循环加上tryCatch,避免单个请求失败导致整个脚本中断:
for(j in seq_len(nrow(TableForR))){    
    tryCatch({
        rcon <- redcapConnection(
            url = "https://redcap.uhhospitals.org/redcap/api/",
            token = TableForR[j,1],
            ssl_verifypeer = FALSE
        )
        
        backup <- exportReport(
            rcon,
            report_id = TableForR[j,2],
            raw_or_label = "label",
            export_data_access_groups = TRUE,
            format = "csv"
        )
        
        write.csv(backup, file = TableForR[j,3], row.names = FALSE)
        close(rcon)
        cat("成功保存:", TableForR[j,3], "\n")
    }, error = function(e){
        cat("第", j, "行处理失败:", e$message, "\n")
    })
}

内容的提问来源于stack exchange,提问作者Adam W

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 20:11:47