Search code examples
rshinymodularization

actionButton in server side modularized code does not work


I am using a modalDialog pop up to allow an issue to be recorded in the github api. However, as the actionButton("ok", "Submit new issue") is in the modularized server side code, I believe due to it not being name spaced the button does not work. I have provided a test code below which should show the title when the button is pressed but it doesn't do anything.

Is there any way around it?

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)  

Solution

  • I see what you mean now, when you are working with modules and creating objects inside the modules, you need to assign the namespace to them e.g. session$ns("ok"), then you can access them like so: session$input$ok, similar for the title input you have, the code below should work for you...

    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)