I'm trying to write out a .csv file.
readr::write_csv seems to think that my file isn't a data.frame.
When I run:
PriceCostRaw <- write_csv(PriceCostRaw, "Price Cost Raw.csv")
it returns this error:
Error in write_delim(x, path, delim = ",", na = na, append = append, 
col_names = col_names) : 
is.data.frame(x) is not TRUE
It is in fact a data.frame:
> str(PriceCostRaw)
Classes ‘tbl_df’, ‘tbl’ and 'data.frame':   192 obs. of  7 variables:
> is.data.frame(PriceCostRaw)
[1] TRUE  
utils::write.csv seems to work just fine.
Why is this happening with write_csv?  Are there other tests I can check for to make sure something weird is going on with my data file or variable structures?
I can't post the data itself because it's proprietary.
Reproducing the write_delim() ... is.data.frame(x) is not TRUE issue on my system:
library(purrr)
library(dplyr)
library(readr)
library(tidyr)
library(purrrlyr)
iris %>%
    group_by(Species) %>%
    nest() %>%
    by_row(~write_csv(.$data, 
                      path = file.path(tempdir(), paste0(.$Species, ".csv"))))
# Error in write_delim(x, path, delim = ",", na = na, append = append,
#                      col_names = col_names,  : 
# is.data.frame(x) is not TRUE
There probably is a simpler way, I was trying to write a data frame to many csv files. Inspired by an answer to this Stackoverflow question: Write multiple data frames to csv-file using purrr::map
The code does work when replacing write_csv() with the base R version write.csv():
iris %>%
    group_by(Species) %>%
    nest() %>%
    by_row(~write.csv(.$data, 
                      file = file.path(tempdir(), paste0(.$Species, ".csv"))))
Successfully writes many csv files, each with a species name.
Explicitly specify path = ... in write_csv()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With