forked from tidyverse/dplyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopy-to.r
52 lines (48 loc) · 1.69 KB
/
copy-to.r
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#' Copy a local data frame to a remote src
#'
#' This function uploads a local data frame into a remote data source, creating
#' the table definition as needed. Wherever possible, the new object will be
#' temporary, limited to the current connection to the source.
#'
#' @param dest remote data source
#' @param df local data frame
#' @param name name for new remote table.
#' @param overwrite If `TRUE`, will overwrite an existing table with
#' name `name`. If `FALSE`, will throw an error if `name` already
#' exists.
#' @param ... other parameters passed to methods.
#' @seealso [collect()] for the opposite action; downloading remote data into
#' a local dbl.
#' @return a `tbl` object in the remote source
#' @export
#' @examples
#' \dontrun{
#' iris2 <- dbplyr::src_memdb() %>% copy_to(iris, overwrite = TRUE)
#' iris2
#' }
copy_to <- function(dest, df, name = deparse(substitute(df)),
overwrite = FALSE, ...) {
UseMethod("copy_to")
}
#' Copy tables to same source, if necessary
#'
#' @param x,y `y` will be copied to `x`, if necessary.
#' @param copy If `x` and `y` are not from the same data source,
#' and `copy` is `TRUE`, then `y` will be copied into the
#' same src as `x`. This allows you to join tables across srcs, but
#' it is a potentially expensive operation so you must opt into it.
#' @param ... Other arguments passed on to methods.
#' @export
auto_copy <- function(x, y, copy = FALSE, ...) {
if (same_src(x, y)) return(y)
if (!copy) {
glubort(NULL, "`x` and `y` must share the same src, ",
"set `copy` = TRUE (may be slow)"
)
}
UseMethod("auto_copy")
}
#' @export
auto_copy.data.frame <- function(x, y, copy = FALSE, ...) {
as.data.frame(y)
}