File: vector2string.R

package info (click to toggle)
r-cran-htmltable 2.4.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,600 kB
  • sloc: javascript: 6,797; makefile: 2
file content (35 lines) | stat: -rw-r--r-- 1,193 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#' Collapse vector to string
#'
#' Merges all the values and outputs a string
#' formatted as '1st element', '2nd element', ...
#'
#' @param x The vector to collapse
#' @param collapse The string that separates each element
#' @param quotation_mark The type of quote to use
#' @return A string with `', '` separation
#' @importFrom stringr str_replace_all
#' @examples
#' vector2string(1:4)
#' vector2string(c("a", "b'b", "c"))
#' vector2string(c("a", "b'b", "c"), quotation_mark = '"')
#' @export
vector2string <- function(x,
                          quotation_mark = "'",
                          collapse = sprintf("%s, %s", quotation_mark, quotation_mark)) {
       paste0(
              quotation_mark,
              paste(sapply(x,
                     function(single_x) {
                            str_replace_all(
                                   single_x,
                                   quotation_mark,
                                   sprintf("\\\\%s", quotation_mark)
                            )
                     },
                     USE.NAMES = FALSE
              ),
              collapse = collapse
              ),
              quotation_mark
       )
}