使用rlang s sym在功能中并获取错误

发布于 2025-02-03 19:02:21 字数 773 浏览 3 评论 0原文

我具有以下功能:

library(tidyverse)
library(dplyr)
library(rlang)

custom_filter <- function(
  df,
  pos_filter_1 = NULL,
  ct_filter_1,
  filter_1
){
  pos_filter_1 <- paste0("df$", pos_filter_1)
  
  position_filter_1 <- if (!is.null(pos_filter_1)) (match.fun(filter_1)(!!rlang::sym(pos_filter_1), ct_filter_1)) else TRUE

  fin_df <- df %>%
    dplyr::filter(position_filter_1)
  
  return(fin_df)
}

但是,当我尝试在数据框架上使用它时,我会在!rlang :: Sym(pos_filter_1)中获得错误错误:无效参数类型。这是一个示例,我尝试在行上过滤列qb_count小于或等于5:

df <- tibble(x1 = letters[1:9],
             qb_count = 1:9,
             rb_count = 10:18)

custom_filter(df, 'qb_count', 5, "<=")

我缺少什么?如果我在功能之外进行操作,这很好。感谢任何帮助!

I have the following function:

library(tidyverse)
library(dplyr)
library(rlang)

custom_filter <- function(
  df,
  pos_filter_1 = NULL,
  ct_filter_1,
  filter_1
){
  pos_filter_1 <- paste0("df
quot;, pos_filter_1)
  
  position_filter_1 <- if (!is.null(pos_filter_1)) (match.fun(filter_1)(!!rlang::sym(pos_filter_1), ct_filter_1)) else TRUE

  fin_df <- df %>%
    dplyr::filter(position_filter_1)
  
  return(fin_df)
}

However, when I try to use it on a dataframe, I get the error Error in !rlang::sym(pos_filter_1) : invalid argument type. Here is an example in which I try to filter on rows in which the column qb_count is less than or equal to 5:

df <- tibble(x1 = letters[1:9],
             qb_count = 1:9,
             rb_count = 10:18)

custom_filter(df, 'qb_count', 5, "<=")

What am I missing? This works fine if I do it outside of the function. Appreciate any help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柒夜笙歌凉 2025-02-10 19:02:21

或更轻松的选项,我们可以创建

custom_filter <- function(
  df,
  pos_filter_1 = NULL,
  ct_filter_1,
  filter_1
){
  
 
if(is.null(pos_filter_1)) {
 df

} else {
  df %>%
    
    dplyr::filter(match.fun(filter_1)(!! rlang::ensym(pos_filter_1),
         ct_filter_1) )
  
  
  }
  }

如果/else 为-testing

>   custom_filter(df, NULL, 5, "<=")
# A tibble: 9 × 3
  x1    qb_count rb_count
  <chr>    <int>    <int>
1 a            1       10
2 b            2       11
3 c            3       12
4 d            4       13
5 e            5       14
6 f            6       15
7 g            7       16
8 h            8       17
9 i            9       18
> custom_filter(df, 'qb_count', 5, "<=")
# A tibble: 5 × 3
  x1    qb_count rb_count
  <chr>    <int>    <int>
1 a            1       10
2 b            2       11
3 c            3       12
4 d            4       13
5 e            5       14

而无需使用检查/else将代码包装在if_any/if_All (它应该与一个或多个列一起使用)并使用any_vars(如果不存在列,则不会评估)

custom_filter <- function(
  df,
  pos_filter_1 = NULL,
  ct_filter_1,
  filter_1
  )
  {



  df %>%    
    dplyr::filter(if_any(any_of(pos_filter_1), 
          ~ match.fun(filter_1)(.x,
                ct_filter_1) ))
  
  
  
  }

- 测试

>   custom_filter(df, NULL, 5, "<=")
# A tibble: 9 × 3
  x1    qb_count rb_count
  <chr>    <int>    <int>
1 a            1       10
2 b            2       11
3 c            3       12
4 d            4       13
5 e            5       14
6 f            6       15
7 g            7       16
8 h            8       17
9 i            9       18

>   custom_filter(df, 'qb_count', 5, "<=")
# A tibble: 5 × 3
  x1    qb_count rb_count
  <chr>    <int>    <int>
1 a            1       10
2 b            2       11
3 c            3       12
4 d            4       13
5 e            5       14

We may create the if/else as

custom_filter <- function(
  df,
  pos_filter_1 = NULL,
  ct_filter_1,
  filter_1
){
  
 
if(is.null(pos_filter_1)) {
 df

} else {
  df %>%
    
    dplyr::filter(match.fun(filter_1)(!! rlang::ensym(pos_filter_1),
         ct_filter_1) )
  
  
  }
  }

-testing

>   custom_filter(df, NULL, 5, "<=")
# A tibble: 9 × 3
  x1    qb_count rb_count
  <chr>    <int>    <int>
1 a            1       10
2 b            2       11
3 c            3       12
4 d            4       13
5 e            5       14
6 f            6       15
7 g            7       16
8 h            8       17
9 i            9       18
> custom_filter(df, 'qb_count', 5, "<=")
# A tibble: 5 × 3
  x1    qb_count rb_count
  <chr>    <int>    <int>
1 a            1       10
2 b            2       11
3 c            3       12
4 d            4       13
5 e            5       14

Or an easier option without having to check with if/else is wrapping the code in if_any/if_all (it should work with one or more columns) and using any_vars (if the column is not present, it wouldn't evaluate)

custom_filter <- function(
  df,
  pos_filter_1 = NULL,
  ct_filter_1,
  filter_1
  )
  {



  df %>%    
    dplyr::filter(if_any(any_of(pos_filter_1), 
          ~ match.fun(filter_1)(.x,
                ct_filter_1) ))
  
  
  
  }

-testing

>   custom_filter(df, NULL, 5, "<=")
# A tibble: 9 × 3
  x1    qb_count rb_count
  <chr>    <int>    <int>
1 a            1       10
2 b            2       11
3 c            3       12
4 d            4       13
5 e            5       14
6 f            6       15
7 g            7       16
8 h            8       17
9 i            9       18

>   custom_filter(df, 'qb_count', 5, "<=")
# A tibble: 5 × 3
  x1    qb_count rb_count
  <chr>    <int>    <int>
1 a            1       10
2 b            2       11
3 c            3       12
4 d            4       13
5 e            5       14
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文