99# 5. 生成 .json (data.json, meta.json, ui.json)
1010# 6. 基于配置和输入文件生成 plot.R
1111#
12- # Test: ./hisub.R test_ezcox .R ezcox
13- # Test: ./hisub.R test_pcatools.R pcatools
12+ # ./hisub.R examples/helloworld .R test_hello
13+ VERSION = 0.1
1414
15- suppressMessages(library(readr ))
16- suppressMessages(library(dplyr ))
17- suppressMessages(library(purrr ))
18- suppressMessages(library(jsonlite ))
19- suppressMessages(library(styler ))
15+ message(" HiSub version " , VERSION )
16+ message(" Copyright (c) 2021 Hiplot (https://hiplot.com.cn/)" )
17+ message(" ========================" )
2018
19+ message(" Checking dependencies..." )
20+ if (! require(" pacman" )) install.packages(" pacman" )
21+ suppressMessages(pacman :: p_load(readr , dplyr , purrr , jsonlite , styler ))
22+ message(" Done" )
23+
24+ message(" Checking input..." )
2125Args <- commandArgs(trailingOnly = TRUE )
2226# Args <- c("test.R", "test-plugin2")
2327
@@ -34,10 +38,14 @@ if (length(Args) > 2) {
3438
3539if (! dir.exists(outdir )) dir.create(outdir , recursive = TRUE )
3640if (flag ) {
41+ message(" Copying middle files..." )
3742 file.copy(Args [2 : (length(Args ) - 1 )], outdir )
3843}
44+ message(" Done" )
45+
3946# Preprocessing -----------------------------------------------------------
4047
48+ message(" Preprocessing R script " , Args [1 ])
4149# 过滤无关行
4250file_content <- file_content [startsWith(file_content , " #" )]
4351file_content <- file_content [
@@ -50,8 +58,12 @@ splitAt <- function(x, pos) unname(split(x, cumsum(seq_along(x) %in% pos)))
5058
5159tag_list <- splitAt(file_content , grep(" # *@" , file_content ))
5260
61+ message(" Done" )
62+
5363# Parsing content ---------------------------------------------------------
5464
65+ message(" Parsing Hiplot tags..." )
66+
5567# 针对每一个元素解析标签和内容
5668tag_name <- map_chr(tag_list , ~ sub(" # *@([^ ]+).*" , " \\ 1" , . [1 ]))
5769
@@ -105,8 +117,6 @@ parse_tag_citation <- function(x) {
105117 x [- 1 ] <- sub(" ^# *" , " " , x [- 1 ])
106118 }
107119 x <- paste(x [x != " " ], collapse = " \n " )
108- message(" \n Citation info parsed." )
109- cat(x )
110120 list (type = " citation" , value = x )
111121}
112122
@@ -155,11 +165,6 @@ parse_tag_description <- function(x) {
155165 x_en <- doc_list $ en
156166 x_zh <- doc_list $ zh
157167
158- message(" \n Description info parsed." )
159- message(" en:" )
160- cat(x_en )
161- message(" \n zh:" )
162- cat(x_zh )
163168 list (type = " description" , value = list (
164169 en = x_en ,
165170 zh = x_zh
@@ -176,15 +181,13 @@ parse_tag_library <- function(x) {
176181 }
177182 x <- paste(x , collapse = " " )
178183 x <- unlist(strsplit(x , split = " " ))
179- message(" \n Required packages parsed." )
180- cat(x )
184+
181185 list (type = " library" , value = x )
182186}
183187
184188parse_tag_param <- function (x ) {
185189 param_name <- parse_tag_value(x [1 ])
186190 if (! grepl(" export::" , x [1 ])) {
187- message(" \n No export detected." )
188191 return (NULL ) # No returns
189192 }
190193
@@ -236,8 +239,6 @@ parse_tag_data <- function(x) {
236239 x <- sub(" ^# *" , " " , x )
237240 x <- x [! grepl(" ^@|#" , x )]
238241 x <- paste(x , collapse = " \n " )
239- message(" \n Code to generate data parsed." )
240- cat(x )
241242 list (type = " data" , value = x )
242243}
243244
@@ -274,11 +275,12 @@ a <- compact(a)
274275# Generate data files -----------------------------------------------------
275276
276277if (" data" %in% names(a )) {
277- message(" Generating data file..." )
278+ message(" Generating example data file..." )
278279 old_wd <- getwd()
279280 setwd(outdir )
280281 eval(parse(text = a $ data $ value ))
281282 setwd(old_wd )
283+ message(" Done" )
282284}
283285
284286# Generate plugin files ---------------------------------------------------
@@ -454,7 +456,7 @@ json_meta <- list(
454456 )
455457)
456458
457- message(" meta.json" )
459+ message(" - meta.json" )
458460# jsonlite::toJSON(json_meta, auto_unbox = TRUE, pretty = TRUE)
459461write_json(json_meta , file.path(outdir , " meta.json" ), auto_unbox = TRUE , pretty = TRUE )
460462
@@ -508,7 +510,7 @@ if (length(unlist(a$params$example_data)) > 0) {
508510}
509511if (length(a $ params $ example_textarea ) > 0 ) json_data $ exampleData $ textarea <- a $ params $ example_textarea
510512
511- message(" data.json" )
513+ message(" - data.json" )
512514# jsonlite::toJSON(json_data, auto_unbox = TRUE, pretty = TRUE)
513515write_json(json_data , file.path(outdir , " data.json" ),
514516 null = " list" , auto_unbox = TRUE , pretty = TRUE
@@ -525,15 +527,15 @@ json_ui <- list(
525527 extra = a $ params $ ui_extra
526528)
527529
528- message(" ui.json" )
530+ message(" - ui.json" )
529531# json_ui <- jsonlite::toJSON(json_ui, auto_unbox = TRUE, pretty = TRUE)
530532write_json(json_ui , file.path(outdir , " ui.json" ),
531533 null = " list" , auto_unbox = TRUE , pretty = TRUE
532534)
533535
534536# plot.R
535537# 保留输入脚本
536- message(" plot.R" )
538+ message(" - plot.R" )
537539write_lines(fc , file.path(outdir , " plot.R" ))
538540# 生成 plot.R 进行调用
539541args_pairs <- map(
@@ -630,4 +632,4 @@ write_lines(plot_r, file.path(outdir, "plot.R"), append = TRUE)
630632
631633style_file(file.path(outdir , " plot.R" ))
632634
633- # output file
635+ message( " ALL operations done " )
0 commit comments