comparison tostr.R @ 0:e66bb061af06 draft

planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit 3529b25417f8e1a5836474c9adec4b696d35099d-dirty
author prog
date Tue, 12 Jul 2016 12:02:37 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:e66bb061af06
1 source("tolst.R")
2
3 # Convert lists and objects to string representation. Supported outputs are:
4 # _ Text.
5 # _ PHP code.
6 # _ R code (to be done).
7
8 ##########################
9 # SET STRING TO VARIABLE #
10 ##########################
11
12 # str The value converted to a string.
13 # mode The mode to use.
14 # var Variable name.
15 .set_str_to_variable <- function(str, mode = 'txt', var = NA_character_) {
16
17 # Add variable
18 switch(mode,
19 txt = { str <- paste(var, '=', str) },
20 php = { str <- paste0('$', var, ' = ', str, ';') },
21 stop("Unknown mode '", mode, "'.")
22 )
23
24 return(str)
25 }
26
27 ################
28 # QUOTE VALUES #
29 ################
30
31 # values A vector of values.
32 # mode The mode to use.
33 # keys If the vector contains keys of a dictionary structured (depending on the mode, they will be quoted or not).
34 .quote_values <- function(values, mode = 'txt', keys = FALSE) {
35
36 if (mode == 'txt' && keys)
37 return(values)
38
39 # Quote string values
40 # TODO escape quote characters
41 if (is.character(values))
42 return(unlist(lapply(values, function(x) { paste0('"', x, '"') } )))
43
44 return(values)
45 }
46
47 ############
48 # SET KEYS #
49 ############
50
51 # values Vector or list of values.
52 # mode The mode to use.
53 .set_keys <- function(values, mode = 'txt') {
54
55 if ( ! is.null(names(values))) {
56 keys <- names(values)
57 indices <- 1:length(values)
58 switch(mode,
59 txt = { values <- lapply(indices, function(x) paste( if (nchar(keys[[x]]) == 0) x else keys[[x]], '=>', values[[x]])) },
60 php = { values <- lapply(names(values), function(x) paste0('"', if (nchar(keys[[x]]) == 0) x else keys[[x]], '"', ' => ', values[[x]])) },
61 stop("Unknown mode '", mode, "'.")
62 )
63 }
64
65 return(values)
66 }
67
68 ###############
69 # JOIN VALUES #
70 ###############
71
72 # values Vector or list of values to join.
73 # mode The mode to use.
74 .join_values <- function(values, mode = 'txt') {
75
76 switch(mode,
77 txt = { str <- paste0('(', paste(values, collapse = ', '), ')') },
78 php = { str <- paste0('[', paste(values, collapse = ', '), ']') },
79 stop("Unknown mode '", mode, "'.")
80 )
81
82 return(str)
83 }
84
85 ###############
86 # NULL TO STR #
87 ###############
88
89 # value The NULL or NA value, or the vector of length 0.
90 # mode The mode to use.
91 # var Variable name.
92 .null_to_str <- function(value, mode = 'txt', var = NA_character_) {
93
94 # Set to 'null' string
95 switch(mode,
96 txt = { str <- if (length(value) > 0 && is.na(value)) 'NA' else 'null' },
97 php = { str <- 'NULL' },
98 stop("Unknown mode '", mode, "'.")
99 )
100
101 if ( ! is.null(var) && ! is.na(var))
102 str <- .set_str_to_variable(str, mode, var)
103
104 return(str)
105 }
106
107 ################
108 # VALUE TO STR #
109 ################
110
111 # TODO hide this function ? value_to_str -> .value_to_str
112
113 # value The value to convert.
114 # mode The mode to use.
115 # var Variable name.
116 # lst If true, print the output as a list or array, even if it contains only one value.
117 .value_to_str <- function(value, mode = 'txt', var = NA_character_, lst = FALSE) {
118
119 if (is.null(value) || (length(value) == 0 && ! lst) || (length(value) > 0 && is.na(value)))
120 return(.null_to_str(value, mode = mode, var = var))
121
122 # Transform value to a string
123 value <- .quote_values(value, mode = mode)
124 str <- if (length(value) == 1 && ! lst && is.null(names(value))) as.character(value) else .join_values(.set_keys(value, mode = mode), mode = mode)
125
126 # Set to variable
127 if ( ! is.null(var) && ! is.na(var))
128 str <- .set_str_to_variable(str, mode, var)
129
130 return(str)
131 }
132
133 ###############
134 # LIST TO STR #
135 ###############
136
137 # vlist The list to convert.
138 # mode The mode to use.
139 # var Variable name.
140 # lst If true, print the output as a list or array, even if it contains only one value.
141 .list_to_str <- function(vlist, mode = 'txt', var = NA_character_, lst = FALSE) {
142
143 if (is.null(vlist) || (length(vlist) == 0 && ! lst) || (length(vlist) > 0 && is.na(vlist)))
144 return(.null_to_str(vlist, mode = mode, var = var))
145
146 #
147 vstr <- character()
148 if (length(vlist) > 0) {
149 keys <- unlist(lapply(names(vlist), function(x) if (nchar(x) == 0) x else .quote_values(x, mode = mode, keys = TRUE)))
150 values <- lapply(vlist, function(x) tostr(x, mode = mode))
151 sep <- switch(mode,
152 txt = '=>',
153 php = '=>',
154 stop("Unknown mode '", mode, "'.")
155 )
156 vstr <- unlist(lapply(1:length(vlist), function(i) if (is.null(keys) || nchar(keys[i]) == 0) values[[i]] else paste(keys[i], sep, values[[i]])))
157 }
158
159 # Join string values
160 if (length(vstr) > 1 || lst || ! is.null(keys))
161 str <- .join_values(vstr, mode = mode)
162 else
163 str <- vstr
164
165 # Set to variable
166 if ( ! is.null(var) && ! is.na(var))
167 str <- .set_str_to_variable(str, mode, var)
168
169 return(str)
170 }
171
172 ##########
173 # TO STR #
174 ##########
175
176 # obj The object to convert.
177 # mode The mode to use.
178 # var Variable name.
179 # lst If true, print the output as a list or array, even if it contains only one value.
180 tostr <- function(obj, mode = 'txt', var = NA_character_, lst = FALSE) {
181
182 switch(typeof(obj),
183 S4 = str <- tostr(tolst(obj), mode = mode, var = var, lst = lst),
184 list = str <- .list_to_str(obj, mode = mode, var = var, lst = lst),
185 str <- .value_to_str(obj, mode = mode, var = var, lst = lst)
186 )
187
188 return(str)
189 }