R闪亮-上次点击的按钮ID [英] R shiny - last clicked button id
问题描述
我有多个操作按钮,要在其上显示不同的选择输入,并且我想知道最后单击的按钮ID,该怎么办?当我使用
I have multiple action buttons, on which i want to show different select Inputs and I want to know last clicked button id, how can I do that? When I use
which(lapply(c(1:10), function(i) { input[[paste0("ActionButton", i)]]}) == TRUE)
它向我显示所有被单击的按钮,但是我想知道哪个按钮是最后一个,以便再次单击前一个按钮.我怎样才能做到这一点?我是个新手,不确定是否能理解所有反应性/隔离性问题,所以我会很乐意提供任何提示.
It shows me all button which were clicked, however I want to know which one was the last in order to enable click once again on previous buttons. How can I do that? I am new in shiny and not sure if understand all reactive/isolate issue so I would be greateful for any hints.
推荐答案
您可以通过添加JS来实现
You can do it by adding JS
类似的东西
$(document).on('click', '.needed', function () {
Shiny.onInputChange('last_btn',this.id);
});
示例(如果您想控制所有的btn,请将类needed
添加到btn)
Example ( add class needed
to btn if you want to control not all btn)
ui <- shinyUI(fluidPage(
titlePanel("Track last clicked Action button"),
tags$head(tags$script(HTML("$(document).on('click', '.needed', function () {
Shiny.onInputChange('last_btn',this.id);
});"))),
sidebarLayout(
sidebarPanel(
actionButton("first", "First",class="needed"),
actionButton("second", "Second",class="needed"),
actionButton("third", "Third",class="needed"),
actionButton("save", "save"),
selectInput("which_","which_",c("first","second","third"))
),
mainPanel(
textOutput("lastButtonCliked")
)
)
))
server <- shinyServer(function(input, output,session) {
observeEvent(input$save,{
updateSelectInput(session,"which_",selected = input$last_btn)
})
output$lastButtonCliked=renderText({input$last_btn})
})
# Run the application
shinyApp(ui = ui, server = server)
这篇关于R闪亮-上次点击的按钮ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!