【问题标题】:Debugging a Function - Unexpected Symbols and Missing Arguments调试函数 - 意外符号和缺少参数
【发布时间】:2021-12-31 06:39:23
【问题描述】:

我有以下数据:

   factor_1 <- c("0","B", "C")
    factor_2 <- c("0","BB", "CC")
    factor_3 <- c("0","BBB", "CCC", "DDD")
    factor_4 <- c("0","BBBB", "CCCC", "DDDD", "EEEE")
    factor_5 <- c("0","BBBBB", "CCCCC", "DDDDD", "EEEEE", "FFFFFF")

假设我有以下功能:

filter_list <- function(x) {
  flist <- list()
  for (i in seq_along(x)) {
    flist <- c(flist,
      combn(x, i) |>     # matrix with cols = combinations of i elements
        data.frame() |>  # temporarily turn it into a data frame
        as.list()        # make it a list and append it to flist
    )
  }
  names(flist) <- NULL   # strip off the rather annoying item names
  flist
}

我正在尝试针对以下情况测试此功能:

factor_1_sets <- filter_list(factor_1)
factor_2_sets <- filter_list(factor_2)
factor_3_sets <- filter_list(factor_3)
factor_4_sets <- filter_list(factor_4)
factor_5_sets <- filter_list(factor_5)
# For convenience, we make a list of all the lists.
factor_sets <- list(factor_1_sets, factor_2_sets, factor_3_sets, factor_4_sets, factor_5_sets)

我的问题:上述函数似乎有一些与“意外符号”有关的错误:

filter_list <- function(x) {
+     flist <- list()
+     for (i in seq_along(x)) {
+         flist <- c(flist,
+                    combn(x, i) |>     # matrix with cols = combinations of i elements
Error: unexpected '>' in:
"        flist <- c(flist,
                   combn(x, i) |>"
>                        data.frame() |>  # temporarily turn it into a data frame
Error: unexpected '>' in "                       data.frame() |>"
>                        as.list()        # make it a list and append it to flist
Error in typeof(x) : argument "x" is missing, with no default
>         )
Error: unexpected ')' in "        )"
>     }
Error: unexpected '}' in "    }"
>     names(flist) <- NULL   # strip off the rather annoying item names
Error in names(flist) <- NULL : object 'flist' not found
>     flist
Error: object 'flist' not found
> }
Error: unexpected '}' in "}"

我试图删除一些可能导致此错误的 ">" 符号 - 现在该函数至少可以在没有任何初始错误的情况下定义:

filter_list <- function(x) {
    flist <- list()
    for (i in seq_along(x)) {
        flist <- c(flist,
                   combn(x, i) |    # matrix with cols = combinations of i elements
                       data.frame() |  # temporarily turn it into a data frame
                       as.list()        # make it a list and append it to flist
        )
    }
    names(flist) <- NULL   # strip off the rather annoying item names
    flist
}

问题:但是,我仍然无法使用该功能来执行上述任务:

factor_1_sets <- filter_list(factor_1)

 Error in typeof(x) : argument "x" is missing, with no default 

谁能告诉我我做错了什么?

谢谢!

> sessionInfo()
R version 4.0.3 (2020-10-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19043)

Matrix products: default

locale:
[1] LC_COLLATE=English_Canada.1252  LC_CTYPE=English_Canada.1252    LC_MONETARY=English_Canada.1252 LC_NUMERIC=C                    LC_TIME=English_Canada.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] GA_3.2.1         iterators_1.0.13 foreach_1.5.1    dplyr_1.0.6     

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.7       rstudioapi_0.13  magrittr_2.0.1   tidyselect_1.1.0 R6_2.5.0         rlang_0.4.10     fansi_0.4.2      tools_4.0.3      xfun_0.21        tinytex_0.30     utf8_1.1.4      
[12] cli_2.5.0        DBI_1.1.1        ellipsis_0.3.2   assertthat_0.2.1 tibble_3.1.2     lifecycle_1.0.0  crayon_1.3.4     purrr_0.3.4      vctrs_0.3.8      codetools_0.2-16 glue_1.4.2      
[23] compiler_4.0.3   pillar_1.6.1     generics_0.1.0   pkgconfig_2.0.3 

【问题讨论】:

    标签: r function


    【解决方案1】:

    |&gt; 是自 version 4.1.0 以来 R 的全新管道运算符。

    将您的 R 升级到当前版本,然后它应该可以正常工作。

    【讨论】:

    • @HYENA:非常感谢您的回答!同时,有没有办法改变这个新的管道操作符,使上述函数在旧版本的 R 上运行?非常感谢!
    • 只需像as.list(data.frame(combn(x,i)))一样将所有内容包装在一个函数调用中。
    【解决方案2】:

    根据@HYENA 的建议,我想我想通了! (将 |> 替换为 %>%

    filter_list <- function(x) {
      flist <- list()
      for (i in seq_along(x)) {
        flist <- c(flist,
          combn(x, i) %>%    # matrix with cols = combinations of i elements
            data.frame() %>%   # temporarily turn it into a data frame
            as.list()        # make it a list and append it to flist
        )
      }
      names(flist) <- NULL   # strip off the rather annoying item names
      flist
    }
    
    
    #test
    
     filter_list(factor_1)
    [[1]]
    [1] "AAAA"
    
    [[2]]
    [1] "BBBB"
    
    [[3]]
    [1] "CCCC"
    
    #etc
    
    [[11]]
    [1] "BBBB" "DDDD"
    
    [[12]]
    [1] "BBBB" "EEEE"
    
    #etc
    
    [[27]]
    [1] "AAAA" "BBBB" "CCCC" "EEEE"
    
    [[28]]
    [1] "AAAA" "BBBB" "DDDD" "EEEE"
    
    [[29]]
    [1] "AAAA" "CCCC" "DDDD" "EEEE"
    
    [[30]]
    [1] "BBBB" "CCCC" "DDDD" "EEEE"
    
    [[31]]
    [1] "AAAA" "BBBB" "CCCC" "DDDD" "EEEE"
    

    旁白:有谁知道为什么 |> 被替换为 %>% ?

    【讨论】:

    • 也许 %>% 不是基本的 R 东西。见cran.r-project.org/doc/manuals/r-release/…
    • |&gt;没有%&gt;% 替换。前者是新的base R pipe operator,后者是贡献包magrittr的pipe operator。
    • 感谢大家的回复!只是为了澄清-有人可以告诉我是否已将原始功能正确转换为正确的格式吗?或者我应该以不同的方式完成它?谢谢!
    • 您的解决方案在我看来不错。
    • 还有一件事。我只会在以前的 R 版本中使用普通函数调用,因为在我的 R 环境中,许多包都定义了 %>% 运算符,包括 jqr、purrr、tidyr 等。如果没有明确指定,将使用哪个版本,以及使用的版本是否是我想要的。
    猜你喜欢
    • 2017-04-15
    • 1970-01-01
    • 2011-06-09
    • 2016-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多