Skip to content
This repository has been archived by the owner on Apr 10, 2022. It is now read-only.

Commit

Permalink
Add template command, close #4
Browse files Browse the repository at this point in the history
  • Loading branch information
ShixiangWang committed Feb 8, 2021
1 parent 3c86b31 commit 0653261
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 61 deletions.
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,23 @@ git clone https://github.com/hiplot/hiplot-plugin-generator
cd hiplot-plugin-generator
```

Then you can prepare a R script descibe below.
Then you can prepare a R script named `source.R` with:

Let's get started with the Hello World plugin for Hiplot, which shows the core concept of **Hisub** and most common setting.
```sh
./hisub.R template
```

Next modify the `source.R` as your wish and convert it into your Hiplot plugin.

```sh
./hisub.R source.R <output-directory>
```


### Hello World

For preparing the core R script, let's get started with the Hello World plugin for Hiplot, which shows the core concept of **Hisub** and most common setting.

The contents of `helloworld.R`:

```R
Expand Down
9 changes: 4 additions & 5 deletions examples/ezcox.R
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,17 @@ call_ezcox <- function(data,
vars_to_show,
drop_controls,
add_caption) {

if (ncol(data) < 3) {
stop("Input data should have at least 3 columns!")
}

if (!all(c("time", "status") %in% colnames(data))) {
cat("WARN: 'time' and 'status' colnames not exist in input data.",
sep = "\n")
sep = "\n"
)
cat("WARN: rename the first and the second column as 'time' and 'status'.",
sep = "\n")
sep = "\n"
)
colnames(data)[1:2] <- c("time", "status")
}

Expand Down Expand Up @@ -119,5 +120,3 @@ call_ezcox <- function(data,
add_caption = add_caption
)
}


121 changes: 68 additions & 53 deletions examples/pcatools.R
Original file line number Diff line number Diff line change
Expand Up @@ -81,71 +81,82 @@ call_pcatools <- function(datTable, sampleInfo,
plotloadingsHighCol,
eigencorplotMetavars,
eigencorplotComponents) {
row.names(datTable) <- datTable[,1]
datTable <- datTable[,-1]
row.names(sampleInfo) <- sampleInfo[,1]
sampleInfo <- sampleInfo[,-1]
row.names(datTable) <- datTable[, 1]
datTable <- datTable[, -1]
row.names(sampleInfo) <- sampleInfo[, 1]
sampleInfo <- sampleInfo[, -1]
data3 <- pca(datTable, metadata = sampleInfo, removeVar = (100 - top_var) / 100)

p1 <- PCAtools::screeplot(
data3, components = getComponents(data3, 1:screeplotComponents),
data3,
components = getComponents(data3, 1:screeplotComponents),
axisLabSize = 14, titleLabSize = 20,
colBar = screeplotColBar,
gridlines.major = FALSE, gridlines.minor = FALSE,
returnPlot = TRUE)
returnPlot = TRUE
)

p2 <- PCAtools::pairsplot(
data3, components = getComponents(data3, c(1:pairsplotComponents)),
data3,
components = getComponents(data3, c(1:pairsplotComponents)),
triangle = TRUE, trianglelabSize = 12,
hline = 0, vline = 0,
pointSize = 0.8, gridlines.major = FALSE, gridlines.minor = FALSE,
colby = 'Grade',
title = '', plotaxes = FALSE,
margingaps = unit(c(0.01, 0.01, 0.01, 0.01), 'cm'),
colby = "Grade",
title = "", plotaxes = FALSE,
margingaps = unit(c(0.01, 0.01, 0.01, 0.01), "cm"),
returnPlot = TRUE,
colkey = get_hiplot_color(conf$general$palette, -1,
conf$general$palette_custom)) #!!
colkey = get_hiplot_color(
conf$general$palette, -1,
conf$general$palette_custom
)
) # !!

params_biplot <- list(data3,
showLoadings = TRUE,
lengthLoadingsArrowsFactor = 1.5,
sizeLoadingsNames = 4,
colLoadingsNames = 'red4',
# other parameters
lab = NULL,
hline = 0, vline = c(-25, 0, 25),
vlineType = c('dotdash', 'solid', 'dashed'),
gridlines.major = FALSE, gridlines.minor = FALSE,
pointSize = 5,
legendPosition = 'none', legendLabSize = 16, legendIconSize = 8.0,
drawConnectors = FALSE,
title = 'PCA bi-plot',
subtitle = 'PC1 versus PC2',
caption = '27 PCs ≈ 80%',
returnPlot = TRUE)
if (!is.null(biplotShapeBy) && biplotShapeBy != "") {
showLoadings = TRUE,
lengthLoadingsArrowsFactor = 1.5,
sizeLoadingsNames = 4,
colLoadingsNames = "red4",
# other parameters
lab = NULL,
hline = 0, vline = c(-25, 0, 25),
vlineType = c("dotdash", "solid", "dashed"),
gridlines.major = FALSE, gridlines.minor = FALSE,
pointSize = 5,
legendPosition = "none", legendLabSize = 16, legendIconSize = 8.0,
drawConnectors = FALSE,
title = "PCA bi-plot",
subtitle = "PC1 versus PC2",
caption = "27 PCs ≈ 80%",
returnPlot = TRUE
)
if (!is.null(biplotShapeBy) && biplotShapeBy != "") {
params_biplot$shape <- biplotShapeBy
}
if (!is.null(biplotColBy) && biplotColBy != "") {
params_biplot$colby <- biplotColBy
params_biplot$colkey <- get_hiplot_color(conf$general$palette, -1,
conf$general$palette_custom) #!!
params_biplot$colkey <- get_hiplot_color(
conf$general$palette, -1,
conf$general$palette_custom
) # !!
}

p3 <- do.call(PCAtools::biplot, params_biplot)

p4 <- PCAtools::plotloadings(
data3, rangeRetain = 0.01, labSize = 4,
data3,
rangeRetain = 0.01, labSize = 4,
components = getComponents(data3, c(1:plotloadingsComponents)),
title = 'Loadings plot', axisLabSize = 12,
subtitle = 'PC1, PC2, PC3, PC4, PC5',
caption = 'Top 1% variables',
title = "Loadings plot", axisLabSize = 12,
subtitle = "PC1, PC2, PC3, PC4, PC5",
caption = "Top 1% variables",
gridlines.major = FALSE, gridlines.minor = FALSE,
shape = 24, shapeSizeRange = c(4, 8),
col = c(plotloadingsLowCol, plotloadingsMidCol, plotloadingsHighCol),
legendPosition = 'none',
legendPosition = "none",
drawConnectors = FALSE,
returnPlot = TRUE)
returnPlot = TRUE
)

eigencorplotMetavars <- unlist(eigencorplotMetavars)
if (length(eigencorplotMetavars) > 0) {
Expand All @@ -159,43 +170,47 @@ call_pcatools <- function(datTable, sampleInfo,
metavars = metavars,
cexCorval = 1.0,
fontCorval = 2,
posLab = 'all',
posLab = "all",
rotLabX = 45,
scale = TRUE,
main = "PC clinical correlates",
cexMain = 1.5,
plotRsquared = FALSE,
corFUN = 'pearson',
corUSE = 'pairwise.complete.obs',
signifSymbols = c('****', '***', '**', '*', ''),
corFUN = "pearson",
corUSE = "pairwise.complete.obs",
signifSymbols = c("****", "***", "**", "*", ""),
signifCutpoints = c(0, 0.0001, 0.001, 0.01, 0.05, 1),
returnPlot = TRUE)
returnPlot = TRUE
)

p6 <- plot_grid(
p1, p2, p3,
ncol = 3,
labels = c('A', 'B Pairs plot', 'C'),
label_fontfamily = 'serif',
label_fontface = 'bold',
labels = c("A", "B Pairs plot", "C"),
label_fontfamily = "serif",
label_fontface = "bold",
label_size = 22,
align = 'h',
rel_widths = c(1.10, 0.80, 1.10))
align = "h",
rel_widths = c(1.10, 0.80, 1.10)
)

p7 <- plot_grid(
p4,
as.grob(p5),
ncol = 2,
labels = c('D', 'E'),
label_fontfamily = 'serif',
label_fontface = 'bold',
labels = c("D", "E"),
label_fontfamily = "serif",
label_fontface = "bold",
label_size = 22,
align = 'h',
rel_widths = c(0.8, 1.2))
align = "h",
rel_widths = c(0.8, 1.2)
)

p <- plot_grid(
p6, p7,
ncol = 1,
rel_heights = c(1.1, 0.9))
rel_heights = c(1.1, 0.9)
)

out_xlsx <- paste(opt$outputFilePrefix, ".xlsx", sep = "")
write.xlsx(as.data.frame(data3$rotated), out_xlsx, row.names = TRUE)
Expand Down
71 changes: 70 additions & 1 deletion hisub.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,67 @@
# 6. 基于配置和输入文件生成 plot.R
#
# ./hisub.R examples/helloworld.R test_hello
VERSION = 0.1
VERSION <- 0.2

TEMPLATE <- '# @hiplot start
# @appname helloworld
# @apptitle
# Hiplot Hello World
# Hiplot 示例插件
# @target basic
# @tag test dotplot
# @author your name
# @url your project link
# @citation any reference you should link to
# @version 0.1.0
# @release 2021-01-01
# @description
# en: One sentence to describe this plugin.
# zh: 插件一段话简单介绍。
# @main helloworld
# @library ggplot2 readr
# @param data export::data::hiplot-textarea::{"default": "data.txt", "required": true}
# en: Data Table
# zh: 数据表
# @param x export::dataArg::data::{"index":1, "default": "mpg", "required": true}
# en: X Axis Variable
# zh: X 轴变量
# @param y export::dataArg::data::{"index":2, "default": "vs", "blackItems": "carb", "required": false}
# en: X Axis Variable
# zh: Y 轴变量
# @param size export::extra::slider::{"default":2, "min":0.5, "max":5, "step":0.5, "class":"col-12"}
# en: Dot Size
# zh: 点大小
# @param add_line export::extra::switch::{"default": true, "class":"col-12"}
# en: Add Line
# zh: 添加线图
# @return ggplot::["pdf", "png"]::{"cliMode": true, "title": "A test plot", "width":4, "height": 4, "theme": "theme_bw"}
# @data
# # You can write the code to generate the example data
# # "data.txt" described in parameter data, or you can
# # omit this tag and submit prepared data files.
# # File size <100Kb is recommended.
# # 此处可以编写生成示例数据(建议小于 100Kb)的代码
# # 示例数据文件需要跟数据表格参数对应起来
# # 或者忽略该标签,提交已经准备好的示例数据
# library(readr)
# data("mtcars")
# write_tsv(mtcars, "data.txt")
# @hiplot end
library(ggplot2)
helloworld <- function(data, x, y, size = 2, add_line = TRUE) {
if (y == "") stop("y must be provided!")
p <- ggplot(data, aes_string(x = x, y = y))
p <- p + geom_point(size = size)
if (add_line) {
p <- p + geom_line()
}
# Here export a ggplot object
# Or the whole main function generate a basic R plot
return(p)
}
'

message("HiSub version ", VERSION)
message("Copyright (c) 2021 Hiplot (https://hiplot.com.cn/)")
Expand All @@ -25,6 +85,15 @@ message("Checking input...")
Args <- commandArgs(trailingOnly = TRUE)
# Args <- c("test.R", "test-plugin2")

if (length(Args) == 1) {
if (Args[1] == "template") {
message("'template' command detected. Generating template 'source.R'.")
write_lines(TEMPLATE, file = "source.R")
message("Done")
quit("no")
}
}

# 如果传入的不是 2 个参数,中间的文件原样拷贝到插件目录以支持
# 已准备好的数据文件或其他所需脚本
fc <- file_content <- read_lines(Args[1])
Expand Down

0 comments on commit 0653261

Please sign in to comment.