闪亮的 ggsurvplot:p 值 = TRUE 时出错

ggsurvplot in shiny: Error when p-value = TRUE

我想在 shine 中做一个生存图,但是当我尝试在图中设置 p 值时我有一个不当行为。当 p 值被禁用时,情节是好的。但是当pval = TRUE时我得到一个错误,说它找不到“surv_object”函数。使用局部变量,它工作正常。

rm(list = ls())

library(shiny)
library(shinyauthr)
library(survival)
library(survminer)

time=c(23,6,53,28,8,5,47,2,4,4,8,25,22,6,6,4,25,25,28,28,28,28,30,31,13,33,16,22,28,6,13,23,23,14,21,18,9,13,10,23,34,39,0,4,16,11,30,44)
death=c(1,1,0,1,1,1,0,1,1,1,1,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,1,0,0,1,1,0,0,0,0,0,0,0,1,0,0,0,1,1,1,1,1,0)
var=c("Wild","Wild","Wild","Wild","Wild","Wild","Wild","Alt","Wild","Alt","Wild",
        "Wild","Alt","Wild","Wild","Alt","Wild","Wild","Wild","Alt","Wild","Wild",
        "Wild","Wild","Wild","Wild","Wild","Wild","Wild","Wild","Wild","Wild","Wild",
        "Wild","Wild","Wild","Wild","Wild","Wild","Wild","Wild","Wild","Alt","Wild",
        "Wild","Wild","Alt","Alt")

data = data.frame(
  time = time,
  death = death,
  var = var)

ui = fluidPage(
  fluidRow(
    column(5,
           plotOutput(outputId = "plot1")
    ), 
    column(5,
           plotOutput(outputId = "plot2")
    )
  )
)

server = function(input, output) {
  
  surv_object = reactive({
    so = Surv(time = data$time, event = data$death)
  })
  
  # this works fine
  output$plot1 = renderPlot({
    fit = survfit(formula = surv_object() ~ var, data = data)
   ggsurvplot(fit = fit,  data = data)
  })
  
  # when pval=TRUE, stop works.
  output$plot2 = renderPlot({
    fit = survfit(formula = surv_object() ~ var, data = data)
    ggsurvplot(fit = fit,  data = data, pval = TRUE)
  })
  
}

shinyApp(ui=ui, server = server)

这似乎可以完成工作:

library(shiny)
library(survival)
library(survminer)

time=c(23,6,53,28,8,5,47,2,4,4,8,25,22,6,6,4,25,25,28,28,28,28,30,31,13,33,16,22,28,6,13,23,23,14,21,18,9,13,10,23,34,39,0,4,16,11,30,44)
death=c(1,1,0,1,1,1,0,1,1,1,1,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,1,0,0,1,1,0,0,0,0,0,0,0,1,0,0,0,1,1,1,1,1,0)
var=c("Wild","Wild","Wild","Wild","Wild","Wild","Wild","Alt","Wild","Alt","Wild",
        "Wild","Alt","Wild","Wild","Alt","Wild","Wild","Wild","Alt","Wild","Wild",
        "Wild","Wild","Wild","Wild","Wild","Wild","Wild","Wild","Wild","Wild","Wild",
        "Wild","Wild","Wild","Wild","Wild","Wild","Wild","Wild","Wild","Alt","Wild",
        "Wild","Wild","Alt","Alt")

data = data.frame(
  time = time,
  death = death,
  var = var)

ui = fluidPage(
  fluidRow(
    column(5,
           plotOutput(outputId = "plot2")
    )
  )
)

server = function(input, output) {
  
  surv_object = reactive(Surv(time = data$time, event = data$death))
  
  output$plot2 = renderPlot({
    ggsurvplot(do.call(survfit,
            list(formula = surv_object() ~ var, data=data)), pval=TRUE)
  })
}

shinyApp(ui=ui, server = server)