Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More helpful error when trying to write out data frames with list columns #1009

Merged
merged 5 commits into from
Jul 8, 2019
Merged

Conversation

ellessenne
Copy link
Contributor

Closes #938:

library(tidyverse)
df <- diamonds %>% nest(-cut)
write_csv(df, "test.csv")
#> Error in output_column.list(x[[i]], i): Flat files can't store the list column 'data'

However, this approach does not work so well when multiple list columns are present:

df$list <- lapply(seq(nrow(df)), rnorm, n = 3)
write_csv(df, "test.csv")
#> Error in output_column.list(x[[i]], i): Flat files can't store the list column 'data'

Is this behaviour acceptable or would it be better to print the names of all unsupported list columns at once?

Alessandro

Copy link
Member

@hadley hadley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implementation looks great - I added a few comments about style.

R/write.R Outdated
format(x, "%Y-%m-%dT%H:%M:%OSZ", tz = "UTC", justify = "none")
}

#' @export
output_column.list <- function(x, name) {
stop(paste0("Flat files can't store the list column '", name, "'"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should use ` rather than ', and you need a call. = FALSE on the end. Also stop() automatically pastes together it's inputs, you can remove the paste() if you want.

@@ -163,3 +163,11 @@ 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(11))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd make this a little simpler: df <- tibble(x = 1, y = list(1))

df$list <- lapply(X = seq_along(df$id), FUN = rnorm, n = 3)
filename <- file.path(tempdir(), "test_list.csv")
on.exit(unlink(filename))
expect_error(write_csv(x = df, path = filename), "Flat files can't store the list column '")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd just do write_csv(df, tempfile()) — you don't need to clean up since this won't read a file.

@hadley hadley added the tidy-dev-day 🤓 Tidyverse Developer Day rstd.io/tidy-dev-day label Jul 8, 2019
@ellessenne
Copy link
Contributor Author

Thanks @hadley, I push some new changes following your suggestions!

@hadley hadley merged commit 45e8370 into tidyverse:master Jul 8, 2019
@hadley
Copy link
Member

hadley commented Jul 8, 2019

Thanks @ellessenne

jimhester added a commit that referenced this pull request Jan 8, 2021
Because of the way #1009 was
implemented it caused a bug when writing data with duplicated column
names. It also only preformed the check for `write_delim()`, not for all
the writing functions.

We now do this in a different way, which again restores the ability to
write data with duplicated columns.

Fixes #1169
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tidy-dev-day 🤓 Tidyverse Developer Day rstd.io/tidy-dev-day
Projects
None yet
Development

Successfully merging this pull request may close these issues.

More helpful error when trying to write out data frames with list columns
2 participants