ggsurvplot在Rshiny中不起作用

时间:2018-07-03 11:09:16

标签: r

我正在尝试在Rshiny中使用ggsurvplot。但我无法获得情节。

安装库

library(shiny)
library(survival)
library(survminer)

已定义输入和输出

ui<-navbarPage("kaplan Meier",
               sidebarLayout(sidebarPanel(fileInput("file","Upload your csv",multiple=FALSE),
                                          tags$hr(),
                                          h5(helpText("Select the read.table parameters below")),
                                          checkboxInput(inputId = 'header', label = 'Header', value = FALSE),
                                          checkboxInput(inputId = "stringAsFactors", "stringAsFactors", FALSE),
                                          radioButtons(inputId = 'sep', label = 'Separator', 
                                                       choices = c(Comma=',',Semicolon=';',Tab='\t', Space=''), selected = ','),
                                          textInput("subdef","Define subset",value = "TRUE"),
                                          uiOutput("var5"),
                                          uiOutput("var6"),
                                          uiOutput("var9"),
                                          uiOutput("var7"),
                                          uiOutput("var8"),

检查km模型是否分层

checkboxInput(inputId="str",label="stratified KM estimates",value=FALSE),
actionButton("click","Submit")
                   ),
        mainPanel(helpText("KaplanMeier estimates"),
                             helpText("Survival Plot"),
                             plotOutput("survplot"))
                   )
    )

服务器从此处启动

server<-function(input,output){
  data <- reactive({
    file1 <- input$file
    if(is.null(file1)){return()} 
    file1<-read.table(file=file1$datapath, sep=input$sep, header = input$header, stringsAsFactors = input$stringAsFactors)
    return(file1)
  })
  f<-reactive({
    subset(data(),eval(parse(text=input$subdef)))
  })

输入函数变量

output$var5<-renderUI({
    selectInput("time","Select time variable",choices=as.list(names(f())),multiple=F)
  })
  output$var6<-renderUI({
    selectInput("cnsr","select censoring variable",choices=as.list(names(f())),multiple=F)
  })
  output$var9<-renderUI({
    selectInput("cnsrvalue","select event value",choices=unique(f()[,input$cnsr]),multiple=F)
  })
  output$var7<-renderUI({
    selectInput("group","select Treatment Group",choices=as.list(names(f())),multiple=F)
  })
  output$var8<-renderUI({
    selectInput("strata","select strata variable",choices=as.list(names(f())),multiple=F)
  })

使用survfit的kaplanmeier

kmsurvival<-eventReactive(input$click,{
    subdata<-f()
    if(input$str==FALSE){
      survfit(Surv(subdata[,input$time],subdata[,input$cnsr]==input$cnsrvalue)~subdata[,input$group],data=subdata,type="kaplan-meier")
    }
    else if(input$str==TRUE){
      survfit(Surv(subdata[,input$time],subdata[,input$cnsr]==input$cnsrvalue)~subdata[,input$group]+strata(subdata[,input$strata]),data=subdata,type="kaplan-meier")
    }
  })

renderPlot中的ggsurplot函数

output$survplot<-renderPlot({
    ggsurvplot(kmsurvival(),pval=F, conf.int=F,risk.table=T,risk.table.col=input$group,linetype = input$group)
  })
}
shinyApp(ui=ui,server=server)

0 个答案:

没有答案