Search code examples
shinyreactive-programming

eventReactive action button failure in R shiny


`I have done my best over several iterations to create an application that will run a survival model given inputs. The data formats and functions all seem to be working fine, however I checked both the app and the reactlogs but it doesn't seem like the action button connects or fires anythings once it runs. I'm not sure what the mistake is

ui <- fluidPage(
 sidebarPanel(
  sliderInput(
  "age",
  "alue = 65,
  min = 18,
  max = 90
),
selectInput(
  "sm_t",
  "Race/Ethnicity:",
  choices = c(
    "1" = 1,
    "2" = 2,
    "3" = 3,
    "4" = 4,
    "9" = 9
  )
),
actionButton("generate", "generate")
),
 mainPanel(
h1("T"),
h3("Survival"),
br(),
plotOutput("survival_plot")
)

)

server <- function(input, output) {

  pred <- eventReactive(input$generate, {


Data <- data.frame(
  age = input$dxage,
  sm1_1 = ifelse(input$sm_t %in% 1, 1, 0),
  sm1_2 = ifelse(input$sm_t %in% 2, 1, 0),
  sm1_3 = ifelse(input$sm_t %in% 3, 1, 0),
  sm1_4 = ifelse(input$sm_t %in% 4, 1, 0),
  sm1_9 = ifelse(input$sm_t %in% 9, 1, 0)
)


mdata <- sgb_data(Data, label = sgb_label(2, 0))




  pred<- predict(sgb_fit, mdata)

 })

 output$survival_plot <- renderPlot({


newdata <- data.frame(time = 1:61)

#these functions are loaded in prior
newdata$survival_prob <- exp(-cumsum(baseline_hazard(hazard, newdata$time)))


ggplot(newdata) +
  geom_line(aes(x = time, y = survival_prob), size = 1) +
  scale_x_continuous(limits = c(1, 61), expand = c(0, 0)) +
  labs(x = "Time", y = "Survival probability", 
       title = "Survival curves for new case") +
  theme_classic()
})

}

Solution

  • The user in the comments were correct. In the hazard function, pred() wasn't being called. This is what it should look like

    newdata$survival_prob <- exp(-cumsum(baseline_hazard(hazard, newdata$time)* pred()))