0

Have read this post already, I still suspect my solution is in there somewhere.

I would like to refer to a variable name by pasting the variable name together. In this case the breaks argument to ggplot::scale_x_continuous()

dput(breaks_7)
c("-3\n$0", "-2\n$1", "-1\n$3", "0\n$10", "1\n$33", "2\n$108", 
"3\n$351", "-3\n$0", "-2\n$1", "-1\n$4", "0\n$11", "1\n$35", 
"2\n$108", "3\n$333", "-3\n$0", "-2\n$1", "-1\n$3", "0\n$8", 
"1\n$25", "2\n$79", "3\n$249", "-3\n$0", "-2\n$1", "-1\n$3", 
"0\n$8", "1\n$25", "2\n$76", "3\n$234", "-3\n$0", "-2\n$1", "-1\n$4", 
"0\n$11", "1\n$32", "2\n$95", "3\n$277", "-3\n$0", "-2\n$1", 
"-1\n$4", "0\n$11", "1\n$31", "2\n$92", "3\n$270", "-3\n$1", 
"-2\n$2", "-1\n$6", "0\n$19", "1\n$62", "2\n$199", "3\n$638", 
"-3\n$1", "-2\n$2", "-1\n$7", "0\n$22", "1\n$70", "2\n$225", 
"3\n$727", "-3\n$0", "-2\n$2", "-1\n$6", "0\n$21", "1\n$76", 
"2\n$278", "3\n$1,023", "-3\n$1", "-2\n$2", "-1\n$6", "0\n$22", 
"1\n$75", "2\n$257", "3\n$879", "-3\n$0", "-2\n$0", "-1\n$2", 
"0\n$10", "1\n$42", "2\n$185", "3\n$811", "-3\n$0", "-2\n$1", 
"-1\n$2", "0\n$8", "1\n$34", "2\n$138", "3\n$557")

breaks_7 %>% glimpse
 chr [1:84] "-3\n$0" "-2\n$1" "-1\n$3" "0\n$10" "1\n$33" "2\n$108" "3\n$351" "-3\n$0" "-2\n$1" "-1\n$4" "0\n$11" "1\n$35" "2\n$108" "3\n$333" "-3\n$0" "-2\n$1" "-1\n$3" "0\n$8" "1\n$25" "2\n$79" "3\n$249" "-3\n$0" ...

breaks_7 is a chr vector of length 84. I'd like to refer to breaks_7 by pasting the string 'breaks_' with the number 7 paste0('breaks_', 7)

Trying to get expected outcome in the console with glimpse:

chr [1:84] "-3\n$0" "-2\n$1" "-1\n$3" "0\n$10" "1\n$33" "2\n$108" "3\n$351" "-3\n$0" "-2\n$1" "-1\n$4" "0\n$11" "1\n$35" "2\n$108" "3\n$333" "-3\n$0" "-2\n$1" "-1\n$3" "0\n$8" "1\n$25" "2\n$79" "3\n$249" "-3\n$0" ...

Tried:

!! paste0('breaks_', 7) %>% glimpse
 chr "breaks_7"
Error in !paste0("breaks_", 7) %>% glimpse : invalid argument type

Tried:

!! rlang::sym(paste0('breaks_', 7)) %>% glimpse
 symbol breaks_7
Error in !rlang::sym(paste0("breaks_", 7)) %>% glimpse : 
  invalid argument type

Tried:

as.symbol(paste0('breaks_', 7)) %>% glimpse
 symbol breaks_7

Tried:

!! as.symbol(paste0('breaks_', 7)) %>% glimpse
 symbol breaks_7
Error in !as.symbol(paste0("breaks_", 7)) %>% glimpse : 
  invalid argument type

How can I glimpse breaks_7 by pasting together 'breaks_' and the number 7?

Doug Fir
  • 19,971
  • 47
  • 169
  • 299

2 Answers2

1

Quasiquations need to be wrapped inside a rlang::expr() to perform the arithmetic that constructs the expression. The expression can then be evaluated with the standard eval():

rlang::expr(
  !! rlang::sym(paste0('breaks_', 7)) %>% glimpse    # as in the question
) %>% eval
Artem Sokolov
  • 13,196
  • 4
  • 43
  • 74
1

I think you're just looking for get:

get(paste0("breaks_", 7))
#>  [1] "-3\n$0"    "-2\n$1"    "-1\n$3"    "0\n$10"    "1\n$33"    "2\n$108"   "3\n$351"  
#>  [8] "-3\n$0"    "-2\n$1"    "-1\n$4"    "0\n$11"    "1\n$35"    "2\n$108"   "3\n$333"  
#> [15] "-3\n$0"    "-2\n$1"    "-1\n$3"    "0\n$8"     "1\n$25"    "2\n$79"    "3\n$249"  
#> [22] "-3\n$0"    "-2\n$1"    "-1\n$3"    "0\n$8"     "1\n$25"    "2\n$76"    "3\n$234"  
#> [29] "-3\n$0"    "-2\n$1"    "-1\n$4"    "0\n$11"    "1\n$32"    "2\n$95"    "3\n$277"  
#> [36] "-3\n$0"    "-2\n$1"    "-1\n$4"    "0\n$11"    "1\n$31"    "2\n$92"    "3\n$270"  
#> [43] "-3\n$1"    "-2\n$2"    "-1\n$6"    "0\n$19"    "1\n$62"    "2\n$199"   "3\n$638"  
#> [50] "-3\n$1"    "-2\n$2"    "-1\n$7"    "0\n$22"    "1\n$70"    "2\n$225"   "3\n$727"  
#> [57] "-3\n$0"    "-2\n$2"    "-1\n$6"    "0\n$21"    "1\n$76"    "2\n$278"   "3\n$1,023"
#> [64] "-3\n$1"    "-2\n$2"    "-1\n$6"    "0\n$22"    "1\n$75"    "2\n$257"   "3\n$879"  
#> [71] "-3\n$0"    "-2\n$0"    "-1\n$2"    "0\n$10"    "1\n$42"    "2\n$185"   "3\n$811"  
#> [78] "-3\n$0"    "-2\n$1"    "-1\n$2"    "0\n$8"     "1\n$34"    "2\n$138"   "3\n$557" 
Allan Cameron
  • 147,086
  • 7
  • 49
  • 87