-
Notifications
You must be signed in to change notification settings - Fork 14
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
base: master
Are you sure you want to change the base?
Conversation
#' @export | ||
`[.percent` <- function(x, ..., drop = TRUE){ | ||
cl <- oldClass(x) | ||
class(x) <- NULL | ||
out <- NextMethod("[") | ||
class(out) <- cl | ||
out | ||
} |
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.
What does this do?
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.
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
.
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.
We can maybe get rid of NextMethod()
entirely as it can be easily written without it.
We can also replace |
Codecov ReportAttention: Patch coverage is
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. |
I also noticed a small bug where |
I also like the idea of users being able to create percent vectors using numbers representing literal percentages. |
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. |
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 😄 |
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 |
I have an idea for how to deal with formatting digits greater than the default 2 decimal places without having to go through For example let's say the user wants their percent vector to be formatted to 4 decimal places. Furthermore, we can code it so that if a user later decides they want a different number of decimal places, they can use |
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.