如何修复R函数中的“Quosures can only be unquoted within a quasiquotation context"错误 [英] How to fix 'Quosures can only be unquoted within a quasiquotation context' error in R function

查看:24
本文介绍了如何修复R函数中的“Quosures can only be unquoted within a quasiquotation context"错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 rlang 编写我的第一个函数,但在修复以下错误时遇到了一些麻烦.

我已经阅读了

I am trying to write my first function using rlang and I am having some trouble fixing the following error.

I've read the vignette, but didn't see a good example of what I'm trying to do.

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)

I'm expecting my distribution graph, but getting an error:

Called from: abort(paste_line("Quosures can only be unquoted within a quasiquotation context.", "", " # Bad:", " list(!!myquosure)", "", " # Good:", " dplyr::mutate(data, !!myquosure)"))

解决方案

We can modify the function by converting the quosures (enquo) to string in the 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函数中的“Quosures can only be unquoted within a quasiquotation context"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆