0

I would like to export 100 data frames to an excel sheet and for this I thought I would create a list contening all the data frames and then use write_xlsx to export it. When I run the following code I get this error message : "Argument x must be a data frame or list of data frames".

Would any one know what the issue seems to be ?

Thanks!

df_list <- list()[sapply(ls(), function(x) any(is.data.frame(get(x))))]

writexl::write_xlsx(df_list, "M:") 

David Potrel
  • 111
  • 8
  • 2
    `list()` is an empty list. Following it with brackets will try to subset it. A subset of an empty list is still empty. `is.data.frame()` returns TRUE or FALSE so `any()` doesn't make sense here. Use [this answer](https://stackoverflow.com/a/25510018/903061) for "How to make a list of all data frames in my current environment?": `df_list <- Filter(function(x) is(x, "data.frame"), mget(ls()))` – Gregor Thomas Aug 16 '22 at 21:08
  • Note that you probably want to fix up the names of your files when you write, at least pasting on a `".xlsx"`. – Gregor Thomas Aug 16 '22 at 21:10
  • 1
    My question was closed because too similar to another one but I wanted to thank you anyway as this was exactly what I needed and I appreciated the explanation. Cheers! – David Potrel Aug 17 '22 at 12:47

0 Answers0