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

Search registered S3 method for melt before redirecting to reshape2 #4864

Merged
merged 13 commits into from
Aug 19, 2021

Conversation

odelmarcelle
Copy link
Contributor

@odelmarcelle odelmarcelle commented Jan 2, 2021

Follow-up of #3633 and #3763

The condition leading to UseMethod in the melt generic is not true for non-data.table object. Hence, S3 methods for melt defined by other packages may be skipped.

I initially suggested changing that condition in order to search if a S3 method exists for the current object before redirecting to reshape2.
After giving it a second thought, it appears more correct to performs the redirection to reshape2 in a default method. This leaves the generic clean and allows registering new methods.

Since there were no tests on the generic melt, I left the test file as-is.

After the change, using S3 method dispatch on a custom object works as intended:

library(data.table)
# Creating a S3 object and it's melt method
S3Object = list(dt1 = data.table(id = 1:2, a = 3L), dt2 = data.table(id = 1:2, b = 4:5))
class(S3Object) = "S3Class"
new_melt = function(data, ..., na.rm = FALSE, value.name = "value") {
  melt(Reduce(merge, data), id.vars = "id")
}
.S3method("melt", "S3Class", new_melt)

melt(S3Object) ## Working fine

@codecov
Copy link

codecov bot commented Jan 2, 2021

Codecov Report

Merging #4864 (293a9e2) into master (9e43317) will decrease coverage by 0.00%.
The diff coverage is 100.00%.

❗ Current head 293a9e2 differs from pull request most recent head f06bbc5. Consider uploading reports for the commit f06bbc5 to get more accurate results
Impacted file tree graph

@@            Coverage Diff             @@
##           master    #4864      +/-   ##
==========================================
- Coverage   99.54%   99.54%   -0.01%     
==========================================
  Files          76       76              
  Lines       14692    14691       -1     
==========================================
- Hits        14625    14624       -1     
  Misses         67       67              
Impacted Files Coverage Δ
R/fmelt.R 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 9e43317...f06bbc5. Read the comment docs.

R/fmelt.R Outdated
data_name = deparse(substitute(data))
ns = tryCatch(getNamespace("reshape2"), error=function(e)
stop("The melt generic in data.table has been passed a ", class(data)[1L],", but data.table::melt currently only has a method for data.tables. Please confirm your input is a data.table, with setDT(", data_name, ") or as.data.table(", data_name, "). If you intend to use a method from reshape2, try installing that package first, but do note that reshape2 is deprecated and you should be migrating your code away from using it."))
warning("The melt generic in data.table has been passed a ", class(data)[1L], " and will attempt to redirect to the relevant reshape2 method; please note that reshape2 is deprecated, and this redirection is now deprecated as well. To continue using melt methods from reshape2 while both libraries are attached, e.g. melt.list, you can prepend the namespace like reshape2::melt(", data_name, "). In the next version, this warning will become an error.")
Copy link
Member

@jangorecki jangorecki Jan 2, 2021

Choose a reason for hiding this comment

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

I think we are now going into new major version so this code could be simplified for raising error, as mentioned in the warning already.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you mean stopping the redirection towards reshape2?

@odelmarcelle odelmarcelle marked this pull request as draft April 8, 2021 07:59
@odelmarcelle
Copy link
Contributor Author

odelmarcelle commented Apr 8, 2021

Committed some additional changes and resolved conflicts. I still think having melt as a generic method makes sense. For example, it could serve to melt an S3 object containing multiple tables and matrices into a single long "melted" format.

I have added a note regarding the change in the NEWS and I added a test on melt.default in test.Rraw.

I think that retaining the redirection towards reshape2 is still useful. As Hadley mentioned in #4908, reshape2 will remain functional for the foreseeable future.

Please let me know if there is anything I missed in this pull request.

@odelmarcelle odelmarcelle marked this pull request as ready for review April 8, 2021 09:56
@odelmarcelle odelmarcelle requested a review from jangorecki April 14, 2021 01:52
@mattdowle mattdowle added this to the 1.14.1 milestone Aug 19, 2021
@mattdowle
Copy link
Member

Thanks @odelmarcelle. Please let me know if your name is different to your id, for the contributor list displayed on CRAN. Have invited you to be project member too: it should be a button in your GitHub profile or projects page that you need to click to accept.

@mattdowle mattdowle merged commit 3849bed into Rdatatable:master Aug 19, 2021
mattdowle added a commit that referenced this pull request Aug 19, 2021
@odelmarcelle
Copy link
Contributor Author

Thank you for reviewing and merging @mattdowle. Although I'm not sure I deserve to be listed in the contributor list for such a small input, my actual name is different from my Github username.

Could you perhaps correct my name to "Olivier Delmarcelle"? I corrected the DESCRIPTION file on my forked branch, but I'm not sure how to propagate this to the master branch now that this pull request is merged. Sorry for the trouble!

mattdowle added a commit that referenced this pull request Aug 25, 2021
@mattdowle
Copy link
Member

Thanks @odelmarcelle, done in 76cfe11.

@jangorecki jangorecki modified the milestones: 1.14.9, 1.15.0 Oct 29, 2023
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.

3 participants