rshinygolem

How to disable button when ns function is used as identifier


I would like to know how to disable the download button until there is something in the textInput and turn it off when the textInput contains something. With the current code, the button remains active. I think the problem comes from the id of the downloadData button, I tried with a defaut shiny app code and without using the ns() function, it works.

mod_test.R

mod_test_ui <- function(id){
  ns <- NS(id)
  tagList(shinyjs::useShinyjs(),
  textInput(inputId = ns("text"), label = "Entrez une url"),
  downloadButton(ns("downloadData"), "download"))}

app_server.R

app_server <- function(input, output, session) {
  observe({
    if (!is.null(input$text) && input$text!= "") {
      print("enable"); shinyjs::enable(input$downloadData)
     } else { print("disable"); shinyjs::disable(input$downloadData)}})}

app_ui.R

app_ui <- function(request) {
  tagList( golem_add_external_resources(), fluidPage(h1("title"),
      mod_test_ui("test_1")))
}

Solution

  • You need to specify the id downloadData. Also, you need a moduleServer. Try this

    mod_test_ui <- function(id){
      ns <- NS(id)
      tagList(shinyjs::useShinyjs(),
            textInput(inputId = ns("text"), label = "Entrez une url"),
            downloadButton(ns("downloadData"), "download"))
    }
    
    mod_test_server <- function(id){
      moduleServer( id, function(input, output, session){
        ns <- session$ns
      
        observe({
          print(input$text)
          if (!is.null(input$text) && input$text!= "") {
            print("enable"); shinyjs::enable("downloadData")
          } else { print("disable"); shinyjs::disable("downloadData")}
        })
        
      })
    }
    
    ui <- fluidPage(
      tagList(         
        # golem_add_external_resources(), 
        h1("title"),
        mod_test_ui("test_1"))
    )
    
    server <- function(input, output, session) {
      mod_test_server("test_1")
    }
    
    shinyApp(ui, server)