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.
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)
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)