如何设置将在函数中使用 {dplyr} 过滤的行数?

how to set the number of lines that will be filtered with {dplyr} in a function?

在 R 函数中,如何设置将使用 {dplyr} 过滤的行数?

这是我的例子。 非常感谢!

library(dplyr)

myfunction <- function(base,
                       start_row=1,
                       end_row=Inf) {

  base %>% slice(start_row:end_row)  
}

result <- myfunction(base=iris,
                     start_row = 1,
                     end_row = 50)
# => works

result <- myfunction()
# Error in start_row:end_row : result would be too long a vector  

如评论中所述,默认使用nrow(base)

myfunction <- function(base,
                       start_row = 1,
                       end_row = nrow(base)) {
  
  base %>% 
    slice(start_row:end_row)  
}

输出

dim(myfunction(iris))
#[1] 150   5