当我尝试在 reactive() 函数中更改值的类型时发生了一些错误

Posted

技术标签:

【中文标题】当我尝试在 reactive() 函数中更改值的类型时发生了一些错误【英文标题】:Some errors happened when I tried to change a value's type in reactive() function 【发布时间】:2022-01-08 07:20:36 【问题描述】:

我用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

【问题讨论】:

【参考方案1】:

这看起来不对:

p_val = surv_pvalue(fit, data = surv_gene, method = "1")
p_val = round(as.numeric(p_val),2)

surv_pvalue 返回一个 data.frame 但您似乎将其视为数字。也许试试:

 p_val = surv_pvalue(fit, data = surv_gene, method = "1")$pval[[1]]
 p_val = round(as.numeric(p_val),2)

【讨论】:

非常感谢您的帮助。图片可以正确显示。我试图搜索一些答案,但发现没有人遇到过类似的问题。它让我明白,一个难题可能很常见,但很容易被忽视。再次感谢!

以上是关于当我尝试在 reactive() 函数中更改值的类型时发生了一些错误的主要内容,如果未能解决你的问题,请参考以下文章

Angular Reactive Form - 订阅表单数组中的表单控件实例以获得总值

如何在不更改 matplotlib 默认值的情况下使用 seaborn?

Intellij 在运行时不会检测到代码更改

使用 Spring boot webflux reactive 记录未持久化到 R2DB

vue.reactive 不是函数

如何更改 flexdashboard selectInput 中的 y