Closed ijlyttle closed 6 years ago
If the intention is to be able to print the "Chart" object without calling vegalite explicitly, adding just this:
#' @export
print.altair.vegalite.v2.api.Chart <- function(object, ...) {
print(vegalite(object))
}
Seems to do the right thing for printing the basic example mtcars plot. Might need to figure out the exact class name or number of generics needed so all Altair objects that should print as graph to do so...
(Based on s3 method section of documentation for reticulate: https://rstudio.github.io/reticulate/articles/package.html)
I had tried what you suggested and things worked, but I could not get the plot to "print" inline in an html notebook.
Let me restore that code, and I will write-up more carefully what works and what doesn't. I like your idea that there are more classes or generics out there that may need to be accounted-for.
I have restored the print method, and added a knit_print method. The big thing still not working is inline plots in Rmd notebooks.
I think there is still some work to do on the knit_print method to get the dimensions to work out.
Re-visiting print... seems to work okay (with just a print method) in notebook on my computer (after I purged my .Rproj.users/shared/notebooks... was getting errors due to older versions of vegalite.js being used due to using some historical version).
One thought about print: I think it could be useful to be able to set "global" defaults for vegalite. Similar to how theme_set
works in ggplot2. Thus if you want all your altair plots to have tooltips or to have certain vega-embed setting or width/height, you could set that with something like vegalite_set_options
. Then you could just print altair objects without explicitly calling vegalite, using the options you set once with vegalite_set_options
.
As usual, looks like you are a few steps ahead of me :smile: Just saw that you added a check for embed <- getOption("altair.embed_options")
in vegalite
...
I have had the same ideas about themes and options!
The embed_options
comes from something that is brewing at Altair, see the "Renderer metadata" section on this comment: https://github.com/altair-viz/altair/pull/776#issue-184054675
It sounds like they are doing something similar with themes, but it is (as yet) unclear to me how a user might create a custom theme. Perhaps this is something to ask about after pycon is over, and they have some spare cycles :)
Tooltips may be more problematic to specify in a theme because they are now in the spec. However, tooltip themes (light, dark, etc.) are being specified in the embedding options, so I think that could be an option. (example)
Thanks, too, for the tip on the purging - looking forward to trying that out!
Themes in vega-embed are currently supported as json config files, however the said feature is flagged as experimental. What I haven't checked yet is how themes will interact with the user's spec. I assume the behaviour would be in line with the config block in a spec, which would mean lowest priority in terms of conflict.
This issue will be closed soon because the print()
and knit_print()
methods have been implemented in the develop
branch. We should open another issue on themes, but it seems this will take a while before it will be implemented in Altair.
see
https://stackoverflow.com/questions/22357534/print-method-for-referenceclass?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa
and
http://adv-r.had.co.nz/OO-essentials.html#rc