4

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.

Ash Levitt
  • 153
  • 3
  • 11
  • 4
    Without a reproducible example, all I can suggest is to make sure you have the latest versions of readr and R and to try again in a clean session. – joran Nov 08 '18 at 17:54
  • 3
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Nov 08 '18 at 18:00
  • try `write_excel_csv`. – Jiaxiang Nov 08 '18 at 18:00
  • 1
    I had already checked that it was current versions of both readr and R, and I had already restarted my R session. I restarted the session again, reinstalled readr (with the same version number), and it worked this time. Strange. Sorry for the hassle. – Ash Levitt Nov 08 '18 at 18:03
  • No worries, it happens to everyone. – joran Nov 08 '18 at 18:22

3 Answers3

2

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.

Paul Rougieux
  • 10,289
  • 4
  • 68
  • 110
1

Explicitly specify path = ... in write_csv()

jakej
  • 61
  • 6
0

If you want to use write_csv, you should make sure that your input is a dataframe. You could use as.data.frame like this (mtcars dataset as an example):

library(readr)
library(dplyr)
library(tidyr)
mtcars %>% 
  dplyr::group_by(cyl) %>% 
  tidyr::nest() %>%
  dplyr::group_walk(~ write_csv(.x$data, paste0(.y$cyl, ".csv")))
#> Error in write_delim(x, file, delim = ",", na = na, append = append, col_names = col_names, : is.data.frame(x) is not TRUE

mtcars %>% 
  dplyr::group_by(cyl) %>% 
  tidyr::nest() %>%
  dplyr::group_walk(~ write_csv(as.data.frame(.x$data), paste0(.y$cyl, ".csv")))

Created on 2023-03-01 with reprex v2.0.2

As you can see, the error is gone when using as.data.frame.

Quinten
  • 35,235
  • 5
  • 20
  • 53
  • Shouldn't it also work with a tibble? According to the documentation x can be "A data frame or tibble to write to disk." https://readr.tidyverse.org/reference/write_delim.html – Emmanuel Teitelbaum Mar 17 '23 at 01:24