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

Added percent functions. #130

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open

Added percent functions. #130

wants to merge 10 commits into from

Conversation

Nic-Chr
Copy link
Contributor

@Nic-Chr Nic-Chr commented Jul 18, 2024

Added percent function and related methods as discussed.

I'm unsure about what the default digits should be set as so I just set them to 2. Maybe we could create an option the user could set for all formatting methods.

R/percent.R Show resolved Hide resolved
R/percent.R Outdated Show resolved Hide resolved
Comment on lines +127 to +134
#' @export
`[.percent` <- function(x, ..., drop = TRUE){
cl <- oldClass(x)
class(x) <- NULL
out <- NextMethod("[")
class(out) <- cl
out
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this do?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It just ensures that the next appropriate method for subsetting is used (in this case numeric vector subsetting) and adds the appropriate class back. That means x[] will always return a percent vector, given that x here is a percent vector.
You can see a pretty similar method used in zoo::[.yearmon.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can maybe get rid of NextMethod() entirely as it can be easily written without it.

R/percent.R Outdated Show resolved Hide resolved
@Nic-Chr
Copy link
Contributor Author

Nic-Chr commented Aug 27, 2024

We can also replace paste() with stringr::str_c() which naturally handles NA values and zero-length vectors.

Copy link

codecov bot commented Aug 27, 2024

Codecov Report

Attention: Patch coverage is 0% with 83 lines in your changes missing coverage. Please review.

Project coverage is 81.45%. Comparing base (d737fcb) to head (322f848).
Report is 2 commits behind head on master.

Files Patch % Lines
R/percent.R 0.00% 83 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           master     #130       +/-   ##
===========================================
- Coverage   97.40%   81.45%   -15.95%     
===========================================
  Files          13       14        +1     
  Lines         424      507       +83     
===========================================
  Hits          413      413               
- Misses         11       94       +83     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@Nic-Chr
Copy link
Contributor Author

Nic-Chr commented Aug 27, 2024

I also noticed a small bug where sign(x) returns a percent vector. Should fix this in the Math group generic.

R/percent.R Outdated Show resolved Hide resolved
R/percent.R Outdated Show resolved Hide resolved
#' @description
#'
#' `percent` is a lightweight S3 class allowing for pretty
#' printing of proportions as percentages. \cr
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#' printing of proportions as percentages. \cr
#' printing of proportions as percentages.

Copy link
Contributor Author

@Nic-Chr Nic-Chr Aug 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it the entire description you want as just that 1 line? Not sure it fully reflects why users might want to use percents.
Edit: Nvm I think it's just line 6.

Copy link
Member

@Moohan Moohan Aug 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, my 'suggestion' was just to remove the \cr at the end of line 6 which I assume is a typo?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, no I typically use it to add a new line, but maybe there's better syntax for that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've checked and can't find an alternative that works surprisingly.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The \cr appears verbatim in the .Rd file (

printing of proportions as percentages. \cr
). I would just put a new line in, no special characters needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe an obvious one but how do I get the documentation to display that line on a new line right below the previous text? If I insert a new line using Enter on my keyboard it places it 2 lines below.

@Nic-Chr
Copy link
Contributor Author

Nic-Chr commented Aug 28, 2024

I also like the idea of users being able to create percent vectors using numbers representing literal percentages.
We could have for example a function called percent or create_percent that converts for example
percent(50) into 50%.
The same as doing as_percent(0.5).

@Moohan
Copy link
Member

Moohan commented Aug 28, 2024

I also like the idea of users being able to create percent vectors using numbers representing literal percentages. We could have for example a function called percent or create_percent that converts for example percent(50) into 50%. The same as doing as_percent(0.5).

I like this idea too. Also prompted the thought that we should have a check (maybe producing a warning), so if someone provides 'numbers that look like percentages' e.g. c(10, 20, 30) to as_percent() it warns them they probably did it wrong. The reverse check and warning could be used on create_percent() (format_percent(), make_percent()?)

@Nic-Chr
Copy link
Contributor Author

Nic-Chr commented Aug 28, 2024

I also like the idea of users being able to create percent vectors using numbers representing literal percentages. We could have for example a function called percent or create_percent that converts for example percent(50) into 50%. The same as doing as_percent(0.5).

I like this idea too. Also prompted the thought that we should have a check (maybe producing a warning), so if someone provides 'numbers that look like percentages' e.g. c(10, 20, 30) to as_percent() it warns them they probably did it wrong. The reverse check and warning could be used on create_percent() (format_percent(), make_percent()?)

I'm a bit apprehensive about this because in general percentages above 100% are completely acceptable depending on the context. For example, in finance, percentage increases above 100% are very normal.

@Moohan
Copy link
Member

Moohan commented Aug 28, 2024

I also like the idea of users being able to create percent vectors using numbers representing literal percentages. We could have for example a function called percent or create_percent that converts for example percent(50) into 50%. The same as doing as_percent(0.5).

I like this idea too. Also prompted the thought that we should have a check (maybe producing a warning), so if someone provides 'numbers that look like percentages' e.g. c(10, 20, 30) to as_percent() it warns them they probably did it wrong. The reverse check and warning could be used on create_percent() (format_percent(), make_percent()?)

I'm a bit apprehensive about this because in general percentages above 100% are completely acceptable depending on the context. For example, in finance, percentage increases above 100% are very normal.

Light touch checks 😄

@Nic-Chr
Copy link
Contributor Author

Nic-Chr commented Aug 28, 2024

I agree in principle that you can certainly have 'incorrect' percentages though I think the need to check for those is probably quite niche and maybe needs a bit of justification.

I would be inclined to go with a 'less-is-more' approach and see how users interact with this new percent object before including checks and warnings that might make things confusing.

In practice we could let the user decide that the function checks for percentages greater than a specified cut-off where the default value is say NULL or 0.

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