seth127
seth127

Reputation: 2754

Read from character vector CSV to tibble in R

TL;DR I’m wondering if readr or tibble or anything base/tidyverse can make a tibble from a character vector of CSV rows (as if I had done read_lines() on some CSV file).

More explanation: I am collecting output (from stdout of a processx object) into a character vector, and this output is actually lines of a CSV, being created by the subprocess. I would like to make this into a tibble, but the only function I can find that does this is data.table::fread (via the ‘text’ arg). This is in a package and I don’t really want to take a dependency on data.table solely for this one function.

I’ve looked through the tidyverse docs and I can’t find anything. I realize I could write my own parser, and I started doing it with map_def and str_split but then I realized there are subtleties like “ignore the comma if it’s within quotes”, etc. maybe I’m just being lazy, but I don’t really want to implement a full csv parser when there are millions that already exist. Not to mention, speed concerns. This vector is sometimes 10k+ rows and I’m sure my homemade approach would not be the most optimized.

Any thoughts are welcome. Maybe I should just bite the bullet and either write the parser or use data.table but I feel like I can’t be the first person to want to do this and there must be something g built in that I’m missing. Thanks!

Upvotes: 0

Views: 977

Answers (1)

s_baldur
s_baldur

Reputation: 33753

Does read.csv not work?

read.csv(
  text = "a,b
          1,2",
  header = TRUE
)

#   a b
# 1 1 2

Upvotes: 4

Related Questions