This repository was archived by the owner on Apr 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathezcox.R
122 lines (113 loc) · 4.11 KB
/
ezcox.R
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#######################################################
# Easy cox analysis and visualization. #
#-----------------------------------------------------#
# Author: Shixiang Wang #
# #
# Date: 2020-08-10 #
# Version: 0.1 #
#######################################################
# CAUTION #
#-----------------------------------------------------#
# Copyright (C) 2020 by Hiplot Team #
# All rights reserved. #
#######################################################
# https://hiplot.com.cn/basic/ezcox
#
# @hiplot start
# @appname ezcox
# @apptitle
# Cox Models Forest
# Cox 模型森林图
# @target basic
# @tag model survival
# @author Hiplot Team | Shixiang Wang
# @email [email protected]
# @url https://github.com/ShixiangWang/ezcox
# @citation Wang, Shixiang, et al. "The predictive power of tumor
# mutational burden in lung cancer immunotherapy response is influenced
# by patients' sex." International journal of cancer (2019).
# @version v0.2.0
# @release 2020-08-11
# @description
# en: Used for survival data analysis.
# zh: 用于生存数据分析。
# @main call_ezcox
# @library readr ezcox
#
# @param data export::data::hiplot-textarea::{"default": "data.txt", "required": true}
# en: a table with at least 3 columns, the 1rd and 2nd columns refer to 'time' and
# 'survival' of sample.
# zh: 至少 3 列的数值的表格,第 1 列和第 2 列分别对应样本的生存时间和状态。
#
# @param covariates export::dataArg::data::{"index":1, "default":["sex","ph.ecog"], "blackItems":["time","status"], "required":true}
# en: Batch Process Covariates
# zh: 批处理协变量
# @param controls export::dataArg::data::{"index":2, "default":"age", "blackItems":["time","status"], "required":false}
# en: Controls
# zh: 控制变量
# @param vars_to_show export::dataArg::data::{"index":3, "blackItems":["time","status"], "required":false, "individual":true}
# en: Select Show Variables
# zh: 展示变量
# @param merge_models export::extra::switch::{"default": false}
# en: Merge Models
# zh: 合并模型
# @param drop_controls export::extra::switch::{"default": false}
# en: Drop Controls
# zh: 去除控制变量
# @param add_caption export::extra::switch::{"default": true}
# en: Add Caption
# zh: 添加说明文字
#
# @return ggplot::["pdf", "png"]::{"width": 6, "height": 4}
# @data
# # 此处可以编写生成示例数据的代码
# # 示例数据文件需要跟数据表格参数对应起来
# # 或者忽略该标签,手动提交示例数据
# library(readr)
# library(survival)
# write_tsv(lung[, c("time", "status", "sex", "ph.ecog", "age")], "data.txt")
# @hiplot end
pacman::p_load(ezcox)
call_ezcox <- function(data,
covariates,
controls,
merge_models,
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"
)
cat("WARN: rename the first and the second column as 'time' and 'status'.",
sep = "\n"
)
colnames(data)[1:2] <- c("time", "status")
}
data$time <- as.numeric(data$time)
data$status <- as.integer(data$status) # Can only be 0 or 1 here
# 协变量
if (covariates == "" || is.null(covariates)) {
covariates <- setdiff(colnames(data), c("time", "status"))
}
# 控制变量
if (controls == "" || is.null(controls)) {
controls <- NULL
}
# 结果图显示变量
if (vars_to_show == "" || is.null(vars_to_show)) {
vars_to_show <- NULL
}
ezcox::show_forest(
data = data,
covariates = covariates,
controls = controls,
merge_models = merge_models,
vars_to_show = vars_to_show,
drop_controls = drop_controls,
add_caption = add_caption
)
}