3

How can I use ensym or another appropriate function that will store this as a variable and assign a value to it.

For example:

quest <- 'a'
list(rlang::ensym(quest) = 1)
>Error: unexpected '=' in "list(rlang::ensym(quest)="

Expected output:

$a
[1] 1
Phil
  • 7,287
  • 3
  • 36
  • 66
Working dollar
  • 306
  • 2
  • 10

3 Answers3

3

fully rlang approach might be

library(rlang)
quest <- 'a'
list2(!!(quest) := 1)
Nir Graham
  • 2,567
  • 2
  • 6
  • 10
1

With tidyverse, we can use dplyr::lst

dplyr::lst(!! quest := 1)
$a
[1] 1

Or in base R, use setNames

setNames(list(1), quest)
$a
[1] 1
akrun
  • 874,273
  • 37
  • 540
  • 662
1

Here is an alternative way:

  1. Create an empty list lst.
  2. Use [[ to add a new element to the list with the name specified by quest variable and assign the value 1 to it:
quest <- 'a'
lst <- list()
lst[[quest]] <- 1
lst

$a
[1] 1
TarJae
  • 72,363
  • 6
  • 19
  • 66