0

I am building a shiny app which calculates the potential score based on different features. In this app, there are "numericInput" boxes to allow user to assign weights to the features. I want to pop up modelDialog box if the summation of all weights are not equal to 1(which is 100%). I want to pop up this with eventReactive. If summation of all weights are 1 the following code should execute, other wise it should popup modelDialog. Please suggest a solution

Venkiii
  • 57
  • 1
  • 7
  • You can use observeEvent instead of eventReactive? – Sabri Karagönen Jun 07 '18 at 06:53
  • 1
    It's very difficult to make suggestions with as little information as you have provided. Please read about [reproducible examples](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) and [how to ask good questions](https://stackoverflow.com/help/how-to-ask), then edit your question with something more to go on. – r2evans Jun 07 '18 at 07:36

1 Answers1

0

somthing like this could work

score = eventReactive(input$some_trigger,{
  if(sum(input_weights) != 1){
    showModal(
      modalDialog(
        tags$p("The sum of your weights are not adding up to 1. Please correct the accordingly"),
        title = "Error: Incorrect weights"
      )
    )
  }
  req(sum(input_weights) == 1)
   #continue with calculation of score
})

Hope this helps!!

Bertil Baron
  • 4,923
  • 1
  • 15
  • 24
  • Thanks. its a great help, @bertil Baron – Venkiii Jun 07 '18 at 12:12
  • Hi Bertil Baron, How to use numericInputs which are having NULL values by default in sum function? According to user requirement I set numeric input box values(weights for features) as NULL. I have to do summation whether the all weights are equal to 1 or not. My code is working still user provides at least some value in numeric input box. But it is abnormally closing down if user not enter weight into to the numeric input box and click on calculate score button. Please get me out of this. – Venkiii Jun 08 '18 at 13:48
  • Use the parameter `na.rm = TRUE` in the sum function! – Bertil Baron Jun 08 '18 at 14:07