From de70321f608b47c7847b8df69a4f5310df1257a8 Mon Sep 17 00:00:00 2001 From: Romain Francois Date: Mon, 20 May 2019 11:30:44 -0400 Subject: [PATCH] colwise functions support constants in formulas; closes #4374. --- NEWS.md | 2 ++ inst/include/dplyr/hybrid/Expression.h | 3 +++ tests/testthat/test-colwise-mutate.R | 12 ++++++++++++ 3 files changed, 17 insertions(+) diff --git a/NEWS.md b/NEWS.md index d33a896faa..66f92436df 100644 --- a/NEWS.md +++ b/NEWS.md @@ -2,6 +2,8 @@ * `rename_at()` handles empty selection (#4324). +* colwise functions support constants in formulas (#4374). + # dplyr 0.8.1 ## Breaking changes diff --git a/inst/include/dplyr/hybrid/Expression.h b/inst/include/dplyr/hybrid/Expression.h index f9383ea705..c0555a05e5 100644 --- a/inst/include/dplyr/hybrid/Expression.h +++ b/inst/include/dplyr/hybrid/Expression.h @@ -139,6 +139,9 @@ class Expression { if (TYPEOF(head) == CLOSXP && Rf_inherits(head, "inline_colwise_function")) { dot_alias = CADR(expr); expr = CADR(Rf_getAttrib(head, symbols::formula)); + if (TYPEOF(expr) != LANGSXP) { + return; + } head = CAR(expr); } diff --git a/tests/testthat/test-colwise-mutate.R b/tests/testthat/test-colwise-mutate.R index 57702408f0..e7be7c89be 100644 --- a/tests/testthat/test-colwise-mutate.R +++ b/tests/testthat/test-colwise-mutate.R @@ -405,3 +405,15 @@ test_that("summarise_at() can refer to local variables and columns (#4304)", { }) }) + +test_that("colwise mutate handles formulas with constants (#4374)", { + expect_identical( + tibble(x = 12) %>% mutate_all(~ 42), + tibble(x = 42) + ) + expect_identical( + tibble(x = 12) %>% mutate_at("x", ~ 42), + tibble(x = 42) + ) +}) +