Informatively display a maximum number of elements

plu_more(x, max = 5, type = TRUE, fn = NULL, ..., det = "more")

more(x, max = 5, type = TRUE, fn = NULL, ..., det = "more")

Arguments

x

A vector or list.

max

The maximum number of items to list. Additional arguments are replaced with "n more". Defaults to 5. If max if Inf, NULL, FALSE, or NA, all elements are preserved.

type

A logical or character.

  • If a character, type is passed to ral() and pasted after the number of elements.

  • If TRUE, the default, the first class of x is used as the type.

    • If x is a list with different classes of element, "element" is used in place of a class name.

  • If FALSE or NA, nothing is pasted after the number of elements.

fn

A function to apply to the number of additional elements. Default to NULL, which applies no function.

...

Additional arguments to fn.

det

A determiner to place before the number of additional elements. Defaults to "more".

Value

If x is a vector, a character vector with a length of max + 1 or less. If x is a list, a list with max + 1 or fewer elements.

Examples

plu::more(letters)
#> [1] "a"                  "b"                  "c"                 
#> [4] "d"                  "e"                  "21 more characters"

# Setting `max`
plu::more(letters, max = 10)
#>  [1] "a"                  "b"                  "c"                 
#>  [4] "d"                  "e"                  "f"                 
#>  [7] "g"                  "h"                  "i"                 
#> [10] "j"                  "16 more characters"
plu::more(letters, max = 27)
#>  [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
#> [20] "t" "u" "v" "w" "x" "y" "z"

# If `max` is Inf or NULL, all elements will be preserved
plu::more(letters, max = Inf)
#>  [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
#> [20] "t" "u" "v" "w" "x" "y" "z"

# If `max` is less than one, no elements will be preserved
plu::more(letters, max = 0)
#> [1] "26 characters"

# Setting element type
plu::more(letters, type = "letter")
#> [1] "a"               "b"               "c"               "d"              
#> [5] "e"               "21 more letters"

# If `type` is FALSE or NULL, no type will be included
plu::more(letters, type = FALSE)
#> [1] "a"       "b"       "c"       "d"       "e"       "21 more"

# Automatically generating type
plu::more(1:100)
#> [1] "1"                "2"                "3"                "4"               
#> [5] "5"                "95 more integers"
plu::more(as.list(1:100))
#> [[1]]
#> [1] 1
#> 
#> [[2]]
#> [1] 2
#> 
#> [[3]]
#> [1] 3
#> 
#> [[4]]
#> [1] 4
#> 
#> [[5]]
#> [1] 5
#> 
#> [[6]]
#> [1] "95 more integers"
#> 
plu::more(c(as.list(1:2), as.list(letters)))
#> [[1]]
#> [1] 1
#> 
#> [[2]]
#> [1] 2
#> 
#> [[3]]
#> [1] "a"
#> 
#> [[4]]
#> [1] "b"
#> 
#> [[5]]
#> [1] "c"
#> 
#> [[6]]
#> [1] "23 more elements"
#> 
plu::more(fracture::fracture((1:9) / (9:1)))
#> [1] "1/9"               "1/4"               "3/7"              
#> [4] "2/3"               "1/1"               "4 more characters"

# Setting a determiner other than "more"
plu::more(letters, det = "other")
#> [1] "a"                   "b"                   "c"                  
#> [4] "d"                   "e"                   "21 other characters"

# Applying a function to the number
plu::more(letters, fn = nombre::cardinal)
#> [1] "a"                          "b"                         
#> [3] "c"                          "d"                         
#> [5] "e"                          "twenty-one more characters"

# Automatic pluralization of type
fish <- c("sea bass", "crucian carp", "dace", "coelecanth")
plu::more(fish, max = 3, type = "fish")
#> [1] "sea bass"     "crucian carp" "dace"         "1 more fish" 
plu::more(fish, max = 2, type = "fish")
#> [1] "sea bass"     "crucian carp" "2 more fish" 

teeth <- c("incisor", "canine", "molar", "wisdom tooth")
plu::more(teeth, max = 3, type = "tooth")
#> [1] "incisor"      "canine"       "molar"        "1 more tooth"
plu::more(teeth, max = 2, type = "tooth")
#> [1] "incisor"      "canine"       "2 more teeth"

cacti <- c("saguaro", "prickly pear", "barrel", "star")
plu::more(cacti, max = 3, type = "cactus")
#> [1] "saguaro"       "prickly pear"  "barrel"        "1 more cactus"
plu::more(cacti, max = 2, type = "cactus")
#> [1] "saguaro"      "prickly pear" "2 more cacti"

# Using plu_more() within a function
verbose_sqrt <- function(x) {
  if (any(x < 0)) {
    problems <- x[x < 0]
    prob_msg <- crayon::silver(encodeString(problems, quote = "`"))

    warning(
      "Square root is undefined for ",
      and::and(plu::more(prob_msg, fn = crayon::silver, type = "input.")),
      call. = FALSE
    )
  }

  sqrt(x)
}

ints <- round(runif(20, -10, 10))
verbose_sqrt(ints)
#> Warning: Square root is undefined for `-2`, `-1`, `-2`, `-5`, `-2`, and 7 more inputs.
#> Warning: NaNs produced
#>  [1]      NaN      NaN      NaN      NaN      NaN      NaN      NaN      NaN
#>  [9]      NaN 3.000000 1.732051 2.828427 1.000000      NaN 2.828427      NaN
#> [17]      NaN 1.414214 1.732051 1.000000