Skip to content

View() and dplyr::glimpse() of data frame with plots stored in list column generate error since ggplot2 4.0.0 #6732

@martingmayer

Description

@martingmayer

First, thanks for all the incredible work behind {ggplot2} and the entire tidyverse. Deeply appreciated.

View() error in RStudio (but not VS Code or Positron)

I noticed that since {ggplot2} 4.0.0, attempting to View() data frames that have ggplots stored in a list column generate an error in RStudio. I mention RStudio specifically because VS Code and Positron seem unaffected from my testing.

Basic reprex:

dat <- data.frame(
  outcome = c("total_cvd", "ascvd", "heart_failure", "chd", "stroke"),
  risk = c(0.346, 0.218, 0.260, 0.168, 0.092)
)

plot <- dat |> 
  ggplot2::ggplot(ggplot2::aes(x = outcome, y = risk)) +
  ggplot2::geom_col(fill = "steelblue") +
  ggplot2::geom_text(ggplot2::aes(label = scales::percent(risk)), vjust = -0.5) +
  ggplot2::labs(
    title = "Risk of Cardiovascular Outcome",
    x = "Outcome",
    y = "Risk"
  )

dat_wide <- dat |> 
  tidyr::pivot_wider(
    names_from = outcome,
    values_from = risk
  )

dat_wide_with_plot <- dat_wide |> 
  dplyr::mutate(plot = list(plot)) 

View(dat_wide_with_plot)

This generates the following error in RStudio:

Image

VS Code and Positron, however, handle this just fine.

Here are a couple of additional, even more basic reprexes:

dat <- data.frame(
  outcome = c("total_cvd", "ascvd", "heart_failure", "chd", "stroke"),
  risk = c(0.346, 0.218, 0.260, 0.168, 0.092)
)

plot <- dat |> 
  ggplot2::ggplot(ggplot2::aes(x = outcome, y = risk)) +
  ggplot2::geom_col(fill = "steelblue")

dat_wide_with_plot <- dplyr::mutate(
  dat |> tidyr::pivot_wider(names_from = outcome, values_from = risk),
  plot = list(plot)
)

View(dat_wide_with_plot)
dat_with_plot <- tibble::tibble(
  thing = "foo",
  stuff = "bar",
  plot = list(ggplot2::ggplot())
)

View(dat_with_plot)

Again, these examples View() fine in VS Code and Positron, but yield the same kind of error in RStudio. Rolling {ggplot2} back to 3.5.2 eliminates the error in RStudio.

dplyr::glimpse() error in RStudio, Positron, and VS Code

In addition, intriguingly, dplyr::glimpse()ing any of the reprexes presented to demonstrate the View() error in RStudio generates the same kind of error across RStudio, Positron, and VS Code with {ggplot2} 4.0.0.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions