forked from tidyverse/ggplot2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfortify.r
49 lines (48 loc) · 1.5 KB
/
fortify.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
#' Fortify a model with data.
#'
#' Rather than using this function, I now recommend using the \pkg{broom}
#' package, which implements a much wider range of methods. `fortify()`
#' may be deprecated in the future.
#'
#' @seealso [fortify.lm()]
#' @param model model or other R object to convert to data frame
#' @param data original dataset, if needed
#' @param ... other arguments passed to methods
#' @export
fortify <- function(model, data, ...) UseMethod("fortify")
#' @export
fortify.data.frame <- function(model, data, ...) model
#' @export
fortify.tbl_df <- function(model, data, ...) model
#' @export
fortify.tbl <- function(model, data, ...) {
check_installed("dplyr", reason = "to work with `tbl` objects")
dplyr::collect(model)
}
#' @export
fortify.NULL <- function(model, data, ...) waiver()
#' @export
fortify.function <- function(model, data, ...) model
# accept purrr-style lambda notation
#' @export
fortify.formula <- function(model, data, ...) as_function(model)
#' @export
fortify.grouped_df <- function(model, data, ...) {
check_installed("dplyr", reason = "to work with `grouped_df` objects")
model$.group <- dplyr::group_indices(model)
model
}
#' @export
fortify.default <- function(model, data, ...) {
msg <- paste0(
"`data` must be a data frame, or other object coercible by `fortify()`, ",
"not ", obj_desc(model), "."
)
if (inherits(model, "uneval")) {
msg <- paste0(
msg, "\n",
"Did you accidentally pass `aes()` to the `data` argument?"
)
}
abort(msg)
}