Skip to content

Commit

Permalink
More helpful error when trying to write out data frames with list col…
Browse files Browse the repository at this point in the history
…umns (#1009)

Fixes #938
  • Loading branch information
ellessenne authored and hadley committed Jul 8, 2019
1 parent cd68d4a commit 45e8370
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ S3method(format,col_spec)
S3method(output_column,POSIXt)
S3method(output_column,default)
S3method(output_column,double)
S3method(output_column,list)
S3method(print,col_spec)
S3method(print,collector)
S3method(print,date_names)
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
than signaling an error when given a connection for the `file` argument that
contains no data. This makes the behavior consistent as when called with an
empty file (@pralitp, #963).

* It is now possible to generate a column specification from any tibble (or
data.frame) with `as.col_spec()` and convert any column specification to a
short representation with `as.character()`
Expand All @@ -24,6 +25,8 @@
as.character(s)
#> [1] "ddddf"

* More helpful error when trying to write out data frames with list columns (@ellessenne, #938)

# readr 1.3.1

* Column specifications are now coloured when printed. This makes it easy to
Expand Down
15 changes: 10 additions & 5 deletions R/write.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ write_delim <- function(x, path, delim = " ", na = "NA", append = FALSE,
col_names = !append, quote_escape = "double") {
stopifnot(is.data.frame(x))

x[] <- lapply(x, output_column)
x[] <- lapply(names(x), function(i) output_column(x[[i]], i))
stream_delim(x, path, delim = delim, col_names = col_names, append = append,
na = na, quote_escape = quote_escape)

Expand Down Expand Up @@ -187,26 +187,31 @@ format_tsv <- function(x, na = "NA", append = FALSE, col_names = !append, quote_
#' # converted to ISO8601.
#' x <- parse_datetime("2016-01-01")
#' str(output_column(x))
output_column <- function(x) {
output_column <- function(x, name) {
UseMethod("output_column")
}

#' @export
output_column.default <- function(x) {
output_column.default <- function(x, name) {
if (!is.object(x)) return(x)
as.character(x)
}

#' @export
output_column.double <- function(x) {
output_column.double <- function(x, name) {
x
}

#' @export
output_column.POSIXt <- function(x) {
output_column.POSIXt <- function(x, name) {
format(x, "%Y-%m-%dT%H:%M:%OSZ", tz = "UTC", justify = "none")
}

#' @export
output_column.list <- function(x, name) {
stop("Flat files can't store the list column `", name, "`", call. = FALSE)
}

stream_delim <- function(df, path, append = FALSE, bom = FALSE, ..., quote_escape) {
quote_escape <- standardise_escape(quote_escape)

Expand Down
5 changes: 5 additions & 0 deletions tests/testthat/test-write.R
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,8 @@ test_that("hms NAs are written without padding (#930)", {
df <- data.frame(x = hms::as.hms(c(NA, 34.234)))
expect_equal(format_tsv(df), "x\nNA\n00:00:34.234\n")
})

test_that("More helpful error when trying to write out data frames with list columns (#938)", {
df <- tibble::tibble(id = seq(1), list = list(1))
expect_error(write_csv(x = df, path = tempfile()), "Flat files can't store the list column")
})

0 comments on commit 45e8370

Please sign in to comment.