Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

fread(text = ..., yaml = TRUE) fails with "cannot open file" when input is a scalar character string with newlines

未关闭
#7,896 0 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
68/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
活跃
技术栈
r
领域
data

调研方向

Start by running the minimal reproducible example with data.table::fread(text = ..., yaml = TRUE) and compare scalar and multi-element inputs. Trace fread's text and YAML input handling; done means a single newline-containing character string behaves like the working vector input without attempting to open it as a file.

由索引模型根据 Issue 内容生成。

描述

bug fread
Summary

When reading CSV data containing YAML metadata using fread(text = ..., yaml = TRUE), the function fails if text is a single character string containing newline characters (\n).

Curiously, if the exact same content is passed as a multi-element character vector (e.g. from capture.output()), fread works as expected.

Minimal Reproducible Example (reprex)
library(data.table)

# 1. Generate YAML-annotated CSV lines
lines_vec <- capture.output(fwrite(head(iris, 2), yaml = TRUE))

# Case A: Multi-element character vector -> WORKS
res_vec <- fread(text = lines_vec, yaml = TRUE)
print(res_vec)

# Case B: Single character string with \n (typical for HTTP responses / file reads) -> FAILS
scalar_text <- paste0(lines_vec, collapse = "\n")

res_scalar <- fread(text = scalar_text, yaml = TRUE)

Output of Case B:

Called from: h(simpleError(msg, call))
Warning message:
In base::file(input, "r") : cannot open file '---
source: R[v4.5.3]::data.table[v1.18.6.1]::fwrite
creation_time_utc: 2026-09-02 14:50:31
schema:
  fields:
  - name: Sepal.Length
    type: numeric
  - name: Sepal.Width
    type: numeric
  - name: Petal.Length
    type: numeric
  - name: Petal.Width
    type: numeric
  - name: Species
    type: factor
header: yes
sep: ','
sep2:
- ''
- '|'
- ''
eol: |2+

na.strings: ''
dec: '.'
qmethod: double
logical01: no
---
Sepal.Length,Sepal.Width,Petal.Length,Petal.Width,Species
5.1,3.5,1.4,0.2,setosa
4.9,3,1.4,0.2,setosa': No such file or directory
Expected Behaviour

It should behave the same as with yaml = FALSE, for which no error occurs:

lines_vec <- capture.output(fwrite(head(iris, 2), yaml = FALSE))
res_vec <- fread(text = lines_vec, yaml = FALSE)
print(res_vec)
scalar_text <- paste0(lines_vec, collapse = "\n")
res_scalar <- fread(text = scalar_text, yaml = FALSE)
res_scalar

> sessionInfo()
R version 4.5.3 (2026-03-11 ucrt)
Platform: x86_64-w64-mingw32/x64
Running under: Windows 11 x64 (build 26200)

Matrix products: default
  LAPACK version 3.12.1

locale:
[1] LC_COLLATE=German_Germany.utf8  LC_CTYPE=German_Germany.utf8   
[3] LC_MONETARY=German_Germany.utf8 LC_NUMERIC=C                   
[5] LC_TIME=German_Germany.utf8    

time zone: Europe/Berlin
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods  
[7] base     

other attached packages:
[1] data.table_1.18.6.1

loaded via a namespace (and not attached):
[1] compiler_4.5.3 cli_3.6.6      tools_4.5.3    yaml_2.3.12
主要语言
R
星标
3.9k
派生
1.1k
平均合并
14 小时 4 分钟
30 天内合并 PR
4

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

Rdatatable/data.table 的其他 Issue

查看 Rdatatable/data.table 的全部 Issue

相似的 Issue

更多 R Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。