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

Support formula in values_fn and values_transform #745

Closed
mgirlich opened this issue Jan 5, 2022 · 0 comments · Fixed by #746
Closed

Support formula in values_fn and values_transform #745

mgirlich opened this issue Jan 5, 2022 · 0 comments · Fixed by #746

Comments

@mgirlich
Copy link
Collaborator

mgirlich commented Jan 5, 2022

library(dbplyr)

lazy_frame(x = 1) %>%
  tidyr::pivot_longer(x, values_transform = list(value = as.character))
#> <SQL>
#> SELECT 'x' AS `name`, CAST(`x` AS TEXT) AS `value`
#> FROM `df`

lazy_frame(x = 1) %>%
  tidyr::pivot_longer(x, values_transform = list(value = ~ as.character(.x)))
#> Error: Result 1 must be a single string, not NULL of length 0


spec1 <- tibble::tibble(.name = "x", .value = "val", key = "x")
lazy_frame(a = c(1, 1, 2), key = c("x", "x", "x"), val = c(1, 10, 100)) %>% 
  dbplyr:::dbplyr_pivot_wider_spec(spec1, values_fn = sum)
#> <SQL>
#> SELECT `a`, SUM(CASE WHEN (`key` = 'x') THEN (`val`) END) AS `x`
#> FROM `df`
#> GROUP BY `a`

lazy_frame(a = c(1, 1, 2), key = c("x", "x", "x"), val = c(1, 10, 100)) %>% 
  dbplyr:::dbplyr_pivot_wider_spec(spec1, values_fn = ~ sum(.x, na.rm = TRUE))
#> Error: Can't convert NULL to a string

Created on 2022-01-05 by the reprex package (v2.0.1)

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 a pull request may close this issue.

1 participant