Skip to content

Programmatic way to include grobs #24

@teunbrand

Description

@teunbrand

Hi Thomas,

This continues a few comments that we exhanged off-github; I'm having difficulty providing an element_marquee() with a programatically generated list of grobs.

From the code below, I'd like to render the pts$foo grob in the text.

library(marquee)
library(ggplot2)
library(grid)

# Populate element
el  <- ggplot2:::combine_elements(element_marquee(), theme_get()$text)

txt <- "Here be ![](foo) a point"
pts <- list(foo = pointsGrob(0.5, 0.5, default.units = "npc"))
expr <- rlang::expr(element_grob(el, label = txt))

For clear reasons, this wouldn't work out of the box, as the desired grob is in a list somewhere, not the current environment.

grob <- rlang::eval_tidy(expr)
grid.newpage()
grid.draw(grob)

Your suggestion at the time, to use a data mask does not work as I had hoped.

grob <- rlang::eval_tidy(expr, data = pts)
grid.newpage()
grid.draw(grob)

Neither does providing the grobs as the env argument.

grob <- rlang::eval_tidy(expr, env = list2env(pts))
grid.newpage()
grid.draw(grob)

After some trial and error, what I found that did work was to use a local environment. This is fine for my purposes, but I'd prefer it if there were an easier way to do this.

grob <- withr::with_environment(list2env(pts), rlang::eval_tidy(expr))
grid.newpage()
grid.draw(grob)

Created on 2024-06-01 with reprex v2.1.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