r - 如何正确评估 R 波浪号表达式中的变量
问题描述
我有一个函数 ( col_grob
),它调用另一个函数 ( pal_bar
),并使用波浪符号表达式,如下所示:
## plots a colour bar with specified colour intervals
pal_bar <- function(cols) {
cols <- colorRampPalette(cols)(200)
par(mar = c(0, 0, 0, 0))
plot(1:200, rep(1, 200), col = cols, pch = 15, cex = 1.1, bty = 'n', xaxt = 'n', xlab = '', yaxt = 'n', ylab = '', main="")
}
## calls pal_bar function to plot the bar as a grob, tilde expression
col_grob <- function(pal) {
g <- ggplotify::as.grob(~pal_bar(pal))
grid::grid.draw(g)
}
运行时返回错误“找不到对象'pal'”:
col_grob(pal = c("red", "blue"))
我遇到了资源和类似的问题,但由于对评估规则缺乏了解,我无法解决问题。我尝试了~pal_bar(I(pal))
, bquote()
function ,并且可能structure(list(), *)
但没有足够的知识来正确格式化语法。
解决方案
一个可能的解决方案:
col_grob <- function(pal) {
txt <- substitute(pal_bar(pal))
g <- ggplotify::as.grob(as.expression(txt))
grid::grid.draw(g)
}
col_grob(pal = c("red", "blue"))
推荐阅读
- javascript - 如何在 Vue 中选择所有具有特定关键字的 $refs?
- typescript - 如何在 addEventListener 上键入 currentTarget
- python - 从 csv 更新 BigQuery 表
- python - Tensorflow 2.0 list_physical_devices 没有检测到我的 GPU
- c# - 在列表值之间分配整数值
- flowground - flowground HTTP 回复连接器“SyntaxError:JSON.parse:JSON 数据的第 1 行第 1 列的意外字符”
- php - 通过 PHP 运行 Neo4j 查询 REST API
- azure - 如何使用 Azure 数据工厂管道调用 REST API?
- android - 需要一些关于 Android SELinux 构建错误的指南
- c++ - 为什么我的 DLL 只能通过手动映射注入?