tidyverse / ggplot2

An implementation of the Grammar of Graphics in R
https://ggplot2.tidyverse.org
Other
6.39k stars 2k forks source link

Don't require dplyr for `fortify.tbl()` #5969

Closed teunbrand closed 1 day ago

teunbrand commented 2 days ago

This PR aims to fix #4786.

Briefly, it attempts to exploit that some as.data.frame() methods will implicitly use collect() to realise a local data.frame. For example:

dbplyr:::as.data.frame.tbl_sql
#> function (x, row.names = NULL, optional = NULL, ..., n = Inf) 
#> {
#>     as.data.frame(collect(x, n = n))
#> }
#> <bytecode: 0x000001b7f118b958>
#> <environment: namespace:dbplyr>

This should ensure that you can still work with e.g. <tbl_sql> classes that will be collect()'ed while not requiring {dplyr} for the fortify.tbl() method. Example demonstrating we can still use lazy data.frames:

devtools::load_all("~/packages/ggplot2")
#> ℹ Loading ggplot2
library(dbplyr)
con <- DBI::dbConnect(RSQLite::SQLite(), ":memory:")
dplyr::copy_to(con, mtcars)
df <- dplyr::tbl(con, "mtcars")

ggplot(df, aes(mpg, disp)) +
  geom_point()

Created on 2024-07-02 with reprex v2.1.0

teunbrand commented 1 day ago

Thanks for the review!