Click to automatically execute the action button

library(shiny)
library(shinyjs)


ui <- basicPage(
  useShinyjs(),
  fluidRow(
    column(
      width = 6,
      textInput('a', 'Text A',"a1"),
      textInput('b', 'Text B',"b1"),
      # textInput('c', 'Text A',"c1"),
      # textInput('d', 'Text B',"d1"),
      # textInput('e', 'Text A',"e1"),
      textInput('f', 'Text B',"f1"),
      selectInput('gh', "select", choices = c(1,2,5,6,7), selected = 1),
      actionButton("sub", "Submit"),
      actionButton("ret", "auto click"),
      uiOutput('f2'),
      uiOutput('f3')
    ),
    column(
      width = 6,
      tags$p(tags$span(id = "valueA", "")),
      tags$script(
        "$(document).on('shiny:inputchanged', function(event) {
          if (event.name === 'a') {
            $('#valueA').text(event.value);
          }
        });
        "
      )
      ,tableOutput('show_inputs')
    )
  )
)

server <- shinyServer(function(input, output, session){
  
  # output$f2 <- renderUI({
  #   if(input$a == "a2"){
  #     textInput('ren', 'ren B',"ren z1")
  #   } else {
  #     NULL
  #   }
  # })
  
  
  
  AllInputs <- reactive({
    x <- reactiveValuesToList(input)
  })
  
  
  asd <- reactiveValues()
  
  observeEvent(input$sub,{
    asd$df <- c(6,9)
      output$f2 <- renderUI({
        selectInput('f2', "select", choices = c(6,9), selected = asd$df)
      })
      
      output$f3 <- renderUI({
        actionButton("new", "Generate iris table")
      })
  })
  
  observeEvent(input$new, {
    output$show_inputs <- renderTable({
      head(iris)
    })
  })

  observeEvent(input$ret,{
    asd$df <- 9 
    sad <- data.frame(a = c("gh"), b = c(7))
    updateSelectInput(session, inputId = sad$a, selected = sad$b)
    click('sub')
    updateSelectInput(session, inputId = "f2", selected = asd$df)
    click('new')   ### to generate IRIS table
    # click('new')
  })

})
shinyApp(ui = ui, server = server)

The process, when you click on "auto click", there is a button "Generate iris table" popping up and also this button should be get auto clicked so that iris table gets populated.

But here only "Generate iris table" button is popping up and no iris table getting displayed. But if you click again on "auto click" button , we can see iris button

Expected output The moment user clicks on "auto click", both "Generate iris table" button and iris table should be displayed

Hello,

You can get the behaviour by observing multiple events in your observeEvent function like this:

observeEvent(c(input$new, input$ret), {
    req(input$new + input$ret > 0)
    output$show_inputs <- renderTable({
      head(iris)
    })
  })

Note that I added req(input$new + input$ret > 0) to prevent the code from triggering when the buttons are first added to the form. Buttons have a value attached to them that counts how many times they have been clicked, thus this requirement says that at least one of them has to be clicked before the code triggers.

Hope this helps,
PJ

1 Like

Hi @vinayprakash808

This is also a behaviour of click that I observed but was not able to track / to reproduce / to understand.

So I decided to circumvent it by using soemthing like this

myclick <- function(id, session = shiny::getDefaultReactiveDomain()) {
  shinyjs::runjs(code = sprintf("Shiny.onInputChange('%s', %i)", id, ifelse(length(session$input[[id]])==0,0L,session$input[[id]]+1L)))
}

And then you can replace click by my myclick in your example

1 Like

Thanks a lot guys. Both are perfec. @gitdemont I see this is javascript right? You have any reference material to learn these?

This is more R than javascript.
Maybe for Javascript <-> R Shiny - How to send messages from the browser to the server and back using Shiny

For javascript maybe JavaScript | MDN
I read books in my language that may not be helpfull unfortunately

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.