【问题标题】:how to create a quoted expression from strings如何从字符串创建带引号的表达式
【发布时间】:2013-02-04 23:35:20
【问题描述】:

给定一个字符串向量,我想创建一个不带引号的表达式。

# eg, I would like to go from 
c("string1", "string2")

# to...  (notice the lack of '"' marks)
quote(list(string1, string2))

我在删除引号时遇到了一些困难

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))")



如果相关,这用于 data.table 列选择。
我正在寻找的应该能够适合 data.table 如下:
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

【问题讨论】:

  • 我倾向于使用 plyr 包中的as.quoted。例如output &lt;- paste0("list(", paste(input, collapse=","), ")"); as.quoted(output)[[1]]。我还发现sprintf 很有用,例如sprintf('list(%s)', paste(input, collapse = ', '))

标签: r expression data.table quote


【解决方案1】:

这是我要做的:

## Create an example of a data.table "dt" whose columns you want to index 
## using a character vector "xx"
library(data.table)
dt <- data.table(mtcars)
xx <- c("wt", "mpg")

## Construct a call object identical to that produced by quote(list("wt", "mpg"))
jj <- as.call(lapply(c("list", xx), as.symbol))

## Try it out
dt[1:5,eval(jj)]
#       wt  mpg
# 1: 2.620 21.0
# 2: 2.875 21.0
# 3: 2.320 22.8
# 4: 3.215 21.4
# 5: 3.440 18.7

"computing on the language" 像这样时,查看您尝试构建的对象的结构通常会很有帮助。基于以下内容(一旦您了解as.call()as.symbol()),创建所需的语言对象就变得轻而易举:

x <- quote(list(wt, mpg))

str(x)
#  language list(wt, mpg)

class(x)
# [1] "call"

str(as.list(x))
# List of 3
#  $ : symbol list
#  $ : symbol wt
#  $ : symbol mpg

【讨论】:

  • 谢谢乔希!我不熟悉as.symbol。这非常有效。
  • 是的,它有时会派上用场。你也可以通过as.name() 调用它,如果这样更容易记住的话。
  • 也感谢您提供的参考资料。事实上,这正是我要去的地方。干杯
【解决方案2】:

我倾向于使用 plyr 包中的 as.quoted

 outputString <- sprintf('list(%s)', paste(input, collapse = ', ')) 


 library(plyr)
  output <- as.quoted(outputString)[[1]]

  mydt[, eval(output)]
   string1 string2
1:       A       a
2:       B       b
3:       C       c

但是,如果它只是简单的列选择,您可以传递字符串并使用..(在 Unix 终端中,这意味着“查找一级”)

mydt[ , ..input]
   string1 string2
1:       A       a
2:       B       b
3:       C       c

【讨论】:

    【解决方案3】:

    我发现这些答案对于在表达式中使用变量和多行很有帮助但不完整。要从字符串创建带引号的表达式,使用变量和多行,请使用 quote()、atop() 和 subsititute():

      # Prepare variables
      samp_skewness = round(skewness(dv),2)
      samp_kurtosis = round(kurtosis(dv),2)
      samp_var = round(var(dv))
      samp_summ <- summary(dv)
      num_samples = length(dv)
    
      # Prepare quotes containing math annotations
      q1 = quote(paste(mu,"="))
      q2 = quote(paste(sigma,"="))
      q3 = quote(paste(gamma[1],"="))
      q4 = quote(paste(gamma[2],"="))
    
    # Use subsitition to construct the expression, passing in the variables and quoted expressions
      title = substitute(atop(paste("Top Title, # samples: ", ns), 
                paste(e1,v1,", ",e2,v2,", ",e3,v3,", ",e4,v4)),
                list(ns=num_samples,v1=round(samp_summ['Mean']),v2=samp_var,
                v3=samp_skewness,v4=samp_kurtosis,e1=q1,e2=q2,e3=q3,e4=q4))
    

    在ggplot中:...

    labs(title = title) +
    

    ...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多