当我尝试在 reactive() 函数中更改值的类型时发生了一些错误
Some errors happened when I tried to change a value's type in reactive() function
我用ggsurvplot画生存曲线,想给参数P值输入文字。输入内容为字符时,可以正确显示,输入内容为数字时,会出现错误。
输入数据如下:
enter image description here
完整代码如下:
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)
enter image description here
当p_val为字符时正常工作,如下:
p_val = "abc"
enter image description here
这看起来不对:
p_val = surv_pvalue(fit, data = surv_gene, method = "1")
p_val = round(as.numeric(p_val),2)
surv_pvalue
returns a data.frame 但您似乎将其视为数字。也许试试:
p_val = surv_pvalue(fit, data = surv_gene, method = "1")$pval[[1]]
p_val = round(as.numeric(p_val),2)
我用ggsurvplot画生存曲线,想给参数P值输入文字。输入内容为字符时,可以正确显示,输入内容为数字时,会出现错误。
输入数据如下:
enter image description here
完整代码如下:
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)
当p_val为字符时正常工作,如下:
p_val = "abc"
这看起来不对:
p_val = surv_pvalue(fit, data = surv_gene, method = "1")
p_val = round(as.numeric(p_val),2)
surv_pvalue
returns a data.frame 但您似乎将其视为数字。也许试试:
p_val = surv_pvalue(fit, data = surv_gene, method = "1")$pval[[1]]
p_val = round(as.numeric(p_val),2)