Mercurial > repos > prog > lcmsmatching
comparison htmlhlp.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('HtmlWriter')) { # Do not load again if already loaded | |
| 2 | |
| 3 library(methods) | |
| 4 | |
| 5 ##################### | |
| 6 # CLASS DECLARATION # | |
| 7 ##################### | |
| 8 | |
| 9 HtmlWriter <- setRefClass("HtmlWriter", fields = list(.file = "character", .auto.indent = "numeric")) | |
| 10 | |
| 11 | |
| 12 ############### | |
| 13 # CONSTRUCTOR # | |
| 14 ############### | |
| 15 | |
| 16 HtmlWriter$methods( initialize = function(file = NA_character_, auto.indent = TRUE, ...) { | |
| 17 | |
| 18 .file <<- file | |
| 19 .auto.indent <<- if (auto.indent) 0 else NA_integer_ | |
| 20 | |
| 21 # Create empty file | |
| 22 cat('', file = .self$.file, append = FALSE) | |
| 23 | |
| 24 callSuper(...) # calls super-class initializer with remaining parameters | |
| 25 }) | |
| 26 | |
| 27 ######### | |
| 28 # WRITE # | |
| 29 ######### | |
| 30 | |
| 31 HtmlWriter$methods( write = function(text, indent = NA_integer_, newline = TRUE, escape = FALSE) { | |
| 32 | |
| 33 # Compute indentation | |
| 34 if (is.na(indent)) | |
| 35 indent <- if (is.na(.self$.auto.indent)) 0 else .self$.auto.indent | |
| 36 | |
| 37 cat(rep("\t", indent), text, if (newline) "\n" else "", sep = '', file = .self$.file, append = TRUE) | |
| 38 }) | |
| 39 | |
| 40 ############# | |
| 41 # WRITE TAG # | |
| 42 ############# | |
| 43 | |
| 44 HtmlWriter$methods( writeTag = function(tag, attr = NA_character_, text = NA_character_, indent = NA_integer_, newline = TRUE) { | |
| 45 | |
| 46 if (is.na(text)) { | |
| 47 attributes <- if (is.na(attr)) '' else paste0(' ', paste(vapply(names(attr), function(a) paste0(a, '="', attr[[a]], '"'), FUN.VALUE=''), collapse = ' ')) | |
| 48 .self$write(paste0("<", tag, attributes, "/>"), indent = indent, newline = newline, escape = FALSE) | |
| 49 } | |
| 50 else { | |
| 51 .self$writeBegTag(tag, attr = attr, indent = indent, newline = FALSE) | |
| 52 .self$write(text, escape = TRUE , indent = 0, newline = FALSE) | |
| 53 .self$writeEndTag(tag, indent = 0, newline = newline) | |
| 54 } | |
| 55 }) | |
| 56 | |
| 57 ################### | |
| 58 # WRITE BEGIN TAG # | |
| 59 ################### | |
| 60 | |
| 61 HtmlWriter$methods( writeBegTag = function(tag, attr = NA_character_, indent = NA_integer_, newline = TRUE) { | |
| 62 | |
| 63 # Write opening tag | |
| 64 attributes <- if (is.na(attr)) '' else paste0(' ', paste(vapply(names(attr), function(a) paste0(a, '="', attr[[a]], '"'), FUN.VALUE=''), collapse = ' ')) | |
| 65 .self$write(paste0("<", tag, attributes, ">"), indent = indent, newline = newline, escape = FALSE) | |
| 66 | |
| 67 # Increment auto-indent | |
| 68 if ( ! is.na(.self$.auto.indent)) | |
| 69 .auto.indent <<- .self$.auto.indent + 1 | |
| 70 }) | |
| 71 | |
| 72 ################# | |
| 73 # WRITE END TAG # | |
| 74 ################# | |
| 75 | |
| 76 HtmlWriter$methods( writeEndTag = function(tag, indent = NA_integer_, newline = TRUE) { | |
| 77 | |
| 78 # Decrement auto-indent | |
| 79 if ( ! is.na(.self$.auto.indent)) | |
| 80 .auto.indent <<- .self$.auto.indent - 1 | |
| 81 | |
| 82 # Write closing tag | |
| 83 .self$write(paste0("</", tag, ">"), indent = indent, newline = newline, escape = FALSE) | |
| 84 }) | |
| 85 | |
| 86 ############### | |
| 87 # WRITE TABLE # | |
| 88 ############### | |
| 89 | |
| 90 HtmlWriter$methods( writeTable = function(x, indent = NA_integer_, newline = TRUE) { | |
| 91 | |
| 92 .self$writeBegTag('table', indent = indent, newline = newline) | |
| 93 | |
| 94 # Write table header | |
| 95 if ( ! is.null(colnames(x))) { | |
| 96 .self$writeBegTag('tr', indent = indent + 1, newline = newline) | |
| 97 for (field in colnames(x)) | |
| 98 .self$writeTag('th', text = field, indent = indent + 2, newline = newline) | |
| 99 .self$writeEndTag('tr', indent = indent + 1, newline = newline) | |
| 100 } | |
| 101 | |
| 102 # Write values | |
| 103 if (nrow(x) > 0 && ncol(x) > 0) | |
| 104 for (i in 1:nrow(x)) { | |
| 105 .self$writeBegTag('tr', indent = indent + 1, newline = newline) | |
| 106 for (j in 1:ncol(x)) | |
| 107 .self$writeTag('td', text = x[i, j], indent = indent + 2, newline = newline) | |
| 108 .self$writeEndTag('tr', indent = indent + 1, newline = newline) | |
| 109 } | |
| 110 .self$writeEndTag('table', indent = indent, newline = newline) | |
| 111 }) | |
| 112 | |
| 113 | |
| 114 } # end of load safe guard |
