Skip to content
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

Merged
merged 3 commits into from
Feb 14, 2018

Conversation

alistaire47
Copy link
Contributor

This PR changes fct_relabel per issue #91 so it uses rlang::as_function to treat the fun parameter, enabling the use of string names of functions, formula functions, and quosure functions.

Notes on changes:

  • As discussed in the issue, this introduces an rlang Imports dependency.
  • I dropped the sanity check on whether fun is a function, as as_function will always error beforehand anyway.
    • I changed the accompanying test to reflect the new behavior, but given that 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.
  • I added a test to make sure it works with something that requires coercion to a function.
  • I changed the parameter documentation to reflect the change. It uses the word "coercible", which devtools::spell_check doesn't like with my current dictionary, but it is a word.
  • I added an example of the new behavior.
  • I updated NEWS.md.

All tests and checks pass on MacOS.

Let me know if you'd like any reversions or further changes!

Copy link
Member

@hadley hadley left a 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
Copy link
Member

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)
Copy link
Member

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,
Copy link
Member

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()]
Copy link
Member

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)
#'
#'
Copy link
Member

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)

Copy link
Contributor Author

@alistaire47 alistaire47 Feb 12, 2018

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))
Copy link
Member

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

test_that("function coercion", {
f1 <- factor(letters)

expect_identical(fct_relabel(f1, ~paste0(.x, ".")),
Copy link
Member

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 ?

@@ -36,3 +36,10 @@ test_that("additional arguments", {

expect_identical(fct_relabel(f1, paste0, "."), factor(paste0(letters, ".")))
})

test_that("function coercion", {
Copy link
Member

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

Copy link
Contributor Author

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.

@hadley hadley merged commit 6e5249f into tidyverse:master Feb 14, 2018
@hadley
Copy link
Member

hadley commented Feb 14, 2018

Thanks!

For future reference, if you include Fixes #xyz in a commit message (or the text of the PR), the corresponding issue will be automatically closed when the PR is merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants