properly evaluating `rlang` expression inside list column

守給你的承諾、 提交于 2020-01-06 13:10:00

问题


This is a follow-up question to (using `rlang` for conditional labelling in `ggplot` using `ggrepel`), which solved the problem I was facing in a custom function using an expression to filter out data while labeling data points. But the answer raised another issue that I don't know how to solve.

Here is the custom function that uses rlang to evaluate the user-entered expression to filter out data while attaching labels to data points. And this function works fine when not used inside list columns. For example-

# loading needed libraries
library(tidyverse)
library(ggplot2)
library(ggrepel)

# custom function
label_adder <- function(data, x, y, label.var, exp = NULL) {
  param_list <- as.list(match.call())

  if ("exp" %in% names(param_list)) {
    my_exp <- rlang::enquo(exp)
  }
  else {
    a <- "dplyr::row_number(x = .) > 0"
    my_exp <- rlang::quo(!!rlang::sym(a))
  }

  plot <-
    ggplot(mapping = aes(
      x = !!rlang::enquo(x),
      y = !!rlang::enquo(y)
    )) +
    geom_point(data = data) +
    geom_smooth(data = data, method = "lm") +
    geom_label_repel(
      data = data %>% filter(!!my_exp),
      mapping = aes(label = !!rlang::enquo(label.var))
    )
  return(plot)
}

# using the function
label_adder(
  data = datasets::iris,
  x = Sepal.Length,
  y = Sepal.Width,
  label.var = Species,
  exp = Sepal.Length > 7
)

But when I use the same function with purrr::map, it fails.

# creating a list column
df.listcol <- datasets::iris %>%
  dplyr::mutate(.data = ., Species2 = Species) %>% # just creates a copy of this variable
  dplyr::group_by(.data = ., Species) %>%
  tidyr::nest(data = .)

# running function on dataframe with list columns
df.listcol %>% # creates a nested dataframe with list column called `data`
  dplyr::mutate( # creating a new list column of ggstatsplot outputs
    .data = .,
    plot = data %>%
      purrr::map(
        .x = .,
        .f = ~label_adder(
          data = .,
          x = Sepal.Length,
          y = Sepal.Width
        )
      )
  )
#> Error in mutate_impl(.data, dots): Evaluation error: Evaluation error: object 'dplyr::row_number(x = .) > 0' not found..

But if I use the function by specifying label.var and exp, it works just fine.

# running function on dataframe with list columns
df.listcol %>% # creates a nested dataframe with list column called `data`
  dplyr::mutate( # creating a new list column of ggstatsplot outputs
    .data = .,
    plot = data %>%
      purrr::map(
        .x = .,
        .f = ~label_adder(
          data = .,
          x = Sepal.Length,
          y = Sepal.Width,
          label.var = Species,
          exp = Sepal.Length > 7
        )
      )
  )
#> # A tibble: 3 x 3
#>   Species    data              plot    
#>   <fct>      <list>            <list>  
#> 1 setosa     <tibble [50 x 5]> <S3: gg>
#> 2 versicolor <tibble [50 x 5]> <S3: gg>
#> 3 virginica  <tibble [50 x 5]> <S3: gg>

So my question is why the function fails when label.var and exp are not specified and how to resolve this issue?

Created on 2018-08-31 by the reprex package (v0.2.0.9000).


回答1:


So here we edit the function to make the filtering more flexible.

# loading needed libraries
library(tidyverse)
library(ggplot2)
library(ggrepel)

# custom function
label_adder <- function(data, x, y, label.var, exp = NULL) {
  param_list <- as.list(match.call())
  label_data <- data %>% {if ("exp" %in% names(param_list)) filter(., !!enquo(exp)) else .}

  plot <-
    ggplot(mapping = aes(
      x = !!rlang::enquo(x),
      y = !!rlang::enquo(y)
    )) +
    geom_point(data = data) +
    geom_smooth(data = data, method = "lm") +
    geom_label_repel(
      data = label_data,
      mapping = aes(label = !!rlang::enquo(label.var))
    )
  return(plot)
}

df.listcol <- datasets::iris %>%
  dplyr::mutate(.data = ., Species2 = Species) %>% # just creates a copy of this variable
  dplyr::group_by(.data = ., Species) %>%
  tidyr::nest(data = .)



test <- df.listcol %>% mutate(plot = map(data, ~label_adder(., x = Sepal.Length, label.var = Species2, y = Sepal.Width)))
test
#> # A tibble: 3 x 3
#>   Species    data              plot    
#>   <fct>      <list>            <list>  
#> 1 setosa     <tibble [50 × 5]> <S3: gg>
#> 2 versicolor <tibble [50 × 5]> <S3: gg>
#> 3 virginica  <tibble [50 × 5]> <S3: gg>
test$plot[[2]]

test2 <- df.listcol %>% mutate(plot = map(data, ~label_adder(., x = Sepal.Length, label.var = Species2, y = Sepal.Width, exp = Sepal.Length > 6.5)))
test2
#> # A tibble: 3 x 3
#>   Species    data              plot    
#>   <fct>      <list>            <list>  
#> 1 setosa     <tibble [50 × 5]> <S3: gg>
#> 2 versicolor <tibble [50 × 5]> <S3: gg>
#> 3 virginica  <tibble [50 × 5]> <S3: gg>
test2$plot[[2]]

Created on 2018-08-31 by the reprex package (v0.2.0).



来源:https://stackoverflow.com/questions/52117181/properly-evaluating-rlang-expression-inside-list-column

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!