I use ggsurvplot to draw a survival curve, and I want to input text to the parameter P value. When the input content is character, it can be displayed correctly, however, when the input content is numeric, an error will occur.
The input data is as follows:
The full code is as follows:
rm(list = ls())
options(scipen = 200)
options(encoding = "UTF-8")
options(stringsAsFactors = TRUE)
library(survival)
library(survminer)
library(shiny)
library(bslib)
library(shinythemes)
mIHC <<- read.csv("0 expr.csv",header=TRUE,row.names=1,check.names = FALSE)
gene_list <<- colnames(mIHC)[3: dim(mIHC)[2]]
gene_list_order = gene_list[order(gene_list)]
ui <- fixedPage(
tags$style(HTML("
.navbar .navbar-header {float: left}
.navbar .navbar-nav {float: right}
")
),
navbarPage(
windowTitle = "GMAP",
fluid = TRUE,
# theme = bs_theme(bootswatch = "flatly",),
title = span("GMAP"),
tabPanel(
"Introduction",
),
tabPanel(
"Survival analysis",
sidebarLayout(
sidebarPanel(width = 5,
selectInput("gene_name", "Gene symbol", choices = gene_list_order),
sliderInput("cutoff_per", "Cutoff percent",
value = 0.5, min = 0, max = 0.99, step = 0.01,
ticks = TRUE)
),
mainPanel(width = 7,
tabsetPanel(
tabPanel("Plot",
plotOutput("surv", width = "420px", height = "400px"),
downloadButton('downloadPlot','Download Plot')),
tabPanel("Summary"),
tabPanel("Table")
)
)
)
),
tabPanel(
"Statistics analysis",
),
tabPanel(
"Heatmap"
),
tabPanel(
"About"
)
)
)
server <- function(input, output, session) {
env <- parent.frame()
plot2 <- reactive({
gene_name = input$gene_name
cutoff_per = input$cutoff_per
surv_gene = mIHC[ , c("OS", "event", gene_name)]
plot(surv_gene$OS, surv_gene$event)
})
surv_plot <- reactive({
gene_name = input$gene_name
cutoff_per = input$cutoff_per
surv_gene = mIHC[ , c("OS", "event", gene_name)]
surv_temp = surv_gene
surv_temp = cbind(surv_temp,surv_temp[,1])
colnames(surv_temp) = c("OS", "event", gene_name, "group")
for (row_place in 1: dim(surv_temp)[1]) {
if(surv_temp[row_place, 3] > quantile(surv_temp[,3], cutoff_per)) {
surv_temp[row_place, "group"] = "high"
} else {
surv_temp[row_place, "group"] = "low"
}
}
surv_gene <- surv_temp
fit <- eval(parse(text = paste0("survfit(Surv(OS, event) ~ group, data = surv_gene)")))
p_val = surv_pvalue(fit, data = surv_gene, method = "1")
p_val = round(as.numeric(p_val),2)
# p_val = as.character(p_val)
# p_val = "abc"
ggsurv_doc <- eval(parse(text = paste0("survfit(Surv(OS, event) ~ group, data = surv_gene)")))
ggpar(
ggsurvplot(ggsurv_doc,
data = surv_gene,
# ggtheme = theme_bw(),
conf.int = F,
censor = T,
palette = c("#DC143C", "#4071B3"),
legend.title = colnames(surv_gene)[3],
pval = paste("P =", p_val),
# pval = T,
legend.labs=c("High", "Low"),
# legend.labs=unique(surv_gene$group),
surv.median.line = "hv",
break.time.by = 12,
xlab = "Time (months)",
),
font.main = 13,
font.submain = 13,
font.x = 13,
font.y = 13,
font.caption = 13,
font.title = 13,
font.subtitle = 13,
font.legend = 13,
font.tickslab = 13,
)
})
output$surv <- renderPlot({
surv_plot()
}, res = 96)
output$downloadPlot <- downloadHandler(
filename = function() {
paste("plot.pdf")
},
content = function(file) {
pdf(file, width = 4.5,height = 4.5)
print(surv_plot(), newpage = FALSE)
dev.off()
}
)
}
shinyApp(ui, server)
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
enter image description here
It works correctly when p_val is a character, as follow:
p_val = "abc"
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
enter image description here
CodePudding user response:
This looks wrong:
p_val = surv_pvalue(fit, data = surv_gene, method = "1")
p_val = round(as.numeric(p_val),2)
surv_pvalue
returns a data.frame but you seem to be treating it like a numeric. Perhaps try:
p_val = surv_pvalue(fit, data = surv_gene, method = "1")$pval[[1]]
p_val = round(as.numeric(p_val),2)