0

I have one selectizeInput and one fileInput in my shiny app.

I want to keep fileInput disabled until some value is not selected in selectizeInput.

Also I want a popup message to select a value every time user clicks on fileInput. How can I do that.

Mohammad
  • 1,549
  • 1
  • 15
  • 27
  • The `shinyjs` package has a show/hide feature. You can hide the file selector until your condition is satisfied. Take a look at https://github.com/daattali/advanced-shiny – Rohit Dec 03 '19 at 05:43
  • in the same package, you should look at the ```disable``` function, as used [here](https://stackoverflow.com/questions/19611254/r-shiny-disable-able-shinyui-elements) – bretauv Dec 03 '19 at 16:57

1 Answers1

0

As others have mentioned, the shinyjs package is useful here. You could use enable, disable, or toggleState.

library(shiny)
library(shinyjs)

ui = fluidPage(
    shinyjs::useShinyjs(),
    selectizeInput("selector", label="Choose 2:", multiple=TRUE,
                   choices=letters[1:5], selected=letters[1:5]),
    fileInput("file_inputer", label="upload file")#,
    # dataTableOutput('table') 
)

server = function(input, output) {
    observe({
        shinyjs::toggleState("file_inputer", length(input$selector) %in% 0:4)
    })

    observeEvent(input$file_inputer, {
        showModal(modalDialog(
           title="Do you want a header row?",
           selectInput("option_selector", label="Choose an option",
                       choices=c("option 1", "option 2", "option 3")),
           footer = tagList(actionButton("read_file", "Read File"),
                            modalButton("Cancel")
           )
        ))
    })

    observeEvent(input$read_file, {
        # do something with your option value
        removeModal()
    })

}

# Run the application 
shinyApp(ui = ui, server = server)
ichbinallen
  • 1,019
  • 12
  • 18
  • The button does not appear disabled even if it is. I think the problem is that the button does not have the id but the input does. – Jan Stanstrup Jan 21 '21 at 10:37
  • Seems you can achieve that by adding `runjs('$("#file_inputer").parents("span").addClass("disabled")')` with the appropriate condition. – Jan Stanstrup Jan 21 '21 at 11:09
  • @JanStanstrup can you clarify where this runjs() statement needs to go? – smartse Jul 10 '23 at 16:57
  • Don't worry - I figured it out - it's just after the disable(file_inputer) but as an annoyance, if your code is modularised, the runjs() needs an input specific to the module i.e. id-file-inputer – smartse Jul 10 '23 at 17:06