fread(text = ..., yaml = TRUE) fails with "cannot open file" when input is a scalar character string with newlines
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 68/100
Research direction
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.
Written by the indexing model from the issue text.
Description
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
- Dominant language
- R
- Stars
- 3.9k
- Forks
- 1.1k
- Avg merge
- 14h 4m
- Merged PRs (30d)
- 4
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from Rdatatable/data.table
-
as.data.table() recurses without end on a survival::Surv object (or any data.frame carrying one) Open
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
Rdatatable/data.table#7887 ·
-
consistency tests
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Rdatatable/data.table#7853 · 3 comments ·
-
internals
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
Rdatatable/data.table#6938 · 1 comment ·
-
encoding fread
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
Rdatatable/data.table#5179 · 8 comments ·
-
documentation programming
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Rdatatable/data.table#3199 · 3 comments ·
All issues in Rdatatable/data.table
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
r-lib/pkgdepends#485 · 3 comments ·
-
Difficulty 1/5 Under an hour Newbie friendliness 92/100
-
beginners blocker
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
enviPathR OpenBuild Error Build OK Build Warning policies-accepted pre-review precheck-passed
Difficulty 1/5 Under an hour Newbie friendliness 84/100
Bioconductor/BiocContributions#207 · 6 comments ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
datacarpentry/semester-biology#1255 ·