Skip to content

Commit

Permalink
Define correct dplyr methods (#210)
Browse files Browse the repository at this point in the history
And implicit dbplyr dependency more clear.

Fixes #177. Closes #208.
  • Loading branch information
hadley authored Feb 14, 2024
1 parent 239d5cb commit f9499b2
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 17 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Imports:
jsonlite
Suggests:
crayon,
dbplyr,
flexdashboard,
ggplot2,
gridExtra,
Expand Down
10 changes: 5 additions & 5 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ S3method(api_build,op_base_connect)
S3method(api_build,op_head)
S3method(as.data.frame,tbl_connect)
S3method(collect,tbl_connect)
S3method(dim,tbl_lazy)
S3method(dimnames,tbl_lazy)
S3method(connect_vars,op_base)
S3method(connect_vars,op_single)
S3method(connect_vars,tbl_connect)
S3method(dim,tbl_connect)
S3method(dimnames,tbl_connect)
S3method(head,tbl_connect)
S3method(op_vars,op_base)
S3method(op_vars,op_single)
S3method(op_vars,tbl_lazy)
S3method(print,connect_tag_tree)
S3method(print,tbl_connect)
S3method(vec_cast.fs_bytes,default)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# connectapi (development version)

- Now correctly provides methods for `tbl_connect`, rather than `tbl_lazy`,
preventing problems when also using dplyr (#177).
- BREAKING: The functions `Connect$download_bundle()` and
`Connect$bundle_delete()` have been removed. Use `Content$bundle_download()`
and `Content$bundle_delete()` instead.
Expand Down
25 changes: 13 additions & 12 deletions R/lazy.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#'
#' @export
tbl_connect <- function(src, from = c("users", "groups", "content", "usage_shiny", "usage_static", "audit_logs"), ...) {
rlang::check_required("dbplyr")

validate_R6_class(src, "Connect")

stopifnot(length(from) == 1)
Expand Down Expand Up @@ -59,7 +61,7 @@ check_deprecated_names <- function(.name, deprecated_names) {
#' @importFrom dplyr collect
#' @export
collect.tbl_connect <- function(x, ..., n = Inf) {
api_build(op = x$ops, con = x$src, n = n)
api_build(op = x[["ops"]], con = x[["src"]], n = n)
}

api_build <- function(op, con = NULL, ..., n = NULL) {
Expand Down Expand Up @@ -100,10 +102,10 @@ cat_line <- function(...) {
#' @importFrom utils head
#' @export
head.tbl_connect <- function(x, n = 6L, ...) {
if (inherits(x$ops, "op_head")) {
if (inherits(x[["ops"]], "op_head")) {
x$ops$args$n <- min(x$ops$args$n, n)
} else {
x$ops <- op_single("head", x = x$ops, args = list(n = n))
x$ops <- op_single("head", x = x[["ops"]], args = list(n = n))
}
x
}
Expand Down Expand Up @@ -148,23 +150,22 @@ op_single <- function(name, x, dots = list(), args = list()) {
)
}

# #' @export
op_vars <- function(op) UseMethod("op_vars")
connect_vars <- function(op) UseMethod("connect_vars")
#' @export
op_vars.op_base <- function(op) op$vars
connect_vars.op_base <- function(op) op$vars
#' @export
op_vars.op_single <- function(op) op_vars(op$x)
connect_vars.op_single <- function(op) connect_vars(op$x)
#' @export
op_vars.tbl_lazy <- function(op) op_vars(op$ops)
connect_vars.tbl_connect <- function(op) connect_vars(op[["ops"]])

# important for `nrow`/`ncol` to work
#' @export
dim.tbl_lazy <- function(x) {
c(NA, length(op_vars(x$ops)))
dim.tbl_connect <- function(x) {
c(NA, length(connect_vars(x[["ops"]])))
}

# important for `colnames` to work
#' @export
dimnames.tbl_lazy <- function(x) {
list(NULL, op_vars(x$ops))
dimnames.tbl_connect <- function(x) {
list(NULL, connect_vars(x[["ops"]]))
}

0 comments on commit f9499b2

Please sign in to comment.