Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

R shiny: How to use removeUI in shiny module to hide action button

I want to hide an action button once it is clicked and when the input is not empty.

Similar question has been asked here, and the provided solutions works very well.

However, I don’t know how to make it work in a shiny module.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Here is my example code:

# module UI
choice_ui <- function(id) {
  ns <- NS(id)
  tagList(
    textInput(inputId = ns("myInput"), label = "write something"),
    actionButton(inputId = ns("click"), label = "click me"),
    verbatimTextOutput(outputId = ns("text"))
    )
  }

# module server
choice_server <- function(id) {
  moduleServer(id, function(input, output, session){
    observeEvent(input$click, {
      x <- input$myInput
      output$text <- renderPrint({x})
      req(x)
      removeUI(selector = paste0("#click", id), session = session)
      })
    })
  }

# Application
library(shiny)
app_ui <- function() {
  fluidPage(
    choice_ui("choice_ui_1")
  )
}

app_server <- function(input, output, session) {
  choice_server("choice_ui_1")
}

shinyApp(app_ui, app_server)

>Solution :

On the module server, get the session namespace:

ns <- session$ns

Use it in removeUI() as follows:

removeUI(selector = paste0("#", ns("click")), session = session)

Here’s a complete reprex:

# module UI
choice_ui <- function(id) {
  ns <- NS(id)
  tagList(
    textInput(inputId = ns("myInput"), label = "write something"),
    actionButton(inputId = ns("click"), label = "click me"),
    verbatimTextOutput(outputId = ns("text"))
  )
}

# module server
choice_server <- function(id) {
  moduleServer(id, function(input, output, session) {
    ns <- session$ns
    observeEvent(input$click, {
      x <- input$myInput
      output$text <- renderPrint({
        x
      })
      req(x)
      removeUI(selector = paste0("#", ns("click")), session = session)
    })
  })
}

# Application
library(shiny)
app_ui <- function() {
  fluidPage(
    choice_ui("choice_ui_1")
  )
}

app_server <- function(input, output, session) {
  choice_server("choice_ui_1")
}

shinyApp(app_ui, app_server)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading