Coco Newton
Coco Newton

Reputation: 69

How to print comma separated list of character strings without quotations in R

I have a data frame 'output' in R listing the labels of tibbles in my workspace:

output
1    csv1
2    csv2
3    csv3
4    csv4
5    csv5
6    csv6
7    csv7
8    csv8
.
. 
35   csv35

and would like to print it as the input of a function:

dplyr::bind_rows(csv1,csv2,csv3...csv35) 

(each of csv1,csv2 etc are tibbles themselves)

paste() and as.list() don't seem to work

Many thanks for any advice!

Upvotes: 1

Views: 691

Answers (1)

Ben Bolker
Ben Bolker

Reputation: 226097

Probably the easiest way to do what you want is to call mget() to create a list to pass to bind_rows:

bind_rows(mget(output[[1]]))

More generally, though, your procedure looks like an antipattern; it's easier to deal with sets of objects as lists in R, rather than generating a bunch of objects in your workspace and then figuring out how to deal with them ...

For example, if you have a character vector of CSV files,

csv_list <- purrr::map(csv_filenames, read_csv)
bind_rows(csv_list)

will work. (In fact, purrr::map_dfr(csv_filenames, read_csv) will do this in a single step ...)

Upvotes: 3

Related Questions