首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Cox模型在闪亮应用程序中的打印结果

Cox模型在闪亮应用程序中的打印结果
EN

Stack Overflow用户
提问于 2019-06-02 01:08:46
回答 1查看 285关注 0票数 0

我正试图将Cox模型的结果添加到一个R闪亮的应用程序中。不幸的是,结果没有用下面的代码行打印出来:

代码语言:javascript
复制
 textOutput('modelSummary')

另外,我想通过复选框添加可选的协变量。例如,我有兴趣在最后一个‘selectInput’下面添加如下内容:

代码语言:javascript
复制
  checkboxGroupInput("variable", "Covariates:",
                     c("Covariate 1" = "cov1",
                       "Covariate 2" = "cov2",
                       "Covariate 3" = "cov3"))

然后将所选变量添加到Cox模型中。

完整的闪亮应用程序如下:

代码语言:javascript
复制
#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
#    http://shiny.rstudio.com/
#
library(shiny)
library(survival)
library(survminer)
library(ggplot2)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      fileInput("file","File input"),

      selectInput("Survival",
                  label = "Length of Survival",
                  choices =  c('survival1',
                               'survival2'),
                  selected = 'survival1'),

      selectInput("Treatment",
                  "Treatment",
                  choices = c("treatment1", "treatmentt2"),
                  selected = "treatment1"),

      selectInput("Endpoint",
                  "Endpoint",
                  choices = c("endpoint1", "endpoint2"),
                  selected = "endpoint1")

    ),
    mainPanel(
      plotOutput("KM"), 
      textOutput('modelSummary')
    )
  )
)

server <- function(input, output) {
  options(shiny.maxRequestSize=40*1024^2)
  raw_surv_data <- reactive({
    req(input$file)
    read.csv(input$file$datapath)
  })

  surv_data <- reactive({
    raw_surv <- raw_surv_data()
    data.frame(
      Time = raw_surv[[input$Survival]],
      Treatment    = raw_surv[[input$Treatment]],
      Endpoint  = raw_surv[[input$Endpoint]]
    )
  })


  surv_fit <- reactive({

    survfit(Surv(Time , Endpoint) ~ Treatment, data = surv_data())
  })

  output$KM <- renderPlot({  
    ggsurvplot(surv_fit(), risk.table = TRUE, data = surv_data())
  })

  cox_fit <- reactive({
  coxph(Surv(Time, Endpoint) ~ Treatment, data = surv_data())
  })


  output$modelSummary <- renderPrint({
  summary(cox_fit)
  })


}

shinyApp(ui = ui, server = server)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-06-03 13:27:58

要从复选框中添加协变量,可以这样做:

代码语言:javascript
复制
cox_fit <- reactive({
  model <- as.formula(paste0("Surv(Time, Endpoint) ~", 
                             paste0(input$variable, collapse = "+")))
  coxph(model, data = surv_data())
})
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56411531

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档