在将您标记为dup之前,我了解使用字符串作为函数参数,但是我的用例略有不同。我不需要在函数内部传递参数,我想在+
(think ggplot2
)之后传递动态数量的参数。
(注意:请不要格式化和删除外观精美的####,我将它们留在了里面,以便于简单起见,人们可以将代码复制粘贴到R中)。
这是我的过程:
####因此,让我们重现此示例:
library(condformat)
condformat(iris[c(1:5,70:75, 120:125),]) +
rule_fill_discrete(Species) +
rule_fill_discrete(Petal.Width)
####我希望能够rule_fill_discrete()
动态地传递这两个函数(在我的实际用例中,我有可变数量的可能输入,并且无法对其进行硬编码)。
####首先,创建一个泛化函数:
PlotSeries <- function(x){
b=NULL
for (i in 1:length(x)){
a <- paste('rule_fill_discrete(',x[i],')',sep="")
b <- paste(paste(b,a,sep="+"))
}
b <- gsub("^\\+","",b)
eval(parse(text = b))
}
####与一个参数一起使用
condformat(iris[c(1:5,70:75, 120:125),]) +
PlotSeries("Species")
####但是如果我们传递两个参数,则不会:
condformat(iris[c(1:5,70:75, 120:125),]) +
PlotSeries(c("Species","Petal.Width"))
rule_fill_discrete(Species)+ rule_fill_discrete(Petal.Width)中的错误:二进制运算符的非数字参数
####如果我们分别打电话给每个人,它将起作用
condformat(iris[c(1:5,70:75, 120:125),]) +
PlotSeries("Species") +
PlotSeries("Petal.Width")
####这给了我们关于问题是什么的指示...当将rule_fill_discrete
语句作为一个语句传递时,它不喜欢这样的事实。让我们测试一下:
condformat(iris[c(1:5,70:75, 120:125),]) +
eval(rule_fill_discrete(Species) +
rule_fill_discrete(Petal.Width) )
rule_fill_discrete(Species)+ rule_fill_discrete(Petal.Width)中的错误:二进制运算符的非数字参数
####失败。但:
condformat(iris[c(1:5,70:75, 120:125),]) +
eval(rule_fill_discrete(Species)) +
eval(rule_fill_discrete(Petal.Width) )
####这有效。但是我们需要能够传递GROUP语句(这很重要)。因此,让我们尝试获取eval语句:
Nasty <- "eval(rule_fill_discrete(Species)) eval(rule_fill_discrete(Petal.Width))"
condformat(iris[c(1:5,70:75, 120:125),]) + Nasty #### FAIL
误差在
+.default
(condformat(虹膜[C(1:5,70:75,120:125),]),讨厌):非数字参数二进制运算符
condformat(iris[c(1:5,70:75, 120:125),]) + eval(Nasty) #### FAIL
错误
+.default
(condformat(iris [c(1:5,70:75,120:125),]),eval(Nasty)):二进制运算符的非数字参数
condformat(iris[c(1:5,70:75, 120:125),]) + parse(text=Nasty) #### FAIL
错误
+.default
(condformat(iris [c(1:5,70:75,120:125),]),parse(text = Nasty)):二进制运算符的非数字参数
condformat(iris[c(1:5,70:75, 120:125),]) + eval(parse(text=Nasty)) #### FAIL
eval(rule_fill_discrete(Species))+ eval(rule_fill_discrete(Petal.Width))中的错误:二进制运算符的非数字参数
那么我们该怎么做呢?
注意:此答案提供了针对旧版本的错误的解决方法condformat
。自此以来,该错误已得到修复,修复该错误后,请参见@zeehio对于当前版本的回答。
我认为您有两个主要独立的问题。这些都混在一起在您的帖子中。我将尝试重述并分别回答它们,然后将它们放在一起-目前尚无法完全解决,但会逐渐接近。
首先,让我们通过定义几个变量来保存一些输入:
ir = iris[c(1:5,70:75, 120:125), ]
cf = condformat(ir)
+
在向量或输入列表上使用?这是一个简单的问题。的base
回答是Reduce
。以下都是等效的:
10 + 1 + 2 + 5
"+"("+"("+"(10, 1), 2), 5)
Reduce("+", c(1, 2, 5), init = 10))
与您的情况更相关,我们可以这样做来复制您所需的输出:
fills = list(rule_fill_discrete(Species), rule_fill_discrete(Petal.Width))
res = Reduce(f = "+", x = fills, init = cf)
res
rule_fill_discrete
?这是我第一次使用condformat
,但是它看起来像是lazyeval
与rule_fill_discrete_
非标准评估的标准评估对应物以范式编写的rule_fill_discrete
。甚至在中提供了此示例?rule_fill_discrete
,但无法正常工作
cf + rule_fill_discrete_(columns = "Species")
# bad: Species column colored entirely red, not colored by species
# possibly a bug? At the very least misleading documentation...
cf + rule_fill_discrete_(columns = "Species", expression = expression(Species))
# bad: works as expected, but still uses an unquoted Species
# other failed attempts
cf + rule_fill_discrete_(columns = "Species", expression = expression("Species"))
cf + rule_fill_discrete_(columns = "Species", expression = "Species")
# bad: single color still single color column
env
SE函数中也有一个环境参数,但是我也没有运气。也许具有更多lazyeval
/表达经验的人可以指出我忽略或做错的事情。
解决方法:我们可以做的是直接传递该列。之所以有效,是因为我们没有对列做任何花哨的功能,而只是直接使用它的值来确定颜色:
cf + rule_fill_discrete_(columns = c("Species"), expression = ir[["Species"]])
# hacky, but it works
使用NSE版本Reduce
很容易:
fills = list(rule_fill_discrete(Species), rule_fill_discrete(Petal.Width))
res = Reduce(f = "+", x = fills, init = cf)
res
# works!
通过将SE与输入字符串一起使用,我们可以使用棘手的解决方法。
input = c("Species", "Petal.Width")
fills_ = lapply(input, function(x) rule_fill_discrete_(x, expression = ir[[x]]))
res_ = Reduce(f = "+", x = fills_, init = cf)
res_
# works!
当然,这可以包装到一个自定义函数中,该函数将数据框和列名的字符串向量作为输入。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句