Skip to content

Commit

Permalink
Use tidy dots in data_grid
Browse files Browse the repository at this point in the history
Fixes #58
  • Loading branch information
hadley committed May 10, 2018
1 parent 164fb15 commit 08b6442
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Imports:
tibble,
broom,
dplyr,
tidyr (>= 0.6.0)
tidyr (>= 0.8.0)
Suggests:
testthat,
ggplot2,
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# modelr 0.1.1.9000

* `data_grid()` no longer fails with modern tidyr (#58).

* Use more robust calculation of `rsquare()`: 1 - SS_res / SS_tot rather
than SS_reg / SS_tot (#37).

Expand Down
5 changes: 3 additions & 2 deletions R/data-grid.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ data_grid <- function(data, ..., .model = NULL) {

# Generate grid of typical values
needed <- setdiff(predictor_vars(.model), names(expanded))
typical <- tidyr::crossing_(lapply(data[needed], typical))
typical_vals <- lapply(data[needed], typical)
typical_df <- tidyr::crossing(!!!typical_vals)

tidyr::crossing(expanded, typical)
tidyr::crossing(expanded, typical_df)
}
14 changes: 14 additions & 0 deletions tests/testthat/test-data-grid.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
context("test-data-grid.R")

test_that("can generate typical values", {
df <- tibble::tibble(
x = rep(c("a", "b"), each = 5),
y = rep(c("a", "b"), 5),
z = rnorm(10)
)
mod <- lm(z ~ x + y, data = df)
out <- data_grid(df, .model = mod)

expect_equal(out$x, c("a", "a", "b", "b"))
expect_equal(out$y, c("a", "b", "a", "b"))
})

0 comments on commit 08b6442

Please sign in to comment.