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

format.col_spec() fails with long expressions. #597

Closed
jdeboer opened this issue Feb 5, 2017 · 2 comments
Closed

format.col_spec() fails with long expressions. #597

jdeboer opened this issue Feb 5, 2017 · 2 comments
Labels
bug an unexpected problem or unintended behavior

Comments

@jdeboer
Copy link
Contributor

jdeboer commented Feb 5, 2017

I am defining a column as a factor, as follows.

# Example 1:
readr::cols(a = readr::col_factor(
    levels = c("apple", "pear", "banana", "peach", "apricot", "orange", "plum"), ordered = TRUE)
)

Which produces the following error:

## Error in vapply(cols[[i]], deparse, character(1)) : 
##  values must be length 1,
## but FUN(X[[1]]) result is length 2 

However, the following two examples do not produce errors:

# Example 2:
# Remove one of the levels to leave just six.
readr::cols(a = readr::col_factor(
   levels = c("apple", "pear", "banana", "peach", "apricot", "orange"), ordered = TRUE)
)
# Example 3:
# Back to seven levels again, but with shorter labels.
readr::cols(a = readr::col_factor(
    levels = c("apple", "pear", "banana", "peach", "apri", "oran", "plum"), ordered = TRUE)
)

But with the third example, if I add just one extra character to any of the labels, e.g. 'apri' to 'apric', I get the error again:

# Example 4:
readr::cols(a = readr::col_factor(
    levels = c("apple", "pear", "banana", "peach", "apric", "oran", "plum"), ordered = TRUE)
)
## Error in vapply(cols[[i]], deparse, character(1)) : 
##  values must be length 1,
## but FUN(X[[1]]) result is length 2 

After trying a number of combinations, I believe the error occurs once there are more than 32 cumulative characters used to define the labels of the levels:

# From example 3:
nchar(paste(c("apple", "pear", "banana", "peach", "apri", "oran", "plum"), collapse = ""))
## [1] 32

# From example 4:
nchar(paste(c("apple", "pear", "banana", "peach", "apric", "oran", "plum"), collapse = ""))
## [1] 33

Below is a copy of the traceback:

## 7.
## vapply(cols[[i]], deparse, character(1)) 
## 6.
## FUN(X[[i]], ...) 
## 5.
## vapply(seq_along(cols), function(i) {
##     col_funs <- sub("^collector_", "col_", class(cols[[i]])[[1]])
##     args <- vapply(cols[[i]], deparse, character(1))
##     args <- paste(names(args), args, sep = " = ", collapse = ", ") ... 
## 4.
## format.col_spec(x, n = n, condense = condense, ...) 
## 3.
## cat(format.col_spec(x, n = n, condense = condense, ...)) 
## 2.
## print.col_spec(x) 
## 1.
## function (x, ...) 
## UseMethod("print")(x) 

I am using the current development version of readr available through github and R version 3.3.2 on Windows 10.

@jennybc
Copy link
Member

jennybc commented Feb 5, 2017

You can make such a specification ... but you just can't print it at the moment.

x <- readr::cols(a = readr::col_factor(
  levels = c("apple", "pear", "banana", "peach", "apricot", "orange", "plum"), ordered = TRUE)
)
print(x)
#> Error in vapply(cols[[i]], deparse, character(1)): values must be length 1,
#>  but FUN(X[[1]]) result is length 2

I realize that is still a problem! But it means you can still use col_factor() in the meantime, as long you can suppress the printing of the col spec.

@jimhester jimhester added the bug an unexpected problem or unintended behavior label Feb 6, 2017
jimhester added a commit that referenced this issue Feb 6, 2017
@jimhester jimhester changed the title col_factor fails with cumulative length of labels greater than 32 characters for levels argument. format.col_spec() fails with long expressions. Feb 6, 2017
@jimhester
Copy link
Collaborator

As @jennybc said, this was an error in the format method for col_spec objects caused by deparse breaking long expressions into multiple lines. Fixed by #598.

jimhester added a commit that referenced this issue Feb 8, 2017
@lock lock bot locked and limited conversation to collaborators Sep 24, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug an unexpected problem or unintended behavior
Projects
None yet
Development

No branches or pull requests

3 participants