Home > other >  Debugging a Function - Unexpected Symbols and Missing Arguments
Debugging a Function - Unexpected Symbols and Missing Arguments

Time:12-31

I have the following data:

   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")

Suppose I have the following function:

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
}

I am trying to test this function for the following case:

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)

My Question: The above function seems to have some errors relating to "unexpected symbols":

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 "}"

I tried to remove some of the ">" symbols which might be causing this error - now the function can at least be defined without any initial errors :

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
}

Problem: However, I still can not used the function to perform the above task:

factor_1_sets <- filter_list(factor_1)

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

Can someone please show me what I am doing wrong?

Thanks!

> 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 

CodePudding user response:

|> is R's brand new pipe operator since version 4.1.0.

Upgrade your R to the current version then it should work fine.

CodePudding user response:

Based on the suggestion by @ HYENA, I think I figured it out! (replace |> with %>%)

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"

Side Thoughts: Does anyone know why |> has been replaced with %>% ?

  • Related