-
Notifications
You must be signed in to change notification settings - Fork 127
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use rlang to coerce fct_relabel input to function (#91) #112
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Unfortunately while the code looks good, the docs and tests need a few tweaks.
NEWS.md
Outdated
@@ -1,5 +1,8 @@ | |||
# forcats 0.2.0.9000 | |||
|
|||
# `fct_relabel()` now accepts objects coercible to functions |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should start with *
, not #
NEWS.md
Outdated
@@ -1,5 +1,8 @@ | |||
# forcats 0.2.0.9000 | |||
|
|||
# `fct_relabel()` now accepts objects coercible to functions | |||
by `rlang::as_function` (#91) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please add your github user name as described in http://style.tidyverse.org/news.html#during-development ?
DESCRIPTION
Outdated
@@ -16,7 +16,8 @@ Depends: | |||
R (>= 3.1) | |||
Imports: | |||
magrittr, | |||
tibble | |||
tibble, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These should be in alphabetical order (that should resolve the merge conflict)
R/relabel.R
Outdated
#' @param fun A function that is applied to each level. Must accept one | ||
#' character argument and return a character vector of the same length as its | ||
#' input. | ||
#' @param fun A function or something coercible to one by [rlang::as_function()] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's more useful to describe exactly what it can be here, rather than requiring the user to read more technical documentation.
R/relabel.R
Outdated
@@ -22,10 +22,14 @@ | |||
#' | |||
#' rincome2 <- fct_relabel(gss_cat$rincome, convert_income) | |||
#' fct_count(rincome2) | |||
#' | |||
#' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please think through the use of blank lines here? Generally they should be used to group together related code. (This may require tweaking to the entire example block)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed the new example to pipes and put the function definition at the start of the second one, so now each example has no blank lines within it, and there's one between them. The fct_count(gss_cat$rincome)
doesn't have to come after the function definition, but I think it's more readable in terms of whitespace.
R/relabel.R
Outdated
#' | ||
#' fct_count(gss_cat$partyid) | ||
#' | ||
#' partyid2 <- fct_relabel(gss_cat$partyid, ~gsub(",", ", ", .x)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this example should come first because it's simpler
tests/testthat/test-fct_relabel.R
Outdated
test_that("function coercion", { | ||
f1 <- factor(letters) | ||
|
||
expect_identical(fct_relabel(f1, ~paste0(.x, ".")), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please use the formatting described at http://style.tidyverse.org/syntax.html#long-lines ?
tests/testthat/test-fct_relabel.R
Outdated
@@ -36,3 +36,10 @@ test_that("additional arguments", { | |||
|
|||
expect_identical(fct_relabel(f1, paste0, "."), factor(paste0(letters, "."))) | |||
}) | |||
|
|||
test_that("function coercion", { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be a sentence
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The others in this file are concise. It's now a sentence, though not capitalized and with a period. If you want that (or for the others to be changed) let me know.
Thanks! For future reference, if you include |
This PR changes
fct_relabel
per issue #91 so it usesrlang::as_function
to treat thefun
parameter, enabling the use of string names of functions, formula functions, and quosure functions.Notes on changes:
Imports
dependency.fun
is a function, asas_function
will always error beforehand anyway.as_function
error messages could change, it may be better to drop the test altogether. For now, I left it with just"function"
as a regex, which is broad enough that it will probably never break even if errors do change.devtools::spell_check
doesn't like with my current dictionary, but it is a word.All tests and checks pass on MacOS.
Let me know if you'd like any reversions or further changes!