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

Can't save patchwork object in a list column in data.table when group #6395

Closed
person-c opened this issue Aug 24, 2024 · 3 comments
Closed

Comments

@person-c
Copy link

person-c commented Aug 24, 2024

see #5685

Are there any ways to walk around this?

r$> mtDT <- setDT(copy(mtcars))

    mtDT[, .(plot = list(ggplot(data = .SD) + geom_point(aes(x=wt, y=mpg)))), by=.(carb,am)][,
    .(list(patchwork::wrap_plots(plot))), by=am]
Error in `X[[i]]`:
! Index out of bounds
Run `rlang::last_trace()` to see where the error occurred.

r$> rlang::last_trace(drop = FALSE)
<error/rlang_error>
Error in `X[[i]]`:
! Index out of bounds
---
Backtrace:
     x
  1. +-...[]
  2. \-data.table:::`[.data.table`(...)
  3.   \-data.table (local) runlock(ans)
  4.     \-base::lapply(x, runlock, current_depth = current_depth + 1L)
  5.       \-data.table (local) FUN(X[[i]], ...)
  6.         \-base::lapply(x, runlock, current_depth = current_depth + 1L)
  7.           \-data.table (local) FUN(X[[i]], ...)
  8.             \-base::lapply(x, runlock, current_depth = current_depth + 1L)
  9.               +-X[[i]]
 10.               \-patchwork:::`[[.patchwork`(X, i)
 11.                 \-cli::cli_abort("Index out of bounds")
 12.                   \-rlang::abort(...)
@rikivillalba
Copy link
Contributor

rikivillalba commented Aug 26, 2024

It appears to be a conflict with patchwork:::[[ and data.table.
data.table looks at x[[1]] perhaps to print or some other stuff.
Custom [[ on "patchwork" indexes on the list x$patches$plots and throws error if the list is empty. For me that is not expected to happen and might be a patchwork bug (as I think [[ is not expected to throw an error at least in base R).

A workaround: unclass then reclass

dt <- mtDT[, .(plot = list(ggplot(data = .SD) + geom_point(aes(x=wt, y=mpg)))), 
     by=.(carb,am)][,.(wp = list(unclass(patchwork::wrap_plots(plot)))), by=.(am)]
dt[ , wp := list(setattr(wp[[1]], "class", c("patchwork", "gg", "ggplot")))]

Then: if you run
dt[,wp][[1]]
The plot is displayed

But if you run ...
dt[,wp[[1]]]

Error in X[[i]]:
! Index out of bounds
Run rlang::last_trace() to see where the error occurred.

If you agree then fill an issue in https://github.com/thomasp85/patchwork.

@person-c
Copy link
Author

Thanks, I will.

@thomasp85
Copy link

[[ certainly throws an out of bound error in base R (e.g. try running list()[[1]])

The issue revolves around the implementation of lapply() and friends which do not seem to honor the dispatching for [[ and length(). I have yet to figure out a way around this but in my opinion it is a bug/constraint of base R

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

No branches or pull requests

3 participants