如何修复R Shiny中的“错误:可变长度不同(为'input $ s'找到)”

时间:2019-01-06 04:58:27

标签: r shiny survival-analysis survival

我正在尝试制作一个简单的闪亮ap,以创建kaplan-meier生存曲线,并根据用户的选择进行分层。当我对KM计算进行静态编码(列名称为thorTr)时,它可以工作,但计算和绘图是静态的。当我用input $ s替换时,出现错误:可变长度不同(为“ input $ s”找到)

我尝试查看其他使用as.formula并粘贴的代码,但我不理解,无法正常工作。但是我是R and Shiny的新用户,所以也许我做对了。这是一个类似的闪亮ap,但我想使用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)

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

3 个答案:

答案 0 :(得分:0)

两件事:

  1. survfit()的调用中的公式需要明确定义。原始代码中传递给survfit()的对象使用函数右侧的字符值。这会引发错误,我们可以通过将整个粘贴的值转换为公式即as.formula(paste('Surv(time,cens) ~',input$s))
  2. 来解决。
  3. 需要在对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))

答案 1 :(得分:0)

尝试使用surv_fit()代替survfit()

surv_fit()survminer的助手,与Byron所建议的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 <- survfit(as.formula(formula_text), data=os_df)
    ggsurvplot(fit = fit, data=os_df)
})

答案 2 :(得分:0)

您好,终于让这两种解决方案结合起来了。我不了解此修复程序,但至少它现在可以按我想要的方式工作:)

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)