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

Error message showing column name when trying to write list column #962

Merged
merged 5 commits into from
Sep 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion R/write.R
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,10 @@ output_column <- function(x, name) {

#' @export
output_column.default <- function(x, name) {
if (!is.object(x)) return(x)
if (!is.object(x) || "AsIs" %in% class(x)) {
return(x)
}

as.character(x)
}

Expand Down
21 changes: 21 additions & 0 deletions src/write_delim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,21 @@ void stream_delim(
output << '"';
}

void validate_col_type(SEXP x, std::string name) {
switch (TYPEOF(x)) {
case LGLSXP:
case INTSXP:
case REALSXP:
case STRSXP:
break;
default:
cpp11::stop(
"Don't know how to handle vector of type %s in column '%s'.",
Rf_type2char(TYPEOF(x)),
name.c_str());
}
}

template <class Stream>
void stream_delim(
Stream& output,
Expand All @@ -106,6 +121,12 @@ void stream_delim(
output << "\xEF\xBB\xBF";
}

cpp11::strings names(df.attr("names"));
// Validate column types
for (int j = 0; j < p; ++j) {
validate_col_type(df.at(j), names[j]);
}

if (col_names) {
cpp11::strings names(df.attr("names"));
for (int j = 0; j < p; ++j) {
Expand Down
7 changes: 7 additions & 0 deletions tests/testthat/test-write.R
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,13 @@ test_that("hms NAs are written without padding (#930)", {
expect_equal(format_tsv(df), "x\nNA\n00:00:34.234\n")
})

test_that("Show column name in error message when writing list column (#938)", {
df <- data.frame(x = LETTERS[1:4],
y = I(list(1, "foo", 2:9, iris)))
expect_error(write_csv(df, "test_list_col_name.csv"),
"Don't know how to handle vector of type list in column 'y'.")
})

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, file = tempfile()), "Flat files can't store the list column")
Expand Down
Empty file.