我正在尝试制作一个简单的闪亮的ap,用于创建卡普兰-迈尔生存曲线,这些曲线由用户所做的选择分层。当我静态编码 KM 计算(列名为 thorTr)时,它可以工作,但计算和绘图是静态的。当我用input$s替换时,我得到错误:可变长度不同(为"input$s"找到)
我尝试查看其他使用 as.formula 和粘贴的代码,但我不明白,无法开始工作。但我是一个新的R和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)
我希望有一个图,使用用户选择更新分层变量
尝试使用 surv_fit()
而不是 survfit()
。
surv_fit()
是来自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)
})
两件事:
- 需要显式定义调用
survfit()
中的公式。在原始代码中传递给survfit()
的对象使用函数右侧的字符值。这抛出了一个错误,我们可以通过将整个粘贴的值转换为公式来解决,即as.formula(paste('Surv(time,cens) ~',input$s))
- 需要在调用
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))
终于把它结合起来了两种解决方案。我不明白修复程序,但至少它现在按照我希望它的方式工作:)
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)