Skip to content

Commit

Permalink
Addressed tidyverse#938, and throw error for list columns with class …
Browse files Browse the repository at this point in the history
…"AsIs"
  • Loading branch information
lambdamoses committed Jan 19, 2019
1 parent 62bb7c9 commit fa7120d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
2 changes: 1 addition & 1 deletion R/write.R
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ output_column <- function(x) {

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

Expand Down
23 changes: 14 additions & 9 deletions src/write_delim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ void stream_delim(
int i,
char delim,
const std::string& na,
quote_escape_t escape);
quote_escape_t escape,
std::string name); // Argument name for error message with colum names

template <class Stream>
void stream_delim_row(
Expand All @@ -24,14 +25,16 @@ void stream_delim_row(
int i,
char delim,
const std::string& na,
quote_escape_t escape) {
quote_escape_t escape,
const CharacterVector& names) {
int p = Rf_length(x);

for (int j = 0; j < p - 1; ++j) {
stream_delim(output, x.at(j), i, delim, na, escape);
stream_delim(output, x.at(j), i, delim, na, escape, std::string(names[j]));
output << delim;
}
stream_delim(output, x.at(p - 1), i, delim, na, escape);
stream_delim(output, x.at(p - 1), i, delim, na, escape,
std::string(names[p - 1]));

output << '\n';
}
Expand Down Expand Up @@ -101,10 +104,10 @@ void stream_delim(
output << "\xEF\xBB\xBF";
}

CharacterVector names = as<CharacterVector>(df.attr("names"));
if (col_names) {
CharacterVector names = as<CharacterVector>(df.attr("names"));
for (int j = 0; j < p; ++j) {
stream_delim(output, names, j, delim, na, escape);
stream_delim(output, names, j, delim, na, escape, std::string(names[j]));
if (j != p - 1)
output << delim;
}
Expand All @@ -115,7 +118,7 @@ void stream_delim(
int n = Rf_length(first_col);

for (int i = 0; i < n; ++i) {
stream_delim_row(output, df, i, delim, na, escape);
stream_delim_row(output, df, i, delim, na, escape, names);
}
}

Expand Down Expand Up @@ -166,7 +169,8 @@ void stream_delim(
int i,
char delim,
const std::string& na,
quote_escape_t escape) {
quote_escape_t escape,
std::string name) { // Argument name for error message with colum names
switch (TYPEOF(x)) {
case LGLSXP: {
int value = LOGICAL(x)[i];
Expand Down Expand Up @@ -218,6 +222,7 @@ void stream_delim(
}
default:
Rcpp::stop(
"Don't know how to handle vector of type %s.", Rf_type2char(TYPEOF(x)));
"Don't know how to handle vector of type %s in column '%s'.",
Rf_type2char(TYPEOF(x)), name);
}
}
7 changes: 7 additions & 0 deletions tests/testthat/test-write.R
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,10 @@ 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("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'.")
})
2 changes: 2 additions & 0 deletions tests/testthat/test_list_col_name.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
x,y
A,

0 comments on commit fa7120d

Please sign in to comment.