Skip to content

Commit

Permalink
Incorporate internal function db_pivot_wider for the time being *
Browse files Browse the repository at this point in the history
It appears that another pivot_wider function is being prepared at dbplyr,
but in the meantime I wanted to prevent the need for my fork of dbplyr, that
took the pivot_wider branch of Edgar Ruiz (who proposed the function)
and brought it up to date with dbplyr's master at times.
By incorporating the function, the watina package can just work with
dbplyr from CRAN and still use the functionality.
All credits for db_pivot_wider() go to Edgar Ruiz.
  • Loading branch information
florisvdh committed Jan 11, 2021
1 parent b048f1b commit e66e58f
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 6 deletions.
7 changes: 4 additions & 3 deletions DESCRIPTION
Expand Up @@ -34,10 +34,11 @@ Imports:
tidyr
Suggests:
knitr,
rmarkdown
purrr,
rmarkdown,
tidyselect
Remotes:
inbo/inbodb,
florisvdh/dbplyr@dbplyr_with_pivot_wider
inbo/inbodb
LazyData: true
Encoding: UTF-8
RoxygenNote: 7.1.1
Expand Down
9 changes: 8 additions & 1 deletion NAMESPACE
Expand Up @@ -23,7 +23,6 @@ importFrom(assertthat,is.flag)
importFrom(assertthat,is.number)
importFrom(assertthat,is.string)
importFrom(assertthat,noNA)
importFrom(dbplyr,db_pivot_wider)
importFrom(dplyr,"%>%")
importFrom(dplyr,anti_join)
importFrom(dplyr,arrange)
Expand Down Expand Up @@ -68,6 +67,14 @@ importFrom(lubridate,now)
importFrom(lubridate,today)
importFrom(lubridate,year)
importFrom(rlang,.data)
importFrom(rlang,enquo)
importFrom(rlang,enquos)
importFrom(rlang,expr)
importFrom(rlang,flatten)
importFrom(rlang,quo_get_expr)
importFrom(rlang,set_names)
importFrom(rlang,sym)
importFrom(rlang,syms)
importFrom(sf,st_as_sf)
importFrom(sf,st_buffer)
importFrom(sf,st_coordinates)
Expand Down
82 changes: 82 additions & 0 deletions R/db_pivot_wider.R
@@ -0,0 +1,82 @@
# NOTE: all credits for this db_pivot_wider function go to Edgar Ruiz.
# He made a PR where this function was proposed to incorporate in dbplyr:
# https://github.com/tidyverse/dbplyr/pull/344
# The code below is a copy and the terms of its original code source apply.
# Minor modifications were made in the approach to import rlang functions and to
# avoid global variables.

#' @importFrom rlang
#' .data
#' sym
#' syms
#' set_names
#' flatten
#' enquo
#' enquos
#' quo_get_expr
#' expr
#' @keywords internal
db_pivot_wider <- function(data,
id_cols = NULL,
names_from = .data$name,
names_prefix = "",
names_sep = NULL,
names_repair = NULL,
values_from = .data$value,
values_fill = NULL,
values_fn = NULL,
spec = NULL) {
if (!requireNamespace("tidyselect", quietly = TRUE)) {
stop("Package \"tidyselect\" is needed when using this function. ",
"Please install it.",
call. = FALSE)
}
if (!requireNamespace("purrr", quietly = TRUE)) {
stop("Package \"purrr\" is needed when using this function. ",
"Please install it.",
call. = FALSE)
}
check_null_pivot_args(
id_cols = !!id_cols, names_sep = !!names_sep,
names_repair = !!names_repair, values_fill = !!values_fill,
values_fn = !!values_fn, spec = !!spec
)
cn <- colnames(data)
names_from <- tidyselect::vars_select(cn, !!enquo(names_from))
values_from <- tidyselect::vars_select(cn, !!enquo(values_from))
pl <- c(values_from, names_from)
kp <- cn[!(cn %in% pl)]
headers <- pull(summarise(group_by(data, !!sym(names_from))))
mt <- purrr::map(
headers,
~ {
header <- .x
purrr::map(
values_from,
~ expr(max(ifelse(!!sym(names_from) == !!header, !!sym(.x), NA), na.rm = TRUE))
)
}
)
fmt <- flatten(mt)
if (length(values_from) > 1) {
vp <- paste0(values_from, "_")
} else {
vp <- ""
}
hn <- purrr::map(headers, ~ paste0(vp, names_prefix, .x))
rhn <- purrr::reduce(hn, c)
nmt <- set_names(fmt, rhn)
grps <- group_by(data, !!!syms(kp))
summarise(grps, !!!nmt)
}

check_null_pivot_args <- function(..., msg = "The `{arg}` argument is not supported for remote back-ends") {
vars <- enquos(...)
purrr::imap(
vars,
~ assert_that(
is.null(quo_get_expr(.x)),
msg = sub("\\{arg\\}", .y, msg)
)
)
}
2 changes: 0 additions & 2 deletions R/get.R
Expand Up @@ -1029,8 +1029,6 @@ get_xg3 <- function(locs,
#' day
#' month
#' year
#' @importFrom dbplyr
#' db_pivot_wider
#' @importFrom dplyr
#' %>%
#' copy_to
Expand Down

0 comments on commit e66e58f

Please sign in to comment.