服务器端模块化代码中的 actionButton 不起作用

actionButton in server side modularized code does not work

我正在使用 modalDialog 弹出窗口允许在 github api 中记录问题。但是,由于 actionButton("ok", "Submit new issue") 在模块化的服务器端代码中,我相信由于它没有命名空间,所以按钮不起作用。我在下面提供了一个测试代码,它应该在按下按钮时显示标题,但它什么也没做。

有什么解决办法吗?

library(shiny)

editTableUI <- function(id){
  ns<-NS(id)
  tagList(
    actionButton(ns("add_issue"), "New Issue"),
    textOutput("text"))
  }
  
editTable <-function(input, output, session){
  
  observeEvent(input$add_issue, {
    loginModal <- function() {
      modalDialog(
        title = "Create Issue",
        textInput("title", "Title"),
        textAreaInput("body", "Body", placeholder = "Leave a comment", width = '100%', height = '300px' ),
        selectInput("asignee",label= "Assignees", selected = NULL, choices = c("a","b","c","d"), multiple = T ),
        footer = tagList(
          modalButton("Cancel"),
          actionButton("ok", "Submit new issue")),
        fade = T,
        size = c("s")
        )
    }
    showModal(loginModal())
  })
  
  observeEvent(input$ok, {
  output$text <- renderText({ input$title })})
  
}
  

ui <- fluidPage(
  editTableUI("tab2"))
  
server <- function(input, output, session) {
  callModule(editTable, "tab2")}
  
shinyApp(ui, server)  

我现在明白你的意思了,当你使用模块并在模块内创建对象时,你需要为它们分配命名空间,例如session$ns("ok"),然后您可以像这样访问它们:session$input$ok,类似于您拥有的 title 输入,下面的代码应该适合您...

library(shiny)

editTableUI <- function(id){
    ns <- NS(id)
    tagList(
        actionButton(ns("add_issue"), "New Issue"),
        textOutput(ns("text"))
    )
}

editTable <-function(input, output, session){
    
    observeEvent(input$add_issue, {
        loginModal <- function() {
            modalDialog(
                title = "Create Issue",
                textInput(session$ns("title"), "Title"),
                textAreaInput("body", "Body", placeholder = "Leave a comment", width = '100%', height = '300px' ),
                selectInput("asignee",label= "Assignees", selected = NULL, choices = c("a","b","c","d"), multiple = T ),
                footer = tagList(
                    modalButton("Cancel"),
                    actionButton(session$ns("ok"), "Submit new issue")),
                fade = T,
                size = c("s")
            )
        }
        showModal(loginModal())
    })
    
    observeEvent(session$input$ok, {
        output$text <- renderText({ 
            input$title 
        })
    })
    
}


ui <- fluidPage(
    editTableUI("tab2"))

server <- function(input, output, session) {
    callModule(editTable, "tab2")
    
}

shinyApp(ui, server)