daattali / ggExtra

📊 Add marginal histograms to ggplot2, and more ggplot2 enhancements
http://daattali.com/shiny/ggExtra-ggMarginal-demo/
Other
383 stars 48 forks source link

plots don't print in-line in R Notebooks #133

Closed Aariq closed 5 years ago

Aariq commented 5 years ago

ggMarginal() plots don't print in-line or in the Viewer tab (i.e. they don't print at all) in R Notebooks. ggExtra notebook bug.Rmd.zip

crew102 commented 5 years ago

Hey @Aariq , a few notes:

---
title: "R Notebook"
output: html_notebook
---
```{r}
library(ggplot2)
library(ggExtra)

p <- ggplot(trees, aes(x = Height, y = Girth)) + geom_point() +
  geom_smooth(method = "lm")

p_marg <- ggMarginal(p, type = "histogram")
library(grid)
grid.newpage()
grid.draw(p_marg)

The preview shows you a rendered HTML copy of the contents of the editor. Consequently, unlike Knit, Preview does not run any R code chunks. Instead, the output of the chunk when it was last run in the editor is displayed.

...so if you want to see your ggMarginal plot in a preview, you have to run the code chunks responsible for creating the plot first.

Aariq commented 5 years ago

Thanks! Sorry I didn't find #89 before posting this. Is there a reason why grid.draw() isn't the default print method? It would be nice if this just "worked", if you know what I mean.

crew102 commented 5 years ago

It actually is:

https://github.com/daattali/ggExtra/blob/4468f2113ecdd4d4fa2f8d380f843d2ac917b4db/R/ggMarginal.R#L191-L194

I didn't keep up with #89 so I'm not sure why ggMarginal needs to use the 2-chunk approach to printing in notebooks/Rmarkdown docs.

daattali commented 5 years ago

I'm still not sure why notebooks/rmd behave this way, and whether or not this is a big. If you follow the links in #89 you'll see this is not a resolved question. This information is also available in the README (which is, admittedly, a bit long). If you solved the problem (even though we don't know the root of the issue), you can close the issue

Aariq commented 5 years ago

I don't really know what the code does here, but this change makes it work inline:

print.ggExtraPlot <- function(x, newpage = is.null(vp), vp = NULL,...) {
  if (newpage) grid::grid.newpage()
  grid::grid.draw(x)
}

I kind of just haphazardly copied things from print.ggplot() until I found something that works. Happy to submit a PR if this doesn't break things. I don't know what newpage = grDevices::def.interactive() was doing.

crew102 commented 5 years ago

Hey @Aariq , thanks for attempting to fix...Unfortunately your fix would re-introduce the problem fixed in https://github.com/daattali/ggExtra/issues/48, so I'm thinking we leave things as is for now.

Aariq commented 5 years ago

Got it.