Skip to contents

Similar to paste0(..., collapse = ""), but recursively converts its inputs to type character.

Usage

as_str(..., sep = "", rm_na = FALSE)

Arguments

...

R objects to be assembled to a single string. Dynamic dots are supported.

sep

Separator to delimit .... Defaults to none ("").

rm_na

Exclude missing values. If FALSE, missing values will be represented as "NA" in the resulting string.

Value

A character scalar.

Examples

library(magrittr)

input <-
  paste0(2:4,
         collapse = ", ") |>
  purrr::map(rep,
             times = 20) %>%
  list(c("This is a glut of ", "meaningless numbers: "), .)

# while this just converts `input` in a lazy way...
paste0(input,
       collapse = "")
#> [1] "c(\"This is a glut of \", \"meaningless numbers: \")list(c(\"2, 3, 4\", \"2, 3, 4\", \"2, 3, 4\", \"2, 3, 4\", \"2, 3, 4\", \"2, 3, 4\", \"2, 3, 4\", \"2, 3, 4\", \"2, 3, 4\", \"2, 3, 4\", \"2, 3, 4\", \"2, 3, 4\", \"2, 3, 4\", \"2, 3, 4\", \"2, 3, 4\", \"2, 3, 4\", \"2, 3, 4\", \"2, 3, 4\", \"2, 3, 4\", \"2, 3, 4\"))"

# ...this one works harder
pal::as_str(input)
#> [1] "This is a glut of meaningless numbers: 2, 3, 42, 3, 42, 3, 42, 3, 42, 3, 42, 3, 42, 3, 42, 3, 42, 3, 42, 3, 42, 3, 42, 3, 42, 3, 42, 3, 42, 3, 42, 3, 42, 3, 42, 3, 42, 3, 42, 3, 4"