Given a vector of strings, I would like to create an expression without the quotation marks.
# eg, I would like to go from
c("string1", "string2")
# to... (notice the lack of '"' marks)
quote(list(string1, string2))
I am encountering some difficulty dropping the quotation marks
input <- c("string1", "string2")
output <- paste0("quote(list(", paste(input, collapse=","), "))")
# not quite what I am looking for.
as.expression(output)
expression("quote(list(string1,string2))")
This is for use in data.table column selection, in case relevant.
What I am looking for should be able to fit into data.table as follows:
library(data.table)
mydt <- data.table(id=1:3, string1=LETTERS[1:3], string2=letters[1:3])
result <- ????? # some.function.of(input)
> mydt[ , eval( result )]
string1 string2
1: A a
2: B b
3: C c
I tend to use
as.quoted
from the plyr packageHowever if it is simply column selection, you can pass the string and use
with = FALSE
Here is what I'd do:
When "computing on the language" like this, it's often helpful to have a look at the structure of the object you're trying to construct. Based on the following (and once you know about
as.call()
andas.symbol()
), creating the desired language object becomes a piece of cake:I found these answers helpful but incomplete for using variables and multiple lines within the expression. To create a quoted expression from strings, with variables and multiple lines make use of quote(), atop() and subsititute():
In ggplot: ...
...