Skip to content

rlang::sym not recognized within label_bquote #4141

Closed
@areisett

Description

@areisett

I want to create facet labels dynamically. The labels come from a data frame and contain Greek letters. label_bquote alone fails to replace the Greek letter name stored in a variable with the symbol. Incorporating rlang::sym() around the variable names used to fix the issue, but now throws an error. However, this strategy still works when using bquote. The first piece of code below is from a solution to the stackoverflow post How can I make bquote replace the greek letter stored in a variable with the symbol?

### this works
library("tidyverse")

a <- "alpha"
b <- "beta"

ggplot(data.frame(x = c(1), y = c(1)), aes(x, y)) +
  geom_point() +
  labs(x = bquote(.(sym(a))[.(sym(b))])) +
  labs(y = bquote(alpha[beta]))

### this used to work, but now throws an error
library("tidyverse")
df <- data.frame(x = rep(1:5, 2), 
                 y = rnorm(10),
                 facet_var = rep(c("alpha", "beta"), times = c(5, 5)))

df
#>    x           y facet_var
#> 1  1 -1.14089312     alpha
#> 2  2  0.08233709     alpha
#> 3  3 -1.17177333     alpha
#> 4  4  1.00633917     alpha
#> 5  5 -0.01021232     alpha
#> 6  1 -1.48566764      beta
#> 7  2  0.32726416      beta
#> 8  3 -0.72468232      beta
#> 9  4  0.88635594      beta
#> 10 5  2.04821051      beta

ggplot(df, aes(x, y)) +
  geom_point() +
  facet_grid(facet_var ~ .,
             labeller = label_bquote(rows = .(sym(facet_var))))
#> Error in sym(facet_var): could not find function "sym"

### trying to be more explicit about sym doesn't seem to help
ggplot(df, aes(x, y)) +
  geom_point() +
  facet_grid(facet_var ~ .,
             labeller = label_bquote(rows = .(rlang::sym(facet_var))))
#> Error in rlang::sym: could not find function "::"

sessionInfo()
#> R version 4.0.2 (2020-06-22)
#> Platform: x86_64-w64-mingw32/x64 (64-bit)
#> Running under: Windows 10 x64 (build 18362)
#> 
#> Matrix products: default
#> 
#> locale:
#> [1] LC_COLLATE=English_United States.1252 
#> [2] LC_CTYPE=English_United States.1252   
#> [3] LC_MONETARY=English_United States.1252
#> [4] LC_NUMERIC=C                          
#> [5] LC_TIME=English_United States.1252    
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] forcats_0.5.0   stringr_1.4.0   dplyr_0.8.5     purrr_0.3.4    
#> [5] readr_1.3.1     tidyr_1.1.0     tibble_3.0.1    ggplot2_3.3.0  
#> [9] tidyverse_1.3.0
#> 
#> loaded via a namespace (and not attached):
#>  [1] tidyselect_1.1.0 xfun_0.14        haven_2.3.0      lattice_0.20-41 
#>  [5] colorspace_1.4-1 vctrs_0.3.0      generics_0.0.2   htmltools_0.4.0 
#>  [9] yaml_2.2.1       blob_1.2.1       rlang_0.4.6      pillar_1.4.4    
#> [13] glue_1.4.0       withr_2.2.0      DBI_1.1.0        dbplyr_1.4.4    
#> [17] modelr_0.1.8     readxl_1.3.1     lifecycle_0.2.0  munsell_0.5.0   
#> [21] gtable_0.3.0     cellranger_1.1.0 rvest_0.3.5      evaluate_0.14   
#> [25] labeling_0.3     knitr_1.28       fansi_0.4.1      highr_0.8       
#> [29] broom_0.5.6      Rcpp_1.0.4.6     scales_1.1.1     backports_1.1.6 
#> [33] jsonlite_1.6.1   farver_2.0.3     fs_1.4.1         hms_0.5.3       
#> [37] digest_0.6.25    stringi_1.4.6    grid_4.0.2       cli_2.0.2       
#> [41] tools_4.0.2      magrittr_1.5     crayon_1.3.4     pkgconfig_2.0.3 
#> [45] ellipsis_0.3.0   xml2_1.3.2       reprex_0.3.0     lubridate_1.7.8 
#> [49] assertthat_0.2.1 rmarkdown_2.1    httr_1.4.1       R6_2.4.1        
#> [53] nlme_3.1-148     compiler_4.0.2

Created on 2020-07-20 by the reprex package (v0.3.0)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugan unexpected problem or unintended behavior

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions