comparison strhlp.R @ 6:f86fec07f392 draft default tip

planemo upload commit c397cd8a93953798d733fd62653f7098caac30ce
author prog
date Fri, 22 Feb 2019 16:04:22 -0500
parents fb9c0409d85c
children
comparison
equal deleted inserted replaced
5:fb9c0409d85c 6:f86fec07f392
1 if ( ! exists('trim')) { # Do not load again if already loaded
2
3 #######################
4 # WHITESPACE TRIMMING #
5 #######################
6
7 # Trim leading whitespaces
8 trim.leading <- function (x) sub("^\\s+", "", x)
9
10 # Trim trailing whitespaces
11 trim.trailing <- function (x) sub("\\s+$", "", x)
12
13 # Trim leading and trailing whitespaces
14 trim <- function (x) gsub("^\\s+|\\s+$", "", x)
15
16 #############
17 # SPLITTING #
18 #############
19
20 # s The string to split.
21 # sep The separator on which to split.
22 # trim Trim whitespaces for the resulting elements.
23 # unlist Unlist the result, So that for a single string (i.e.: s has length 1), it returns a vector of strings instead of a list of vectors of strings.
24 # RETURN A list of strings.
25 split.str <- function(s, sep = ',', trim = TRUE, unlist = FALSE) {
26 v <- strsplit(s, sep)
27 if (trim) v <- lapply(v, trim)
28 if (unlist) v <- unlist(v)
29 return(v)
30 }
31
32 ########################
33 # SPLIT KEY/VALUE LIST #
34 ########################
35
36 split.kv.list <- function(s, sep = ',', kvsep = '=') {
37
38 # Split
39 kvs <- strsplit(strsplit(s, sep)[[1]], kvsep)
40
41 # Get keys
42 k <- vapply(kvs, function(x) x[[1]], FUN.VALUE = '')
43 v <- vapply(kvs, function(x) x[[2]], FUN.VALUE = '')
44
45 # Set names
46 names(v) <- k
47
48 return(v)
49 }
50
51 #########################
52 # CONCAT KEY/VALUE LIST #
53 #########################
54
55 concat.kv.list <- function(x, sep = ',', kvsep = '=') {
56
57 k <- names(x)
58
59 s = paste(paste(names(x), x, sep = kvsep), collapse = sep)
60
61 return(s)
62 }
63
64 #################
65 # REMOVE QUOTES #
66 #################
67
68 remove.quotes <- function(s) {
69 return(sub('^["\']?([^\'"]*)["\']?$', '\\1', s, perl = TRUE))
70 }
71
72 } # end of load safe guard