Skip to content

Conversation

@zeehio
Copy link

@zeehio zeehio commented Nov 14, 2025

DT needs to convert data frames/tibbles to json.

The R to json conversion has an ambiguity on scalar vector conversions: R can't tell the difference between an "atomic vector of length 1" and "a scalar", while in json a scalar is "just the value" and a vector of length 1 is represented in a list ["just the value"].

When our data frame has a list column, we need to resolve this ambiguity. For list-columns, in DT we need each item of the column to be itself a json list. This means that if the item already is a vector of length > 1, we have no trouble, but if the item is a scalar, we need to coerce it to a list to guarantee that the conversion behaves as expected. See the example:

Example:

Imagine in a two-row dataframe we have a list column with the following content: list(c("a", "b"), "c"). Each element of this list column needs to become a list when converted to json. Naturally, c("a", "b") becomes ["a", "b"], but what about "c"? It would become "c" and we need it to become ["c"] instead.

In R, both items c("a", "b") and "c" are character vectors. We need to coerce the second to a list so x becomes list(c("a", "b"), list("c")) and the conversion to json works as we require.

The problem:

This coercion fails if the list column is not of type list, but a vctrs::list_of with a ptype=character(0) vector. In that scenario, when we try to turn "c" (a character vector) into list("c") (a list), vctrs steps in and tries to coerce the list back to a character, because vctrs::list_of enforces the character type to all elements of the list. This raises a coercion error as seen on #1180.

The easiest solution in this case is to drop the list_of type if we need to, and leave the column as a regular list, so items may have mixed types in R -but will have homogeneous types in json!- and the assignment works without coercions from vctrs.

Reprex in the associated issue #1180 .

I added a unit test to prevent regressions and updated the NEWS file.

Closes #1180

@CLAassistant
Copy link

CLAassistant commented Nov 14, 2025

CLA assistant check
All committers have signed the CLA.

Copy link
Contributor

@yihui yihui left a comment

Choose a reason for hiding this comment

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

Thanks! This looks good to me. Cc @gadenbuie

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

boxAtomicScalarElements errors on vctrs::list_of list

4 participants