我正在尝试将一个参数传递给一个函数,该函数是一个字符串,但必须同时评估它的名称(符号?)和它的值(参见下面的示例)。到目前为止,我可以用来base::get
获取实际值,但分配flowCore::'transform,flowSet-method'
失败。我知道关于 SO 的许多问题都是关于将字符串评估为变量名,正如您将在下面看到的那样,我尝试了很多。我认为必须有一个rlang
基于的答案,但我找不到任何解决方案,任何指针将不胜感激。
一个可重现的例子:
# load required packages -------------------------------------------------------
library(flowAI) # Bioconductor
library(flowCore) # Bioconductor
library(rlang)
# load example data ------------------------------------------------------------
data(Bcells) # from flowAI
# reprex -----------------------------------------------------------------------
timeCh <- "Time" # this could be variable
x <- flowCore::transform(Bcells,`Time`=(`Time`-min(`Time`))) # this works
y <- flowCore::transform(Bcells,`Time`=(get(timeCh)-min(get(timeCh)))) # still good
z <- flowCore::transform(Bcells,timeCh=(get(timeCh)-min(get(timeCh)))) # not good
虽然在上面的代码中,transform forz
可以正常运行,但实际上向 flowSet 添加了一个名为“timeCh”的新列。这不是想要的效果,因为我想使用转换来专门更改现有的 column Time
。因此,我一直在尝试一些策略来评估存储在 timeCh 中的字符串作为对象名称 (?) into transform
,但无济于事:
timeSym <- sym("Time")
timequo <- quo(timeCh)
t1 <- flowCore::transform(Bcells,!!timeSym=(get(timeCh)-min(get(timeCh))))
# Error: unexpected '=' in "t1 <- flowCore::transform(Bcells,!!timeSym="
t2 <- flowCore::transform(Bcells,{{timeSym}}=(get(timeCh)-min(get(timeCh))))
# Error: unexpected '=' in "t2 <- flowCore::transform(Bcells,{{timeSym}}="
t3 <- flowCore::transform(Bcells,eval(parse(text=timeCh))=(get(timeCh)-min(get(timeCh))))
# Error: unexpected '=' in "t3 <- flowCore::transform(Bcells,eval(parse(text=timeCh))="
t4 <- flowCore::transform(Bcells,assign(timeCh,(get(timeCh)-min(get(timeCh)))))
# Error in get(timeCh) : object 'Time' not found
t5 <- flowCore::transform(Bcells,assign(timeCh,(get(timeCh)-min(get(timeCh))),inherits = TRUE))
# Error in get(timeCh) : object 'Time' not found
t6 <- flowCore::transform(Bcells,with(Bcells,assign(timeCh,(get(timeCh)-min(get(timeCh))),inherits = TRUE)))
# Error in get(timeCh) : object 'Time' not found
t7 <- flowCore::transform(Bcells,as.name(timeCh)=(get(timeCh)-min(get(timeCh))))
# Error: unexpected '=' in "t7 <- flowCore::transform(Bcells,as.name(timeCh)="
t8 <- flowCore::transform(Bcells,UQ(timequo)=(get(timeCh)-min(get(timeCh))))
# Error: unexpected '=' in "t8 <- flowCore::transform(Bcells,UQ(timequo)="
t9 <- flowCore::transform(Bcells,(eval(quote(timeCh)))=(get(timeCh)-min(get(timeCh))))
# Error: unexpected '=' in "t9 <- flowCore::transform(Bcells,(eval(quote(timeCh)))="
在我看来,范围界定是一个问题,但我真的很难解决如何解决它。