3

I am trying to access the last clicked checkbox or button id from inside a Shiny module.

I have found the great response from this post helpful: R shiny - last clicked button id and have adapted the code to my question. I also took a hint from this post: https://github.com/datastorm-open/visNetwork/issues/241 but still can't get it working.

user_inputUI <- function(id){
    # Create a namespace function using the provided id
    ns <- NS(id)

    tagList(
        tags$head(tags$script(HTML("$(document).on('click', '.needed', function () {
                               Shiny.onInputChange('", ns("last_btn"), "', this.id);
});"))),
        tags$span(HTML('<div><input id="first" type="checkbox" class="needed"></div>')),
        actionButton(ns("second"), "Second",class="needed"),
        actionButton(ns("third"), "Third",class="needed"),
        actionButton(ns("save"), "save"),
        selectInput(ns("which_"),"which_",c("first","second","third"))
    )
}

update_options <- function(input, output, session) {

    observeEvent(input$save,{

        updateSelectInput(session,"which_",selected = input$last_btn)
    })

    return(reactive(input$last_btn))
}

ui <- shinyUI(fluidPage(

    titlePanel("Track last clicked Action button"),


    sidebarLayout(
        sidebarPanel(
            user_inputUI("link_id")
        ),

        mainPanel(

            textOutput("lastButtonCliked")
        )
    )
    ))


server <- shinyServer(function(input, output,session) {

    last_id <- callModule(update_options, "link_id")
    output$lastButtonCliked=renderText({last_id()})

})


# Run the application 
shinyApp(ui = ui, server = server)

I would expect the input$last_btn value (the id name of the last button clicked) to be created and returned at the bottom of the app as well as becoming the updated input in the selectize. However, the input$last_btn is not being created, I have checked this using the debugging browser() as well.

amycookie
  • 43
  • 6

1 Answers1

4

You were almost there, but there were some minor formatting issues. The updated code is listed below.

You mainly misused HTML (I changed it to JS but thats not the problem) in the way that you just comma separated the ns("last_btn"). If you had inspected the output of your original HTML(...) statement, you would have seen that the resulting JavaScript string was

Shiny.onInputChange(' link_id-last_btn ', this.id);

And I mean the spaces around the input id. Because of the extra spaces, the input was not properly mapped on input$last_btn. I used paste0 in my example to correctly glue the strings together.

Second, there are some missing ns calls which I corrected, but that you would for sure have found out yourself once the input blocker was gone.

user_inputUI <- function(id){
  # Create a namespace function using the provided id
  ns <- NS(id)

  tagList(
    tags$head(
      tags$script(
        JS(paste0("$(document).on('click', '.needed', function () {debugger;
           Shiny.onInputChange('", ns("last_btn"), "', this.id);
        });"))
      )
    ),
    tags$span(HTML(paste0('<div><input id="', ns("first"), '" type="checkbox" class="needed"></div>'))),
    actionButton(ns("second"), "Second", class="needed"),
    actionButton(ns("third"), "Third", class="needed"),
    actionButton(ns("save"), "save"),
    selectInput(ns("which_"), "which_", c(ns("first"), ns("second"), ns("third")))
    )
}

update_options <- function(input, output, session) {

  observeEvent(input$last_btn, {
    print(input$last_btn)
  })

  observeEvent(input$save, {
    updateSelectInput(session, "which_", selected = input$last_btn)
  })

  return(reactive(input$last_btn))
}

ui <- shinyUI(fluidPage(

  titlePanel("Track last clicked Action button"),


  sidebarLayout(
    sidebarPanel(
      user_inputUI("link_id")
    ),

    mainPanel(

      textOutput("lastButtonCliked")
    )
  )
))


server <- shinyServer(function(input, output,session) {

  last_id <- callModule(update_options, "link_id")
  output$lastButtonCliked=renderText({last_id()})

})


# Run the application 
shinyApp(ui = ui, server = server)
K. Rohde
  • 9,439
  • 1
  • 31
  • 51
  • thank you so much! That opens up a suite of possibilities for my code now. You said 'if you had inspected the output of your original HTML' in your answer. How do you inspect HTML output in a shiny app with the ns()? I am pretty new to HTML. If it's too hard to explain don't worry about it. Thanks again : ) – amycookie Mar 31 '19 at 12:53
  • Just replace `tags$script` with `tags$span` and put it anywhere in the document. Or just `print` it from within your code. – K. Rohde Mar 31 '19 at 13:04
  • @K.Rohde could you please help with this https://stackoverflow.com/questions/59431981/how-to-create-two-independent-drill-down-plot-using-highcharter – John Smith Dec 21 '19 at 12:44