簡體   English   中英

For Loop 用於在 Shiny 中繪制生存曲線

[英]For Loop for plotting survival curve in Shiny

我正在使用 R 中survival package 的diabetic數據集。在該數據集中,列age 如果我要比較患者年齡的 plot 條事件發生時間曲線,我的生存曲線就會太多。 我想要它,以便當我 select 輸入“年齡”按鈕時,它將age列分成 4 組,這樣當我 plot 生存曲線時,我只有 4 條曲線。 雖然我不斷收到錯誤消息。 我是否正確編寫了循環?

ui <- navbarPage(title = "Diabetic Retinopathy",
                 ### Page 1 ###
                 tabPanel(title = "Survival Curves",
                          sidebarLayout(
                            sidebarPanel(
                              radioButtons(inputId = "comparison",
                                           label = "Select variable you wish to compare",
                                           choices = c("laser", "age", "eye", "risk"),
                                           selected = "laser")
                            ),
                            mainPanel(
                              tabsetPanel(
                                tabPanel(title = "Plot", 
                                         plotOutput(outputId = "curve")),
                                tabPanel(title = "Summary", 
                                         verbatimTextOutput(outputId = "details"))
                              )
                            )
                          )
                 )
      )

server <- function(input, output, session) {
  ### Page 1 ###
  fit <- reactive(
    surv_fit(as.formula(paste("Surv(time, status) ~ ", 
                              if(input$comparison == "age") {
                                for(i in diabetic$age) {
                                  if(i < 15) {
                                    diabetic$age[i] <- "lessthan15"
                                  } else if(i >= 15 & i < 30) {
                                    diabetic$age[i]<- "fifteento30"
                                  } else if(i >= 30 & i < 45) {
                                    diabetic$age[i] <- "thirtyto45"
                                  } else {diabetic$age[i] <- "fortyfiveplus"}
                                } 
                              } else {return(input$comparison)})), data = diabetic)
         )
  
  output$curve <- renderPlot(
    ggsurvplot(fit())
  )
}

shinyApp(ui, server)

錯誤信息圖片在這里

surv_fit(as.formula(paste("Surv(time, status) ~ ",等兩件事混淆了:

  1. input$comparison的值粘貼預測器(激光,年齡,...)
  2. 操縱 dataframe糖尿病年齡列 - 但是不返回任何有意義的公式項

您可以從一開始就向您的數據添加一列age_class ,並將其用作您的比較選擇器的一個選項。

PS:在沒有 if-else-skyscrapers 的情況下將數字(年齡)轉換為因子(age_class):

df$age_class <- cut(df$age, 
                    breaks=c(-Inf, 15, 30, 45, Inf),
                    labels=c("lessthan15","fifteento30",
                             "thirtyto45","fortyfiveplus")
                    )

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM