-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Support for formats other than data.frame #5390
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
Comments
Using bioconductor extensively myself, I think this is a good idea in principle. |
I don't think this is a good idea because as.data.frame(1)
#> 1
#> 1 1
as.data.frame(matrix(1:4, nrow = 2))
#> V1 V2
#> 1 1 3
#> 2 2 4
as.data.frame(NULL)
#> data frame with 0 columns and 0 rows Created on 2023-08-13 with reprex v2.0.2 In general automatic coercion has a tendency to a make a small number of use cases easier at the high cost of making many mistakes harder to discover. I think it would be fine to add these methods individually, but I don't think that a blanket default to |
One problem is that the DataFrame class specifically is that the DataFrame class is defined in the Bioconductor package S4Vectors. Adding support to |
No, I think that importing from BioC is not a good option since it isn't in any sense required for the functioning of ggplot2. For that same reason, I don't think it is a good idea for S4Vectors to import ggplot2. Which leaves a little bit of a dilemma. However, this is precisely what the external generic concept in S7 seems to solve. |
@hadley Those are valid concerns but maybe having a few additional sanity checks like |
@hpages yeah, that might be a reasonable approach. |
Shall we prepare a PR, or what would be a good way to proceed? |
I'll work on a PR in the next few days. Thanks @hadley and @antagomir! |
Thanks @hpages and everyone involved in the resolution! |
Awesome. Looking fwd to testing! |
This is ready for testing @antagomir. With this change:
But:
This is a feature! |
Works like a charm for all the use cases that I tested so far. We can and will do some more testing if this PR becomes merged. |
* fortify.default() accepts data-frame-like objects `fortify.default()` now accepts a data-frame-like object granted the object exhibits healthy `dim()`, `colnames()`, and `as.data.frame()` behaviors. Closes #5390. * Update snapshot of ggplot(aes(x = x)) * Improve fortify.default() based on Teun's feedback * Follow style guide a little bit more closely in error messages (see https://style.tidyverse.org/error-messages.html)
Problem Whereas ggplot2 supports
data.frame
, many other data structures are available that could benefit from the ability to use ggplot2 functionality. Examples include e.g. DataFrame, matrix, dgCMatrix, DelayedMatrix, SparseMatrix, etc. Many of these classes supportas.data.frame()
and can be easily converted into a data.frame. However, the need to do this with every ggplot2 function call becomes rapidly very repetitive.Suggested solution The default fortify() method,
ggplot2:::fortify.default()
could just try to callas.data.frame()
on the supplied object. This would directly makeggplot()
work on any object that supportsas.data.frame()
(e.g. DataFrame, matrix, dgCMatrix, DelayedMatrix, SparseMatrix, etc.)Let's load libraries and example data
Usual data.frame works as expected:
DataFrame does not work, and ggplot call throws and error:
At the moment our default solution has been to always add
as.data.frame()
around DataFrame objects, like:There was initial discussion that related to the challenges this adds to teaching standard plotting in ecosystems that rely on classes that are closely related to data.frame but not that.
Initial thought was to solve this in the S4Vectors class (for DataFrame), see the PR by @kevinrue - then @hpages pointed out the more general solution described above.
-> Could ggplot add the
as.data.frame
check to extend the support to other formats thandata.frame
? If yes, we might be able to provide a PR.The text was updated successfully, but these errors were encountered: