R read.csv data from inline string with a csv file content

3.3k views Asked by At

this is supposed to be a trivial thing, but I have not found anything googling it.

I have the following data in a csv file

test.csv

var1,var2
'a',1
'b',2

which I read into R with

d <- read.csv('test.csv')

This there a way for me to insert the content of the csv file in my R code? Something like:

d <- read.csv(
    'var1,var2\n
    'a',1\n
    'b',2')

(above syntax does not work)

how can I tell R that the input string should be treated as the data itself instead of a file name containing it?

This would be for adding small tables to Rmarkdown without having to create a bunch of auxiliary files

of course I could add the same info with

d <- data.frame(
  var1=c('a','b'),
  var2=1,2
)

but listing the data vector by vector slow the process down. The row by row structure of the csv is easyer

3

There are 3 answers

1
G5W On BEST ANSWER

Try this

CSV_Text = "var1,var2
'a',1
'b',2"

Dat = read.csv(text=CSV_Text, header=TRUE)
0
akrun On

We can use fread which would be very fast

library(data.table)
fread(txt)
#   var1 var2
#1:    a    1
#2:    b    2

data

txt = "var1,var2
  a,1
  b,2"
0
Aaron Wells On

The tibble package's tribble() method, is another great option for writing short bits of tabular data in an R file, in a way that is human-readable, machine-readable, and easy to maintain/edit. And as a bonus, it makes it easy to use R expressions as values in the table.

# Column names are prefaced with "~"
my_tibble1 <- tribble(
  ~colA, ~colB, ~colC,
  "a",   1,     10.1,
  "b",   2,     3.4,
  "c",   3,     5.6
)
#> # A tibble: 3 x 3
#>    colA  colB  colC
#>   <chr> <dbl> <dbl>
#> 1     a     1  10.1
#> 2     b     2   3.4
#> 3     c     3   5.6


# tribble will create a list column if the value in any cell is
# not a scalar
my_tibble2 <- tribble(
  ~x,  ~y,
  "a", 1:3,
  "b", 4:6
)
#> # A tibble: 2 x 2
#>       x         y
#>   <chr>    <list>
#> 1     a <int [3]>
#> 2     b <int [3]>

If you haven't encountered tibbles before, they're a (mostly) drop-in replacement for data.frame. If you need to really make sure that your data is a data.frame and not a tibble, you can convert it to one with as.data.frame(...).