r - 在编写 R 包时,使用 flowCore::transform 函数,我可以使用变量名作为文本并获取实际值吗?

标签 r bioconductor scoping rlang quosure

我试图将一个参数传递给一个函数,它是一个字符串,但必须同时评估它的名称(符号?)和它的值(见下面的例子)。到目前为止,我可以使用 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

而在上面的代码中, z 的转换会运行得很好,实际上一个新列被添加到名为“timeCh”的 flowSet 中。这不是想要的效果,因为我想使用转换来专门更改现有列 Time .因此,我一直在尝试一些策略来将存储在 timeCh 中的字符串作为对象名称 (?) 评估为 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)))="

在我看来,范围界定是一个问题,但我真的很难解决它。

最佳答案

使用 rlang 表达式算法的一种可能方法:

# Compose the expression `Time = Time - min(Time)`
tfarg <- rlang::exprs( !!timeSym := !!timeSym - min(!!timeSym) )

# Compose the expression `flowCore::transform(Bcells, Time = Time - min(Time))`
xpr  <- rlang::expr( flowCore::transform(Bcells, !!!tfarg) )
xpr2 <- rlang::call2( flowCore::transform, Bcells, !!!tfarg )   # Alternative

# Evaluate the second expression
t1 <- eval(xpr)      # or t1 <- eval(xpr2)

# Compare to desired output
identical( summary(t1), summary(x) )  # TRUE

在上面,我们首先使用 quasiquotation 创建表达式 Time = Time - min(Time) 。将其视为用存储在 !!timeSym 中的符号(即 timeSym )替换 Time ,同时使用 := 使其在分配的左侧工作。

然后,我们通过将第一个表达式粘贴到 flowCore::transform(Bcells, Time = Time - min(Time)) 函数调用中来创建第二个表达式 flowCore::transform()。最后,我们对其进行评估并将结果与​​您想要的输出进行比较。

旁注: 似乎最小时间值为零,因此您的 transform() 实际上什么也没做。

关于r - 在编写 R 包时,使用 flowCore::transform 函数,我可以使用变量名作为文本并获取实际值吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60668763/

相关文章:

css - 更改 textInput Shiny 小部件的占位符颜色

r - 无法在 Linux CentOS 7 上安装 R oligo 和 RCurl 包

r - R 中多个区间内的平均信号

c++ - 按值(value)界定和传递类(class)?

r - 调整ggplot2中堆叠geom_bar的y轴原点

r - R Markdown中的图形大小

r - Shiny (Rstudio) 应用程序不起作用

r - 为什么 "$"自动补全适用于 BioConductor 的 S4 类 "SummarizedExperiment"

javascript - 嵌套函数可以访问它们的范围 "above"...但是它们在哪里定义有关系吗?

r - 将函数中的变量存储在全局环境中