在Shiny中绘制Kaplan-Meier图

时间:2019-08-05 14:41:45

标签: r shiny

我正在尝试将Kaplan Meier的实例转换为rshiny应用程序,在其中可以降低要使用的因子。

我已经包含了允许在单个实例中生成图的代码。 access_sam2 [[x]]将引用该列,该列将包括要基于其进行分层的数据。

access_sam2<-access_sam2 %>% mutate(factorHL=ifelse(access_sam2[[x]] >=(median(access_sam2[[x]], na.rm=TRUE)), "high", "low"))  #Creates the sections
access_sam2$Area_mTLS_periHL<-factor(access_sam2$Area_mTLS_periHL)
fit<-survfit(surv_object~access_sam2$Area_mTLS_periHL)
ggsurvplot(fit, data=access_sam2, pval=TRUE, ylab="PFS",  xlab="Time (months)", title="mTLS Peri", legend.labs=c("High Area","Low Area"))

rShiny应用程序:

ui <- fluidPage(

  # App title ----
  titlePanel("Kaplan-Meier"),

  # Sidebar layout with input and output definitions ----
  sidebarLayout(

    # Sidebar panel for inputs ----
    sidebarPanel(

      # Input: Selector for variable to plot against mpg ----
      selectInput("variable", "Variable:",
                  c("Distance_iTLS_intra" = "Distance_iTLS_intra",
                    "Distance_iTLS_peri" = "Distance_iTLS_peri",
                    "Distance_iTLS" = "Distance_iTLS"))

    ),

    # Main panel for displaying outputs ----
    mainPanel(

      # Output: Formatted text for caption ----
      h3(textOutput("caption")),

      # Output: Kaplan Meier ----
         plotOutput("output$KM")

    )
  )
)


# Define server logic to plot various variables against mpg ----
server <- function(input, output) {
  access_sam2rx<-reactive(access_sam2 %>% mutate(factorHL=ifelse(access_sam2[[input$variable]] >=(median(access_sam2[[input$variable]], na.rm=TRUE)), "high", "low")))
  #reactive(if (access_sam2[[x]]>=median(access_sam2[[x]])) {
  #  access_sam2$factorHL<-"high"
  #} else {
  #  access_sam2$factorHL<-"low"
  #})
  reactive(survfit(surv_object~access_sam2rx$factorHL))
  reactive(surv_fit <-{survfit(Surv(time=access_sam2rx$Progression_Free_Time) ~ access_sam2rx$factorHL , data=access_sam2rx())
  })

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

# Create Shiny app ----
shinyApp(ui, server)

我收到错误消息:

Warning: Error in .getReactiveEnvironment()$currentContext: Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)
  64: mutate_impl
  63: mutate.tbl_df
  60: mutate.data.frame
  58: function_list[[k]]
  56: freduce
  55: _fseq
  54: eval
  53: eval
  51: %>%
  50: server [#2]
Error in .getReactiveEnvironment()$currentContext() : 
  Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)

0 个答案:

没有答案