-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.R
106 lines (86 loc) · 2.24 KB
/
server.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
function(input, output, session) {
formula_string <- reactive({
req(input$predictor_var)
pred <- input$predictor_var
if (length(pred) == 1) {
preds <- pred
} else {
preds <- paste0(pred, collapse = " + ")
}
paste0("Surv(time, status) ~ ", preds)
})
predictor_fit <- reactive({
formula_object <- as.formula(formula_string())
# fit survival
fit <- survival::survfit(formula_object, data = dat)
fit$call$formula <- formula_object
fit
})
# download Veteran dataset
output$download_data <- downloadHandler(
filename = "veteran.csv",
content = function(file) {
write.csv(
dat,
file,
row.names = FALSE
)
}
)
### Chart tab ----------------------------
output$formula_string_out <- renderText({
formula_string()
})
predictor_chart <- reactive({
survminer::ggsurvplot(
predictor_fit(),
data = veteran,
risk.table = TRUE,
pval = TRUE,
conf.int = if (input$show_cl == "Yes") TRUE else FALSE,
risk.table.y.text = FALSE,
title = paste0("Survival Probability by ", paste0(input$predictor_var, collapse = " + "))
)
})
output$predictor_chart_out <- renderPlot({
predictor_chart()
})
output$download_chart <- downloadHandler(
filename = "veteran_chart.png",
# content is a function with argument file. content writes the plot to the device
content = function(file) {
ggsave(file, print(predictor_chart()), height = 10, width = 16)
}
)
### Table tab --------------------
fit_table_prep <- reactive({
req(predictor_fit())
fit <- predictor_fit()
hold_summary <- summary(fit)
cols <- lapply(c(8, 2:6, 9:11) , function(x) hold_summary[x])
# return a data frame
do.call(data.frame, cols)
})
output$fit_table_out <- renderDT({
datatable(
fit_table_prep(),
rownames = FALSE,
extensions = "Buttons",
options = list(
dom = "Bfltip",
pageLength = 12,
buttons = list(
list(
extend = "excel",
text = "Download",
title = "verteran_table"
)
)
)
) %>%
formatRound(
6:9,
digits = 3
)
}, server = FALSE)
}