4

我正在尝试制作一个简单的闪亮应用程序来创建通过用户选择进行分层的卡普兰-迈尔生存曲线。当我对 KM 计算进行静态编码时(使用列名 thorTr),它可以工作,但计算和绘图是静态的。当我用 input$s 替换时,我得到 ERROR:variable lengths different (found for 'input$s')

我试过查看其他使用 as.formula 和 paste 的代码,但我不明白也无法开始工作。但我是一个新的 R 和 Shiny 用户,所以也许我没有做对。这是一个类似的闪亮应用程序,但我想使用 survminer 和 ggsurvplot 进行绘图

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

#load data
data(GBSG2, package = "TH.data")


#Define UI for application that plots stratified km curves
ui <- fluidPage(

  # Sidebar layout with a input and output definitions
  sidebarLayout(

    # Inputs
    sidebarPanel(

      # Select variable strat
      selectInput(inputId = "s", 
                  label = "Select Stratification Variable:",
                  choices = c("horTh","menostat","tgrade"), 
                  selected = "horTh")

    ),

    # Outputs
    mainPanel(
      plotOutput(outputId = "km")
    )
  )
)

# Define server function required to create the km plot
server <- function(input, output) {

  # Create the km plot object the plotOutput function is expecting
  output$km <- renderPlot({

    #calc KM estimate with a hard coded variables - the following line works but obviously is not reactive
    #km <- survfit(Surv(time,cens) ~ horTh,data=GBSG2)

    #replaced hard coded horTh selection with the respnse from the selection and I get an error
    km <- survfit(Surv(time,cens) ~ input$s ,data=GBSG2)

    #plot km
    ggsurvplot(km)

  })

}

# Create a Shiny app object
shinyApp(ui = ui, server = server)

我希望有一个用用户选择更新分层变量的图

4

3 回答 3

2

两件事情:

  1. 调用中的公式survfit()需要明确定义。在原始代码中传递给的对象survfit()使用函数右侧的字符值。这会引发错误,我们可以通过将整个粘贴的值转换为公式来解决该错误,即as.formula(paste('Surv(time,cens) ~',input$s))
  2. 需要在调用中定义公式ggsurvplot()以避免范围问题。这有点技术性,并且与ggsurvplot()编程方式有关。基本上,ggsurvplot()无法访问在其自身调用之外定义的公式。

尝试更换

km <- survfit(Surv(time,cens) ~ input$s ,data=GBSG2)
ggsurvplot(km)

ggsurvplot(survfit(as.formula(paste('Surv(time,cens) ~',input$s)),data=GBSG2))
于 2019-01-07T22:26:49.760 回答
2

尝试使用surv_fit()而不是survfit().

surv_fit()正如 Byron 所建议的那样,是一个帮助survminer器,与 相比survival:survit(),它执行不同的作用域,这是您似乎需要的。

我的片段看起来像:

output$plot <- renderPlot({

    formula_text <- paste0("Surv(OS, OS_CENSOR) ~ ", input$covariate)

    ## for ggsurvplot, use survminer::surv_fit instead of survival:survfit
    fit <- surv_fit(as.formula(formula_text), data=os_df)
    ggsurvplot(fit = fit, data=os_df)
})
于 2019-01-16T21:41:02.290 回答
1

嗨,终于让这两种解决方案结合起来工作了。我不明白这个修复,但至少它现在按我想要的方式工作:)

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

data(GBSG2, package = "TH.data")

# Define UI for application that plots features of movies
ui <- fluidPage(

  # Sidebar layout with a input and output definitions
  sidebarLayout(

    # Inputs
    sidebarPanel(

      # Select variable strat
      selectInput(inputId = "s", 
                  label = "Select Stratification Variable:",
                  choices = c("Hormone Therapy" = "horTh",
                              "Menopausal Status" = "menostat",
                              "Tumor Grade" = "tgrade"), 
                  selected = "horTh")

    ),

    # Outputs
    mainPanel(
      plotOutput(outputId = "km")
    )
  )
)

# Define server function required to create the scatterplot
server <- function(input, output) {

  # Create the km plot object the plotOutput function is expecting
  output$km <- renderPlot({

    ## calc survival curve and plot
    kmdata <- surv_fit(as.formula(paste('Surv(time,cens) ~',input$s)),data=GBSG2)
    ggsurvplot(kmdata)

  })

}

# Create a Shiny app object
shinyApp(ui = ui, server = server)
于 2019-01-19T01:07:49.213 回答