我正在尝试使用 rlang
编写我的第一个函数,但在修复以下错误时遇到了一些麻烦。
我已经阅读了 vignette ,但没有看到我正在尝试做的事情的好例子。
library(babynames)
library(tidyverse)
name_graph <- function(data, name, sex){
name <- enquo(name)
sex <- enquo(sex)
data %>%
filter_(name == !!name, sex == !!sex) %>%
select(year, prop) %>%
ggplot()+
geom_line(mapping = aes(year, prop))
}
name_graph(babynames, Robert, M)
我期待我的分布图,但出现错误:
Called from: abort(paste_line("Quosures can only be unquoted within a quasiquotation context.", "", " # Bad:", " list(!!myquosure)", "", " # Good:", " dplyr::mutate(data, !!myquosure)"))
最佳答案
我们可以通过将 enquo
中的 quosures ( filter
) 转换为字符串来修改函数
library(rlang)
library(dplyr)
library(ggplot2)
name_graph <- function(data, name, sex){
name <- enquo(name)
sex <- enquo(sex)
data %>%
filter(name == !! as_label(name), sex == !! as_label(sex)) %>%
select(year, prop) %>%
ggplot()+
geom_line(mapping = aes(year, prop))
}
name_graph(babynames, Robert, M)
关于r - 如何修复 R 函数中的 'Quosures can only be unquoted within a quasiquotation context' 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55406546/