Home > OS >  Disable action button in R facing problem for multiple inputs
Disable action button in R facing problem for multiple inputs

Time:09-17

Hi i am building an R code as follow to have an shiny app

ui = fluidPage(
  
  shinyjs::useShinyjs(),
  shinyjs::inlineCSS(appCSS),
  shinyFeedback::useShinyFeedback(),
  titlePanel("Predicting concrete strength",""),
  numericInput("CEM","Cement (kg)",""),
  numericInput("Water","Water (kg)",""),
  numericInput("PFA","Flyash (kg)",""),
  actionButton("submit", "Get the results", class = "btn-primary"),
  textOutput("class"),
  textOutput("class_prob")
  )

and the server code is

server <- function(input, output, session) {
  
   inputsValues = reactiveValues(inputs = NULL)
  
## to disable action button until all inputs are given
   observe({
     
     if(input$CEM!="" && input$Water!= "" && input$PFA!=""){
       shinyjs::enable("submit")
     } else {
       shinyjs::disable("submit")
     }
   })
   # putting some feedback if inputs are wrongly given
  observeEvent(input$submit,{
    
    cem_con<- (as.numeric(input$CEM) > 163 & as.numeric(input$CEM) < 3307)
    shinyFeedback::feedbackDanger("CEM", !cem_con  , " abc")
    wat_con<- (as.numeric(input$Water) > 131 & as.numeric(input$Water) < 1640)
    shinyFeedback::feedbackDanger("Water",!wat_con  , "abc")
    pfa_con<-(as.numeric(input$PFA) > 55 & as.numeric(input$PFA) < 1617)
    shinyFeedback::feedbackDanger("PFA", !pfa_con, "abc")
    req(cem_con,wat_con)
    inputsValues$inputs<-c("CEM"=input$CEM,"Water"=input$Water,"PFA"=input$PFA)
    inputsValues$inputs<-as.numeric(inputsValues$inputs)
  })
  
  output$class_prob<- renderText(inputsValues$inputs)
  output$class <- renderText(sum(inputsValues$inputs))
  
}                             

and when I run the app using

shinyApp(ui, server)

its stops and give the following error

Listening on http://126.0.0.1:3739 Warning: Error in enable: could not find function "enable" [No stack trace available]

CodePudding user response:

To check for missing value, you could use is.na() as shown below.

observe({
    if (is.na(input$CEM) | is.na(input$Water) | is.na(input$PFA) ) {
      shinyjs::disable("submit")
    } else {
      shinyjs::enable("submit")
    }
  })
  • Related