Home > Blockchain >  How do I use an expression as the key of a `c()`?
How do I use an expression as the key of a `c()`?

Time:01-18

In R, I can put a concatenation expression in the value of a c() like this:

c(
  foo = paste("first", "second", "third")
)

but I can't do that with the key (because it's not a string)

c(
  paste("first", "second", "third") = "foo"  # Error: unexpected '='
)

is there a way to do it?

CodePudding user response:

Use setNames as = wouldn't allow to evaluate expression on the lhs

setNames("foo", paste("first", "second", "third"))
first second third 
             "foo" 

Or another option is eval/parse, but it is better to use setNames or names<-

eval(parse(text = paste('c(', 
    dQuote(paste('first', 'second', 'third'), FALSE), '= "foo")')))
first second third 
             "foo" 

although, dplyr::lst can do this with :=, so create a named list and unlist if we want a named vector

library(dplyr)
unlist(lst(!! paste("first", "second", "third") := "foo"))
first second third 
             "foo" 

CodePudding user response:

You may achieve the desired symmetric behavior considering the "names" attribute.

`attr<-`("foo", "names", paste("first", "second", "third"))
# first second third 
#              "foo" 

`attr<-`(paste("first", "second", "third"), "names", "foo")
#                  foo 
# "first second third" 

edit

To address your comment:

The documentation ?httr::add_headers states that ".headers can be a named character vector". So, provided you have a list hnames (your "variable") you can lapply the paste over the list and name the values using `attr<-`,

httr::add_headers(.headers=`attr<-`(values, "names", lapply(hnames, paste, collapse='-')))
# <request>
# Headers:
# * Origin: http://fiddle.jshell.net
# * Accept-Encoding: gzip, deflate
# * Accept-Language: en-US,en;q=0.8
# * User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36
# * Content-Type: application/x-www-form-urlencoded; charset=UTF-8
# * Accept: */*
# * Referer: http://fiddle.jshell.net/_display/
# * X-Requested-With: XMLHttpRequest
# * Connection: keep-alive

where of course @akrun's solution with setNames might be simpler:

httr::add_headers(.headers=setNames(values, lapply(hnames, paste, collapse='-')))
# <request>
# Headers:
# * Origin: http://fiddle.jshell.net
# * Accept-Encoding: gzip, deflate
# * Accept-Language: en-US,en;q=0.8
# * User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36
# * Content-Type: application/x-www-form-urlencoded; charset=UTF-8
# * Accept: */*
# * Referer: http://fiddle.jshell.net/_display/
# * X-Requested-With: XMLHttpRequest
# * Connection: keep-alive

Data:

hnames <- list("Origin", c("Accept", "Encoding"), c("Accept", "Language"
), c("User", "Agent"), c("Content", "Type"), "Accept", "Referer", 
    c("X", "Requested", "With"), "Connection")
values <- c("http://fiddle.jshell.net", "gzip, deflate", "en-US,en;q=0.8", 
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36", 
"application/x-www-form-urlencoded; charset=UTF-8", "*/*", "http://fiddle.jshell.net/_display/", 
"XMLHttpRequest", "keep-alive")
  •  Tags:  
  • r
  • Related