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

Problem with Nest leading to r error 4 (R code execution error) #654

Closed
jestarr opened this issue Jun 26, 2019 · 9 comments
Closed

Problem with Nest leading to r error 4 (R code execution error) #654

jestarr opened this issue Jun 26, 2019 · 9 comments

Comments

@jestarr
Copy link

jestarr commented Jun 26, 2019

I've been getting an error message when using nest. Let's use the example from the documentation.

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(tidyr)

as_tibble(iris) %>% nest(-Species)
#> Warning: All elements of `...` must be named.
#> Did you want `data = c(Sepal.Length, Sepal.Width, Petal.Length, Petal.Width)`?
#> # A tibble: 3 x 2
#>   Species              data
#>   <fct>      <list<df[,4]>>
#> 1 setosa                [4]
#> 2 versicolor            [4]
#> 3 virginica             [4]
Created on 2019-06-26 by the reprex package (v0.3.0)

If I assign this to an object and go to view, I get the following error message "r error 4 (R code execution error)".

@batpigandme
Copy link
Contributor

Adding the relevant warning from View() (note: that part not run through reprex, because it's IDE-dependent)

library(dplyr)
library(tidyr)

iris2 <- as_tibble(iris) %>% nest(-Species)
#> Warning: All elements of `...` must be named.
#> Did you want `data = c(Sepal.Length, Sepal.Width, Petal.Length, Petal.Width)`?

View(iris2)
#> Error: Can't cast `x` <list_of<
#>   tbl_df<
#>     Sepal.Length: double
#>     Sepal.Width : double
#>     Petal.Length: double
#>     Petal.Width : double
#>   >
#> >> to `to` <character>.

backtrace

<error>
message: Can't cast `x` <list_of<
  tbl_df<
    Sepal.Length: double
    Sepal.Width : double
    Petal.Length: double
    Petal.Width : double
  >
>> to `to` <character>.
class:   `vctrs_error_incompatible_cast`
backtrace:
  1. (function (x, start, len, ...) ...
  7. vctrs:::vec_cast.character.default(...)
  8. vctrs::vec_default_cast(x, to, x_arg = x_arg, to_arg = to_arg)
  9. vctrs::stop_incompatible_cast(x, to, x_arg = x_arg, to_arg = to_arg)
 10. vctrs:::stop_incompatible(...)
 11. vctrs:::stop_vctrs(...)

@lionel-
Copy link
Member

lionel- commented Jun 26, 2019

There doesn't seem to be a reasonable way of implementing as.character() for arbitrary objects contained in list_of() vectors, short of resorting to rlang::as_label(). Perhaps the IDE should take another approach for representing complex column types, e.g. using pillar if available?

@kevinushey
Copy link
Contributor

Right now, we just print the deparsed representation for nested lists, but I think we'd be open to something better. It's tough because that representation is the most useful in many cases though, e.g.

library(tibble)
tbl <- tibble(x = list(list(a = 1), list(a = 2), list(a = 3)))
View(tbl)

Screen Shot 2019-06-26 at 12 31 28 PM

@lionel-
Copy link
Member

lionel- commented Jun 27, 2019

Thinking more about it, I think this default method for list_of is reasonable:

#' @export
as.character.vctrs_list_of <- function(x, ...) {
  map_chr(x, as_label)
}

This gives:

Screenshot 2019-06-27 at 09 37 28

WDYT @hadley?

@hadley
Copy link
Member

hadley commented Jun 27, 2019

Would it be better to return vec_ptype_full()?

@lionel-
Copy link
Member

lionel- commented Jun 27, 2019

I think it's generally better to return a single-line string per row from as.character()? It could be vec_ptype_abbr() given that list_of() cannot contain scalars.

@hadley
Copy link
Member

hadley commented Jun 27, 2019

We could use vec_ptype_abbr() and then which to vec_ptype_line() when it exists?

@lionel-
Copy link
Member

lionel- commented Jun 27, 2019

Sounds good!

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

No branches or pull requests

5 participants