R闪亮 - 最后点击按钮内部闪亮的模块功能

问题描述 投票:1回答:1

我试图从Shiny模块内部访问最后点击的复选框或按钮ID。

我发现这篇文章的回复非常有用:R shiny - last clicked button 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)

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

r module shiny shinyjs shinymodules
1个回答
1
投票

你几乎就在那里,但有一些小的格式问题。更新的代码如下所示。

你主要用逗号分隔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)
© www.soinside.com 2019 - 2024. All rights reserved.