我正在尝试制作一个简单的闪亮应用程序来创建通过用户选择进行分层的卡普兰-迈尔生存曲线。当我对 KM 计算进行静态编码时(使用列名 thorTr),它可以工作,但计算和绘图是静态的。当我用 input$s 替换时,我得到 ERROR:variable lengths different (found for 'input$s')
我试过查看其他使用 as.formula 和 paste 的代码,但我不明白也无法开始工作。但我是一个新的 R 和 Shiny 用户,所以也许我没有做对。这是一个类似的闪亮应用程序,但我想使用 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)
我希望有一个用用户选择更新分层变量的图