{"action":"created","issue":{"url":"https://api.github.com/repos/r-lib/vctrs/issues/136","repository_url":"https://api.github.com/repos/r-lib/vctrs","labels_url":"https://api.github.com/repos/r-lib/vctrs/issues/136/labels{/name}","comments_url":"https://api.github.com/repos/r-lib/vctrs/issues/136/comments","events_url":"https://api.github.com/repos/r-lib/vctrs/issues/136/events","html_url":"https://github.com/r-lib/vctrs/issues/136","id":378405363,"node_id":"MDU6SXNzdWUzNzg0MDUzNjM=","number":136,"title":"Base R coercion methods should use shape_broadcast()","user":{"login":"DavisVaughan","id":19150088,"node_id":"MDQ6VXNlcjE5MTUwMDg4","avatar_url":"https://avatars3.githubusercontent.com/u/19150088?v=4","gravatar_id":"","url":"https://api.github.com/users/DavisVaughan","html_url":"https://github.com/DavisVaughan","followers_url":"https://api.github.com/users/DavisVaughan/followers","following_url":"https://api.github.com/users/DavisVaughan/following{/other_user}","gists_url":"https://api.github.com/users/DavisVaughan/gists{/gist_id}","starred_url":"https://api.github.com/users/DavisVaughan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/DavisVaughan/subscriptions","organizations_url":"https://api.github.com/users/DavisVaughan/orgs","repos_url":"https://api.github.com/users/DavisVaughan/repos","events_url":"https://api.github.com/users/DavisVaughan/events{/privacy}","received_events_url":"https://api.github.com/users/DavisVaughan/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-11-07T18:05:08Z","updated_at":"2018-11-07T18:18:35Z","closed_at":null,"author_association":"CONTRIBUTOR","body":"Currently, this is being used:\r\n\r\n```r\r\nas.double.vctrs_vctr <- function(x, ...) {\r\n  vec_cast(x, double())\r\n}\r\n```\r\n\r\nBut if I have a class that extends `vctrs_vctr` and it a matrix-ish class, then this will fail:\r\n\r\n```r\r\nas.double(my_vctrs_mtrx_obj)\r\n```\r\n\r\nIt fails because the `to` shape is too strict. It should adapt to `x`. You could just do:\r\n\r\n```r\r\nas.double.vctrs_vctr <- function(x, ...) {\r\n  vec_cast(x, shape_broadcast(double(), x))\r\n}\r\n```\r\n\r\nThis would probably be implemented for `double`, `integer`, `logical` and `character`."},"comment":{"url":"https://api.github.com/repos/r-lib/vctrs/issues/comments/436724747","html_url":"https://github.com/r-lib/vctrs/issues/136#issuecomment-436724747","issue_url":"https://api.github.com/repos/r-lib/vctrs/issues/136","id":436724747,"node_id":"MDEyOklzc3VlQ29tbWVudDQzNjcyNDc0Nw==","user":{"login":"DavisVaughan","id":19150088,"node_id":"MDQ6VXNlcjE5MTUwMDg4","avatar_url":"https://avatars3.githubusercontent.com/u/19150088?v=4","gravatar_id":"","url":"https://api.github.com/users/DavisVaughan","html_url":"https://github.com/DavisVaughan","followers_url":"https://api.github.com/users/DavisVaughan/followers","following_url":"https://api.github.com/users/DavisVaughan/following{/other_user}","gists_url":"https://api.github.com/users/DavisVaughan/gists{/gist_id}","starred_url":"https://api.github.com/users/DavisVaughan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/DavisVaughan/subscriptions","organizations_url":"https://api.github.com/users/DavisVaughan/orgs","repos_url":"https://api.github.com/users/DavisVaughan/repos","events_url":"https://api.github.com/users/DavisVaughan/events{/privacy}","received_events_url":"https://api.github.com/users/DavisVaughan/received_events","type":"User","site_admin":false},"created_at":"2018-11-07T18:18:35Z","updated_at":"2018-11-07T18:18:35Z","author_association":"CONTRIBUTOR","body":"An example:\r\n\r\n``` r\r\nlibrary(vctrs)\r\n\r\nnew_mtr <- function(.data, dim) {\r\n  new_vctr(.data, dim = dim, class = \"vctrs_mtr\")\r\n}\r\n\r\nvec_cast.double.vctrs_mtr <- function(x, to) {\r\n  # like you do in, vec_cast.double.double, I want to have the \r\n  # chance to broadcast here (potentially in my own way)\r\n  x_coerced <- vctrs:::vec_coerce_bare(x, \"double\")\r\n  x_bcasted <- vctrs:::shape_broadcast(x_coerced, to)\r\n  x_bcasted\r\n}\r\n\r\n# need to define dim\r\n`dim<-.vctrs_mtr` <- function(x, value) {\r\n  attr(x, \"dim\") <- value\r\n  x\r\n}\r\n\r\n# need this b/c otherwise the dim attribute of to does not match\r\n# up with the dimensions of x\r\nvec_restore.vctrs_mtr <- function(x, to) {\r\n  x\r\n}\r\n\r\nx <- new_mtr(c(1,2,3,4), dim = c(4,1))\r\n\r\n# great!\r\nvec_cast(x, matrix(1, nrow = 4, ncol = 2))\r\n#>      [,1] [,2]\r\n#> [1,]    1    1\r\n#> [2,]    2    2\r\n#> [3,]    3    3\r\n#> [4,]    4    4\r\n\r\n# not great!\r\nas.double(x)\r\n#> Error: Can't cast <vctrs_mtr> to <double>\r\n#> Can not decrease dimensions\r\n```"}}
{"actor":{"display_login":"DavisVaughan"}}