-
Notifications
You must be signed in to change notification settings - Fork 13
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 |
#' @description | ||
#' | ||
#' `percent` is a lightweight S3 class allowing for pretty | ||
#' printing of proportions as percentages. \cr |
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.
#' printing of proportions as percentages. \cr | |
#' printing of proportions as percentages. |
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.
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.
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.
Sorry, my 'suggestion' was just to remove the \cr
at the end of line 6 which I assume is a typo?
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.
Ah, no I typically use it to add a new line, but maybe there's better syntax for that.
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've checked and can't find an alternative that works surprisingly.
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 \cr
appears verbatim in the .Rd file (
Line 17 in b9a9151
printing of proportions as percentages. \cr |
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.
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.
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 |
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.