Reactive Value Problem Shiny app

Hello everyone !

I have a reactive value problem which is as follow

Error : Can't access reactive value 'launch' outside of reactive consumer. ℹ Do you need to wrap inside reactive() or observe()?

I am trying to make a shiny app, but i have some big troubles with the server side. Indeed, I would like to see my inputs in a dataframe and then predict a result with my Machine Learning algorithm using my input.

I would appreciate any help, thanks to everyone who will help me

ui.R

library(plotly)
library(shiny)
library(shinyjs)
library(shinysky)
library(data.table)
library(shinyalert)
library(shinyWidgets)
library(shinydashboard)
library(shinydashboardPlus)
library(tidyverse)
library(DT)
library(data.table)
library(lubridate)
library(tidymodels)
library(xgboost)



useShinyalert()



dashboardPage(skin = "red",
              dashboardHeader(title = tagList(
                tags$span(
                  class = "logo-mini", "ICD"
                ),
                tags$span(
                  class = "logo-lg", "C.difficile"
                ))),
              dashboardSidebar(
                sidebarMenu(
                  menuItem("Home", tabName = "Home",icon = icon("home")),
                  menuItem("Clostridioide difficile",tabName = "test",icon = icon("bacteria"))
                )
              ),
              dashboardBody(
                useShinyjs(),
                tabItems(
                  tabItem(tabName = "Home",
                          shinydashboardPlus:: userBox(
                            title = userDescription(
                              title = "ICD",
                              type = 1,
                              image = "ICD.png"
                            ),
                            type = NULL,
                            color = "purple",
                            width = 12,
                            background = NULL,
                            closable = TRUE,
                            tags$br(),
                            tags$br(),
                            h4("Shiny Apps developped"),
                            h5("The Machine Learning performed in this shiny Apps are for research purposes. We disclaim all responsibility for clinical use!")
                          ),tags$br(),
                          tags$br(),
                          userBox(
                            title = userDescription(
                              title = "JPL",
                              subtitle = "Pharmacist Resident",
                              type = 2,
                              image = "JPL.jpg"
                            ),
                            type = NULL,
                            color = "gray",
                            width = 4,
                            background = NULL,
                            closable = TRUE,
                            tags$br(),
                            tags$br(),
                            "Machine Learning model designer",
                            tags$br(),
                            tags$br(),
                         
                          ),tags$br(),tags$br(),
                          userBox(
                            title = userDescription(
                              title = "Pr 1",
                              subtitle =  "Head of Infectious Diseases Department",
                              type = 2,
                              image = "JFF.jpg"
                            ),
                            type = NULL,
                            color = "gray",
                            width = 4,
                            src = "JFF.jpg",
                            background = NULL,
                            closable = TRUE,
                            tags$br(),
                            "Project Supervisor",
                            tags$br(),
                            tags$br(),
                            tags$br(),
                            tags$br()),
                          userBox(
                            title = userDescription(
                              title = "Dr 2",
                              subtitle =  "Pharmacist Resident",
                              type = 2,
                              image = "NGB.jpg"
                            ),
                            type = NULL,
                            color = "gray",
                            width = 4,
                            src = "NGB.jpg",
                            background = NULL,
                            closable = TRUE,
                            tags$br(),
                            tags$br(),
                            "Project Supervisor",
                            tags$br(),
                            tags$br(),  
                          )),
                  
                  tabItem(tabName = "test",
                          ###### Machine Learning--------
                          box(title = "Clostridioides difficile risk by Machine Learning approach",
                              dropdownButton(tags$h4("Epidemiologic data"), div(style="display: inline-block;vertical-align:top; width: 200px;"),
                                             circle = TRUE, inline=T, status = "success", icon = icon("hospital-user"), width = "300px",
                                             tooltip = tooltipOptions(title = "Epidemiologic data" ),
                                             collapsible = T,
                                             #Input: Patient ID
                                             textInput(inputId = "name", label = "patient ID", value = "patient1"),
                                             #Input: IMC ----
                                             selectInput(inputId = "IMC_25",
                                                         label = "CMI>25",
                                                         c("yes"="y", "no"="n")),
                                             
                                             #Input: IMC ----
                                             selectInput(inputId = "IMC_30",
                                                         label = "CMI>30",
                                                         c("yes"="y", "no"="n")),
                                             
                       
                                             
                                             # Input: Variable sexe ----
                                             selectInput(inputId = "sexe",
                                                         label = "Sexe",
                                                         c("Femme"="f", "Homme"="H")),
                                             # Input: Variable Age ----
                                             selectInput(inputId = "ANS_65",
                                                         label = "Age > 65",
                                                         c("yes"="y", "no"="n"))),
                              
                              dropdownButton(tags$h4("Patient's data during the hospitalisation stay"), div(style="display: inline-block;vertical-align:top; width: 200px;"),
                                             circle = TRUE, inline=T, status = "primary", icon = icon("procedures"), width = "300px",
                                             tooltip = tooltipOptions(title = "Informations about the hospitalisation stay" ),
                                             collapsible = T,
                                             # Input: Variable Duree sejour
                                             selectInput(inputId = "SEJ_7J",
                                                         label = "Hospital stay > 7 days",
                                                         c("yes"="y", "no"="n")),
                                             
                                             # Input: Variable Prise Antibiotiques
                                             selectInput(inputId = "ATB",
                                                         label = "Antibiotic use",
                                                         c("yes"="y", "no"="n")),
                                             
                                             # Input: Variable Duree Antibiotiques
                                             selectInput(inputId = "ATB_7J",
                                                         label = "Antibiotic use > 7 days",
                                                         c("yes"="y", "no"="n")),
                                             
                                             # Input: Variable Amoxicilline
                                             selectInput(inputId = "AMOX",
                                                         label = "Amoxicilline use",
                                                         c("yes"="y", "no"="n")),
                                             
                                             # Input: Variable C3G
                                             selectInput(inputId = "C3G",
                                                         label = "3rd Generation Cephalosporin use",
                                                         c("yes"="y", "no"="n")),
                                             
                                             # Input: Variable Tazocilline
                                             selectInput(inputId = "TAZO",
                                                         label = "Tazocilline use",
                                                         c("yes"="y", "no"="n")),
                                             
                                             # Input: Variable Immunodepression
                                             selectInput(inputId = "IMDEPR",
                                                         label = "Immunodepressed",
                                                         c("yes"="y", "no"="n")),
                                             
                                             # Input: Variable Albuminemie
                                             selectInput(inputId = "ALB_2.5GDL",
                                                         label = "hypoalbuminaemia < 2.5g/dl",
                                                         c("yes"="y", "no"="n"))),
                              
                              dropdownButton(tags$h4("Comorbidities and PPI use"), div(style="display: inline-block;vertical-align:top; width: 200px;"),
                                             circle = TRUE, inline=T, status = "danger", icon = icon("user-injured"), width = "300px",
                                             tooltip = tooltipOptions(title = "Comorbidities and PPI use" ),
                                             
                                             
                                             # Input: Variable IPP
                                             selectInput(inputId = "IPP",
                                                         label = "PPI use",
                                                         c("yes"="y", "no"="n")),
                                             
                                             # Input: Variable IPP > 6mois
                                             selectInput(inputId = "IPP_6MOIS",
                                                         label = "PPI use over 6 months",
                                                         c("yes"="y", "no"="n")),
                                             
                                             # Input: Variable IPP Recommandations
                                             selectInput(inputId = "RECOM_IPP",
                                                         label = "PPI use within the AMM indication",
                                                         c("yes"="y", "no"="n")),
                                             
                                             # Input: Variable diabète
                                             selectInput(inputId = "DT2",
                                                         label = "Type 2 Diabetes",
                                                         c("yes"="y", "no"="n")),
                                             
                                             # Input: Variable Charlson score 3
                                             selectInput(inputId = "CHARLSON_3",
                                                         label = "Comorbidity Charlson score > 3",
                                                         c("yes"="y", "no"="n")),
                                             
                                             # Input: Variable Charlson score 4
                                             selectInput(inputId = "CHARLSON_4",
                                                         label = "Comorbidity Charlson score > 4",
                                                         c("yes"="y", "no"="n")),
                                             
                                             # Input: Variable Charlson score 5
                                             selectInput(inputId = "CHARLSON_5",
                                                         label = "Comorbidity Charlson score > 5",
                                                         c("yes"="y", "no"="n"))),
                              collapsible = T,
                              tags$br(),
                              tags$br(),
                              actionBttn(inputId = "launch",
                                         label = "Submit",
                                         color = "primary",
                                         style = "material-flat",
                                         icon = icon("cog"),
                                         block = T
                              ),
                              tags$br(),
                              tags$br(),
                              infoBoxOutput("valueBox12", width = 12)
                          )
                  ))
              ))

server.R

server <- function(input, output) {

  #### Machine Learning ------

  data <- observe({
    # Execute selections on data upon button-press
    if(input$launch == 0) return()
    test <- isolate(
      "patient ID"= input$name, "CMI>25" = input$IMC_25,
      "CMI>30"= input$IMC_30,"Sexe"=input$sexe,
      "Age > 65"= input$ANS_65,"Hospital stay > 7 days" = input$SEJ_7J,
      "Antibiotic use"= input$ATB,
      "Antibiotic use > 7 days"= input$ATB_7J,
      "Amoxicilline use"= input$AMOX,
      "3rd Generation Cephalosporin use" = input$C3G,
      "Tazocilline use" = input$C3G,
      "Immunodepressed"= input$IMDEPR,
      "hypoalbuminaemia < 2.5g/dl"= input$ALB_2.5GDL,
      "PPI use"= input$IPP,
      "PPI use over 6 months"= input$IPP_6MOIS,
      "PPI use within the AMM indication"= input$RECOM_IPP,
      "Type 2 Diabetes"= input$DT2,
      "Comorbidity Charlson score > 3"= input$CHARLSON_3,
      "Comorbidity Charlson score > 4"= input$CHARLSON_4,
      "Comorbidity Charlson score > 5"= input$CHARLSON_5)
  })
  observe({   if(input$launch == 0) return()
    shinyalert(title = "Machine Learning estimation done !", type = "success")})
  

  # Reactive expression to create data frame of all input values ----
  preditValue <- reactive({
    xgb_model<-readRDS("final_model_xgb_231221.rds")
    test <- tibble(IMC_25 = input$IMC_25,
                   IMC_30 = input$IMC_30,
                   SEXE = input$sexe,
                   ANS_65 = input$ANS_65,
                   SEJ_7J = input$SEJ_7J,
                   ATB = input$ATB,
                   ATB_7J = input$ATB_7J,
                   AMOX = input$AMOX,
                   C3G = input$C3G,
                   TAZO  = input$C3G,
                   DT2 = input$DT2,
                   IMDEPR = input$IMDEPR,
                   ALB_2.5GDL = input$ALB_2.5GDL,
                   IPP = input$IPP,
                   IPP_6MOIS = input$IPP_6MOIS,
                   RECOM_IPP = input$RECOM_IPP,
                   CHARLSON_3 = input$CHARLSON_3,
                   CHARLSON_4 = input$CHARLSON_4,
                   CHARLSON_5 = input$CHARLSON_5
                   )
    
    round(predict(xgb_model,test, type = "prob"), digits = 2)
    
  })
  
output$valueBox12 <- renderInfoBox({
  a<-preditValue()
  color<- ifelse(a$.pred>0.5, 'red', 'light-blue')
   infoBox(title = "estimated risk to contract ICD" ,  color = color, icon = icon("notes-medical"), fill = TRUE)
  })

}


Thanks everyone

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.