我正试图将Cox模型的结果添加到一个R闪亮的应用程序中。不幸的是,结果没有用下面的代码行打印出来:
textOutput('modelSummary')另外,我想通过复选框添加可选的协变量。例如,我有兴趣在最后一个‘selectInput’下面添加如下内容:
checkboxGroupInput("variable", "Covariates:",
c("Covariate 1" = "cov1",
"Covariate 2" = "cov2",
"Covariate 3" = "cov3"))然后将所选变量添加到Cox模型中。
完整的闪亮应用程序如下:
#
# 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)发布于 2019-06-03 13:27:58
要从复选框中添加协变量,可以这样做:
cox_fit <- reactive({
model <- as.formula(paste0("Surv(Time, Endpoint) ~",
paste0(input$variable, collapse = "+")))
coxph(model, data = surv_data())
})https://stackoverflow.com/questions/56411531
复制相似问题