2

Currently my actionButton is taking in user input through drop down lists, and then when the actionButton is pressed it outputs this to a csv file. However, if the user only inputs 2 out of the 5 drop down lists and presses the actionButton this will still go to the csv file. How can I make it so it only accepts the actionButton if all inputs are given? I am using observeEvent().

picador
  • 67
  • 6
  • 4
    Take a look at [req](https://shiny.rstudio.com/reference/shiny/1.0.2/req.html). You might want to disable the button until all inputs are filled using [shinyjs](https://deanattali.com/shinyjs/) – GyD May 18 '18 at 09:13

2 Answers2

3

You can create an ActionButton named Submit, after clicking that button you can execute the code, but before the code give an if condition that checks if there is any input given.

In ui use:

actionButton(inputId = "SubmitButton",label = "Submit")

In server you can use:

 observeEvent(input$SubmitButton,
{
 if(input$one!=""&&input$two!=""&&!is.na(input$three))
   {
    ###You't code involving the multiple inputs
   }
 }

Now the code will only work if the Submit Button is pressed and the inputs are not empty or NULL based on what you want. I suggest that you initialize the inputs as "" before in the server section so that this will work Be careful while choosing input$name!="" or !is.na(input$name).

In addition to this, you can have a popup if he/she did not fill all the inputs and ask him/her to fill it all by creating a showModal in the server session.

observeEvent(input$SubmitButton,
    {
     if(input$one!=""&&input$two!=""&&!is.na(input$three))
       {
        ###You't code involving the multiple inputs
       }
     else
       {
         showModal(modalDialog(title ="Warning!!!", "Please fill all the fields before you click the Submit buttion!!!"))
       }
     }

Hope this helps!!!

Vedha Viyash
  • 708
  • 1
  • 8
  • 19
2

if you use the packages shinyjs you can deactivate the button when not all inputs are given with something like this.

observe(
    {
     if(input$one!=""&&input$two!=""&&!is.na(input$three))
       {
        enable("SubmitButton")
       }
     else
       {
         disable("SubmitButton")
       }
     })

just remeber you have to include useShinyjs() somewhere in your ui.

Hope this helps!

Bertil Baron
  • 4,923
  • 1
  • 15
  • 24