-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprint.R
More file actions
32 lines (25 loc) · 895 Bytes
/
print.R
File metadata and controls
32 lines (25 loc) · 895 Bytes
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
library(magrittr)
library(stringr)
txt <- readLines("demo.R")
txt <- txt[!str_detect(txt, '^cat')]
txt <- txt[!str_detect(txt, '^invisible')]
txt <- txt[!str_detect(txt, '^fname')]
txt <- txt[!str_detect(txt, '^pdf')]
txt <- txt[!str_detect(txt, '^system')]
txt <- txt[!str_detect(txt, '^suppress')]
txt <- txt[!str_detect(txt, '^\\{')]
txt <- txt[!str_detect(txt, '^\\}\\)')]
txt <- txt[!str_detect(txt, '^ +"\\\\n')] # Remove trace statements.
model <- basename(getwd())
msg <- sprintf("#\n# This script generated from mlhub.ai:/pool/main/%s/%s/",
str_sub(model, 1, 1), model)
ins <- str_detect(txt, '# Copyright') %>% which()
if (length(ins) == 1) txt %<>% append(msg, after=ins)
# Remove duplicate empty lines.
dup <- c()
for (l in 2:length(txt))
{
if (txt[l] == "" && txt[l-1] == "") dup <- c(dup, l)
}
txt <- txt[-dup]
paste(txt, collapse="\n") %>% cat("\n")