0

I have a list here below

params <- list(exp1 = input$exp1, grad_exp1 = input$grad_exp1)

But this is hardcoded. Meaning, when the names are more, I need to add it manually. So my plan is to get a vector like below so that the params gets automated with the list

experiences <- c("exp1", "grad_exp1")

Example

experiences <- c("exp1", "grad_exp1", "grad_exp2")

Expected output

params <- list(exp1 = input$exp1, grad_exp1 = input$grad_exp1, grad_exp2 = input$grad_exp2)
manu p
  • 952
  • 4
  • 10
  • 1
    One-liner: `setNames(params <- input[experiences], experiences)`. Or `params <- input[experiences]; names(params) <- experiences`. – Rui Barradas Mar 05 '23 at 08:25
  • @Rui why do you need to set the names? Seems that `input` is a named list. so `params <- input[experiences]` will give you a named subset – Onyambu Mar 05 '23 at 08:42
  • @onyambu Yes, you are right, I missed that point. This is mostly a duplicate of [Dynamically select data frame columns using $ and a vector of column names](https://stackoverflow.com/questions/18222286/dynamically-select-data-frame-columns-using-and-a-character-value) – Rui Barradas Mar 05 '23 at 09:06
  • Does this answer your question? [Dynamically select data frame columns using $ and a character value](https://stackoverflow.com/questions/18222286/dynamically-select-data-frame-columns-using-and-a-character-value) – Dylan_Gomes Mar 05 '23 at 18:30

0 Answers0