| 1 | # common.R - Shared R functions.
 | 
| 2 | 
 | 
| 3 | # So tibble doesn't print ANSI char to text files
 | 
| 4 | # https://github.com/r-lib/crayon/issues/96
 | 
| 5 | MaybeDisableColor = function(f) {
 | 
| 6 |   if (!isatty(f)) {
 | 
| 7 |     options(crayon.enabled = F)
 | 
| 8 |   }
 | 
| 9 | }
 | 
| 10 | 
 | 
| 11 | # Disable it globally
 | 
| 12 | MaybeDisableColor(stdout())
 | 
| 13 | 
 | 
| 14 | Log = function(fmt, ...) {
 | 
| 15 |   cat(sprintf(fmt, ...))
 | 
| 16 |   cat('\n')
 | 
| 17 | }
 | 
| 18 | 
 | 
| 19 | Banner = function(fmt, ...) {
 | 
| 20 |   cat('===== '); Log(fmt, ...)
 | 
| 21 |   cat('\n')
 | 
| 22 | }
 | 
| 23 | 
 | 
| 24 | ShowFrame = function(description, df) {
 | 
| 25 |   Log(description)
 | 
| 26 |   print(df)
 | 
| 27 |   Log('')
 | 
| 28 | }
 | 
| 29 | 
 | 
| 30 | ShowValue = function(msg, ...) {
 | 
| 31 |   cat('-- '); Log(msg, ...)
 | 
| 32 |   Log('')
 | 
| 33 | }
 | 
| 34 | 
 | 
| 35 | # Same precision for all columns.
 | 
| 36 | SamePrecision = function(precision = 1) {
 | 
| 37 |   return(function(column_name) {
 | 
| 38 |     precision
 | 
| 39 |   })
 | 
| 40 | }
 | 
| 41 | 
 | 
| 42 | # Precision by column.
 | 
| 43 | ColumnPrecision = function(precision_map, default = 1) {
 | 
| 44 |   return(function(column_name) {
 | 
| 45 |     p = precision_map[[column_name]]
 | 
| 46 |     if (is.null(p)) {
 | 
| 47 |       default
 | 
| 48 |     } else {
 | 
| 49 |       p
 | 
| 50 |     }
 | 
| 51 |   })
 | 
| 52 | }
 | 
| 53 | 
 | 
| 54 | # Write a CSV file along with a schema.
 | 
| 55 | #
 | 
| 56 | # precision: list(column name -> integer precision)
 | 
| 57 | writeCsv = function(table, prefix, precision_func = NULL, tsv = F) {
 | 
| 58 |   if (tsv) {
 | 
| 59 |     data_out_path = paste0(prefix, '.tsv')
 | 
| 60 |     write.table(table, data_out_path, row.names = F, sep = '\t', quote = F)
 | 
| 61 |   } else {
 | 
| 62 |     data_out_path = paste0(prefix, '.csv')
 | 
| 63 |     write.csv(table, data_out_path, row.names = F)
 | 
| 64 |   }
 | 
| 65 | 
 | 
| 66 |   getFieldType = function(field_name) { typeof(table[[field_name]]) }
 | 
| 67 |   types_list = lapply(names(table), getFieldType)
 | 
| 68 | 
 | 
| 69 |   if (is.null(precision_func)) {
 | 
| 70 |     precision_func = function(column_name) { 1 }
 | 
| 71 |   }
 | 
| 72 |   precision_list = lapply(names(table), precision_func)
 | 
| 73 | 
 | 
| 74 |   #print(precision_list)
 | 
| 75 | 
 | 
| 76 |   schema = tibble(
 | 
| 77 |     column_name = names(table),
 | 
| 78 |     type = as.character(types_list),
 | 
| 79 |     precision = as.character(precision_list)
 | 
| 80 |   )
 | 
| 81 |   if (tsv) {
 | 
| 82 |     schema_out_path = paste0(prefix, '.schema.tsv')
 | 
| 83 |     write.table(schema, schema_out_path, row.names = F, sep = '\t', quote = F)
 | 
| 84 |   } else {
 | 
| 85 |     schema_out_path = paste0(prefix, '.schema.csv')
 | 
| 86 |     write.csv(schema, schema_out_path, row.names = F)
 | 
| 87 |   }
 | 
| 88 | }
 | 
| 89 | 
 | 
| 90 | readTsv = function(path) {
 | 
| 91 |   # quote = '' means disable quoting
 | 
| 92 |   read.table(path, header = T, sep = '\t', quote = '')
 | 
| 93 | }
 | 
| 94 | 
 | 
| 95 | writeTsv = function(table, prefix, precision_func = NULL) {
 | 
| 96 |   writeCsv(table, prefix, precision_func = precision_func, tsv = T)
 | 
| 97 | }
 | 
| 98 | 
 |