繁体   English   中英

R 闪亮 - 最后点击的按钮 ID 在闪亮的模块功能中

[英]R shiny - last clicked button id inside shiny module function

我正在尝试从 Shiny 模块内部访问上次单击的复选框或按钮 ID。

我发现这篇文章的精彩回复很有帮助: R 闪亮 - 最后点击的按钮 ID并已将代码调整为我的问题。 我也从这篇文章中得到了一个提示: https : //github.com/datastorm-open/visNetwork/issues/241但仍然无法让它工作。

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)

我希望 input$last_btn 值(最后单击的按钮的 id 名称)在应用程序底部创建并返回,并成为选择中的更新输入。 但是,没有创建 input$last_btn,我也使用调试浏览器()进行了检查。

你快到了,但有一些小的格式问题。 下面列出了更新的代码。

您主要以逗号分隔ns("last_btn")的方式误用了HTML (我将其更改为JS但这不是问题ns("last_btn") 如果您检查过原始HTML(...)语句的输出,您会看到结果 JavaScript 字符串是

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

我的意思是输入 id 周围的空格。 由于多余的空格,输入未正确映射到input$last_btn 我在我的例子中使用了paste0来正确地将字符串粘合在一起。

其次,我更正了一些丢失的ns调用,但是一旦输入阻止程序消失,您肯定会发现自己。

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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