Alternative to reset the action button value to 0 in R?

705 views
Skip to first unread message

Arun

unread,
Feb 25, 2016, 2:57:12 PM2/25/16
to Shiny - Web Framework for R
For one of our analysis, i have used sidebarpanel and tabset to show different steps on the same page. Each of the step has its own action button "GO".

Now when i execute all of them one by one the first time, am not seeing any issue. but the second time i try executing any of the step independently, its not executing the right functions. 

Below is my UI & Server Code

Server.R

    output$inc <-  renderTable({
      input$goButtonb
      input$goButtona
      input$goButtons
      
      
      if (input$goButtona == 0 &&
          input$goButtonb == 0 && input$goButtons == 0)
      {
        return()
      }
      
      else if (input$goButtona != 0)
      {
        isolate({
          withProgress(message = "Pulling the exp", {
            if (input$old_new_ind == TRUE)
              
            {
              incorrect_new_exp(
                user_name = input$Usernamea, date1 = input$datea,threshold = input$threshold, date2 = input$datea -
                  1
              )
              
            }
            
            else
              
            {
              incorrect_all_exps(
                user_name = input$Usernamea, date1 = input$datea,threshold = input$threshold, date2 = input$datea -
                  1
              )
            }
            
            
            
          })
          
        })
        
      }
      
      else if (input$goButtonb != 0 && input$goButtona == 0 && input$goButtons == 0)
        
      {
        isolate({
          withProgress(message = "Process started", {
            pool_information(
              tr_id = input$tr_da, trm_id = input$trmvrna, date3 = input$dateb
            )
            
          })
        })
        
        
      }
      
      else if (input$goButtons != 0 && input$goButtonb == 0 && input$goButtona == 0 )
        
      {
        isolate({
          withProgress(message = "Sample SOJ for the Pools", {
            soj_pool(
              user_name = input$Usernames,tr_id = input$t_tids,tvrsnid = input$tvrsns,date = input$datep, pool = input$pl
            )
            
            
          })
          
        })
        
      }
      
      else 
        
      {
        return()
      }
      
    })


UI.R

 navbarPage(
    "",  tabPanel(
      "Tools",
      sidebarPanel(
        helpText("Step 1 (Get the exp)"),
        dateInput("datea", label = "Date of Issue"),
        textInput("threshold", label = "Threshold", value = 0.1),
        textInput("Usernamea", label = "Username (please change)",value = "xxxxx"),
        checkboxInput("old_new_ind", label = "New Exs Only",value = FALSE),
        br(),
        actionButton("goButtona","Go!",icon = icon("refresh"))
      ),
      sidebarPanel(
        helpText("Step 2 (Get the pl Information)"),
        numericInput("tr_ida", label = "Tr ID",value = 1234),
        numericInput("tr_vrna", label = "Tr Vn", value = 1),
        dateInput("dateb", label = "Date"),
        br(),
        actionButton("goButtonb","Go!",icon = icon("refresh"))
      ),
      sidebarPanel(
        helpText("Step 3"),
        numericInput("tr_ids", label = "Tr ID",value = 1234),
        numericInput("trvrsn", label = "tr vr", value = 1.0),
        dateInput("datep", label = "Date"),
        textInput("pls", label = "Pl (Single value)"),
        textInput("Usernames", label = "username",value = "xxxxx"),
        br(),
        actionButton("goButtons","Go!",icon = icon("refresh"))
      )
      
      , mainPanel(tableOutput("inc"))
      
    )

Arun

unread,
Feb 25, 2016, 2:59:40 PM2/25/16
to Shiny - Web Framework for R
Sorry below is updated Server.R

Server.R

    output$inc <-  renderTable({
      input$goButtonb
      input$goButtona
      input$goButtons
      
      
      if (input$goButtona == 0 &&
          input$goButtonb == 0 && input$goButtons == 0)
      {
        return()
      }
      
      else if (input$goButtona != 0)
      {
        isolate({
          withProgress(message = "Pulling the exp", {
            if (input$old_new_ind == TRUE)
              
            {
              incorrect_new_exp(
                user_name = input$Usernamea, date1 = input$datea,threshold = input$threshold, date2 = input$datea -
                  1
              )
              
            }
            
            else
              
            {
              incorrect_all_exps(
                user_name = input$Usernamea, date1 = input$datea,threshold = input$threshold, date2 = input$datea -
                  1
              )
            }
            
            
            
          })
          
        })
        
      }
      
      else if (input$goButtonb != 0 && input$goButtons == 0)
        
      {
        isolate({
          withProgress(message = "Process started", {
            pool_information(
              tr_id = input$tr_da, trm_id = input$trmvrna, date3 = input$dateb
            )
            
          })
        })
        
        
      }
      
      else if (input$goButtons != 0)
        
      {
        isolate({
          withProgress(message = "Sample SOJ for the Pools", {
            soj_pool(
              user_name = input$Usernames,tr_id = input$t_tids,tvrsnid = input$tvrsns,date = input$datep, pool = input$pl
            )
            
            
          })
          
        })
        
      }
      
      else 
        
      {
        return()
      }
      
    })

Joe Cheng

unread,
Feb 25, 2016, 3:53:34 PM2/25/16
to Arun, Shiny - Web Framework for R
If you have three action buttons that are each going to have their own unique way of populating the same table, you can do that with a reactive values object plus observeEvent. In your server function:

rv <- reactiveValues(table = NULL)

observeEvent(input$goButtonb, {
  # Do some calculations...
  rv$table <- ...
})

observeEvent(input$goButtons, {
  # Do some calculations...
  rv$table <- ...
})

observeEvent(input$goButtona, {
  # Do some calculations...
  rv$table <- ...
})

output$inc <- renderTable({ rv$table })


--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/6b39fece-1e45-4957-b8af-fba29e0170d9%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Arun

unread,
Feb 26, 2016, 2:11:27 AM2/26/16
to Shiny - Web Framework for R
Thanks for the response Joe. It's working fine as per your recommendation. 

How will i be able to handle if i would like to store the output of each "go" button as separate one?

Appreciate your help.


On Thursday, February 25, 2016 at 11:57:12 AM UTC-8, Arun wrote:

Joe Cheng

unread,
Feb 26, 2016, 2:29:34 AM2/26/16
to Arun, Shiny - Web Framework for R
Use three distinct slots (like $table) in the rv object, one for each button. And if you still want to show them in the same table then one more slot to indicate which button was last pushed (NULL by default to indicate no button was pushed yet).
--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.

Ann

unread,
May 25, 2016, 3:39:16 PM5/25/16
to Shiny - Web Framework for R

I have a code with multiple reactive inputs and files,

I want my action button to reset to zero after being clicked once, so that I can have time to change multiple inputs and then click the action button again to start recalculating. 
Right now, the first time I run the app, it waits for the action button to be click to start calculating the output. But if I want to use the app again without refreshing it, it starts recalculating the moment I change an input value, when I would like it to wait before it starts recalculating, till I have clicked the action button again after changing all the values that I want to change. 

Appreciate your help on this!
Thanks! 

Joe Cheng

unread,
May 25, 2016, 3:56:40 PM5/25/16
to Ann, Shiny - Web Framework for R
"it starts recalculating the moment I change an input value"

This is indicative of a problem in the way your reactives and/or outputs are structured--either you're using reactive(...) when you should be using eventReactive(input$button, ...), or you're forgetting to wrap some logic in an output with isolate({ ... }). If you can provide a code sample of how you're using the button I can help you figure it out.

Ann

unread,
May 25, 2016, 4:22:02 PM5/25/16
to Shiny - Web Framework for R, annch...@gmail.com
It works now! 
I realized that it was because I wasn't using isolate().

Thanks so much! 
Reply all
Reply to author
Forward
0 new messages