Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

spec_csv errors on one-line literal data #1533

Open
nicolaspayette opened this issue Mar 4, 2024 · 1 comment
Open

spec_csv errors on one-line literal data #1533

nicolaspayette opened this issue Mar 4, 2024 · 1 comment

Comments

@nicolaspayette
Copy link

I would expect spec_csv to work on literal data, just like read_csv, but when the data passed to it is just a single line, it tries to interpret the string as a filename:

library(readr) # v2.1.5

# `spec_csv` works with a literal two-line string:
spec_csv(file = I("1,x\n2,y"), col_names = c("a", "b"))
#> cols(
#>   a = col_double(),
#>   b = col_character()
#> )

# but doesn't work with a one-line literal string:
spec_csv(file = I("1,x"), col_names = c("a", "b"))
#> Error: '1,x' does not exist in current working directory ('/tmp/RtmptpBMP2/reprex-276b5141a9194f-jolly-bass').

# while `read_csv` itself can handle it just fine
spec(read_csv(file = I("1,x"), col_names = c("a", "b")))
#> Rows: 1 Columns: 2
#> ── Column specification ────────────────────────────────────────────────────────
#> Delimiter: ","
#> chr (1): b
#> dbl (1): a
#> 
#> ℹ Use `spec()` to retrieve the full column specification for this data.
#> ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
#> cols(
#>   a = col_double(),
#>   b = col_character()
#> )

Created on 2024-03-04 with reprex v2.1.0

@nicolaspayette nicolaspayette changed the title spec_csv errors on one-line litteral data spec_csv errors on one-line literal data Mar 4, 2024
@jennybc
Copy link
Member

jennybc commented Mar 5, 2024

To get you unstuck in the here and now, I advise that you end with an explicit newline like so:

library(readr)

spec_csv(file = I("1,x\n"), col_names = c("a", "b"))
#> cols(
#>   a = col_double(),
#>   b = col_character()
#> )
spec_csv(file = "1,x\n", col_names = c("a", "b"))
#> cols(
#>   a = col_double(),
#>   b = col_character()
#> )

(The spec_*() functions are generated in a fairly exotic way and would be good for a refresh/refactor.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants