How to make actionButton only work when all inputs are given in R shiny?

无人久伴 提交于 2021-02-08 04:19:30

问题


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().


回答1:


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!




回答2:


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!!!



来源:https://stackoverflow.com/questions/50407728/how-to-make-actionbutton-only-work-when-all-inputs-are-given-in-r-shiny

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!