1

I would like to write a dataframe with the name of the dataframe passed to the write_csv function. For example, I would normally do the following

iris %>% write_csv("iris.csv")

but I would like to not have to write the name of the file. I tried something like the following

iris %>% write_csv(paste0(., ".csv"))

but I think this will require something more like a quosure

Alex
  • 2,603
  • 4
  • 40
  • 73

2 Answers2

3

Just define your own custom write.csv3:

write.csv3 <- function(x, ...) {
  write.csv(x, file = paste0(deparse(substitute(x)), '.csv'), ...)
}
write.csv3(iris)

If you want to use pipes then you could set an attribute name first:

write.csv3 <- function(x, ...) {
  write.csv(x, file = paste0(attr(x, "name"), '.csv'), ...)
}

attr(iris, "name") <- "iris" 
iris %>% write.csv3

Or you try to apply this hacky approach.

Martin Schmelzer
  • 23,283
  • 6
  • 73
  • 98
0

The basic use case of function parameter in a dplyr pipe is to treat function parameter as a name something like this:

csv_save <- function(xx){
  csv_name = as.name(xx)
  iris %>% write.csv(.,file = !!csv_name)
}

csv_save("files.csv")

(not tested)

Aleksandr
  • 1,814
  • 11
  • 19