0

I would like to create a tibble that contains a nested tibble, but also a column outside the nested tibble that refers to a column header within the nested tibble. This is what I mean

library(tidyverse)
tib <- tibble(dat = list(tibble(xvar = 1, freq = 2, rate = 3.2)),
              yvar = c(freq)
             )

However I get the error "Error in eval_tidy(xs[[j]], mask) : object 'freq' not found"

I want to use the cell contents of tib$yvar as the input into a function that contains a ggplot function eg.

plot_datapoint <- function(dat = tib){
     dat_tib <- dat %>% unnest(cols = c(dat))

ggplot(dat_tib, aes(x=xvar, y = yvar)+
geom_point()

I believe the solution has something to do with quosures, but don't know how to implement it.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Andrew
  • 111
  • 4
  • the error message is pretty clear - you need to define the object `freq` first. How is the data structured with which you intend to create that tibble? Your approach of first creating a tibble and then unnesting it before passing it to ggplot seems rather ... not ideal to me. – tjebo Jun 16 '22 at 12:13
  • 2
    Would `tibble(dat = list(tibble(xvar = 1, freq = 2, rate = 3.2)), yvar = pluck(dat, 1, "freq"))` be what you want? – Bas Jun 16 '22 at 12:16
  • 1
    @Bas this is pretty nice. pluck certainly wasn't on my radar till now. seems pretty cool that it can evaluate this as such – tjebo Jun 16 '22 at 12:48
  • 1
    That solution looks pretty impressive @Bas. Thank you for offering this solution! – Andrew Jun 16 '22 at 22:06

0 Answers0