简体   繁体   中英

R shiny - last clicked button id inside shiny module function

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM