0

I am creating an Rshiny app to run a an xgboost survival model. To do this, the categorical variables in the UI need to be entered into the model as one hot encoded variables. I created a blank dataframe and then used reactive values to adjust the variables. I also have the model preloaded. However when I run the app, I get this error:

xgb.DMatrix does not support construction from closure

I am trying to figure out a work around but need some advice

This is the code I have setup now

ui <- fluidPage(

       sidebarPanel(
         sliderInput("dxage", "Dx Age:",
                     value = 65, min = 18, max = 90
         ),
         selectInput("ethnism_t", "Race/Ethnicity:",
                     c("Non-Hispanic White"=1,
                       "Non-Hispanic Black"=2,
                       "Asian, Hawaiian, Pacific Islander"=3,
                       "Hispanic"=4,
                       "Other"=9)
                     
         ), actionButton("calculate", "Calculate")
         ),  
       
         mainPanel(
           plotOutput("survplot")
           
         ),
       )
   

server <- function(input, output) {

observeEvent(input$calculate, {
  df$dxage = input$dxage
  df$ethnism_1 <- ifelse(input$ethnism_t %in% 1, 1, 0)
  df$ethnism_2 <- ifelse(input$ethnism_t %in% 2, 1, 0)
  df$ethnism_3 <- ifelse(input$ethnism_t %in% 3, 1, 0)
  df$ethnism_4 <- ifelse(input$ethnism_t %in% 4, 1, 0)
  df$ethnism_9 <- ifelse(input$ethnism_t %in% 9, 1, 0)
  df$status = 0
  df$time = 1
  
})


  xgb_test_case <- reactive(as_sgb_data(df, time = time, status = status))
  
 output$survplot<- renderPlot({
   predict(fits, xgb_test_case)
 })
  

} shinyApp(ui = ui, server = server)

yussel60
  • 13
  • 3

1 Answers1

0

Can you maybe change the observeEvent to eventReactive. I guess the error is accessing a NULL, but not sure where exactly its coming from. Also note that xgb_test_case is a reactive function, so add brackets at the end: xgb_test_case() if you're using the xgb_test_case

my_df <- eventReactive(input$calculate,{
  df$dxage = input$dxage
  df$ethnism_1 <- ifelse(input$ethnism_t %in% 1, 1, 0)
  df$ethnism_2 <- ifelse(input$ethnism_t %in% 2, 1, 0)
  df$ethnism_3 <- ifelse(input$ethnism_t %in% 3, 1, 0)
  df$ethnism_4 <- ifelse(input$ethnism_t %in% 4, 1, 0)
  df$ethnism_9 <- ifelse(input$ethnism_t %in% 9, 1, 0)
  df$status = 0
  df$time = 1
  df <- as_sgb_data(df, time = time, status = status)
  df
})

 output$survplot<- renderPlot({
   req(my_df())
   predict(fits, my_df())
 })
Pork Chop
  • 28,528
  • 5
  • 63
  • 77