Help with conditionalPanel based on fileInput

I'm working on a Shiny app to process files. If the chosen file is an R file, I need to ask an additional question, so I am attempting to include a conditionalPanel.

I'm still a bit of a noob in shiny, but with quite a bit of googling, I'm still not able to get this to work.

The selectInput("Rver",... panel is showing up no matter what (before I've even chosen the file.

(Interestingly, the second conditionalPanel doesn't work correctly in the script below, but DOES work fine if I comment out the first conditionalPanel -- the one I'm having trouble with. So I /think/ that one is set up correctly, but please let me know if that's not the case.)

Anyway, here is what I have. Any help is greatly appreciated.

ui <- fluidPage(
  titlePanel("Generate .bat file"),
  div(HTML("<br>")),
  fileInput("script",
            "Select R or py script to automate",
            accept = c(".r", ".R", ".py", "PY", "Py")),
  conditionalPanel(condition = "input.script.name.indexOf(\".R\") != -1",
                   selectInput("Rver",
                               "Select R version",
                               choices = Rvers)),
  checkboxInput("box", "Does this script connect to Box?"),
  conditionalPanel(condition = "input.box == 1",
                   fileInput("boxToken",
                             "Select Box token to use",
                             accept = c(".json", "JSON", "Json"))),
  actionButton("go", "Generate .bat file"),
  div(HTML("<hr>")),
  uiOutput("template")
)

server <- function(input, output){
  observeEvent(input$go, {
    output$template <- renderText ({
      print(input$template$name)
    }) #END output$template
  }) #END observeEvent
}

shinyApp(ui = ui, server = server)

ps I do realize if this worked, it would only look for ".R" (but not ".r"). I'm trying to keep it as simple as I can for now.

Thanks! Luke

I couldn't get it to work with conditionalPanel, but this code, which uses insertUI and removeUI, should produce the behavior you're looking for. Note that I defined Rvers arbitrarily because it was missing in your snippet.

Rvers <- c("1.5", "4.0.3", "1.2.8")

ui <- fluidPage(
    titlePanel("Generate .bat file"),
    div(HTML("<br>")),
    fileInput("script",
              "Select R or py script to automate",
              accept = c(".r", ".R", ".py", "PY", "Py")),
    checkboxInput("box", "Does this script connect to Box?"),
    actionButton("go", "Generate .bat file"),
    div(HTML("<hr>")),
    uiOutput("template")
)

server <- function(input, output){
    
    observe({

        if (!is.null(input$script) && grepl(".R", input$script$name)) {
            
            insertUI(
                selector = "#box",
                where = "beforeBegin",
                ui = selectInput("Rver","Select R version", choices = Rvers)
            )
        } else {
            removeUI(selector = "div:has(> #Rver-label)")
        }
    })
    
    observe({
        
        if(input$box == TRUE) {
            
            insertUI(
                selector = "#go",
                where = "beforeBegin",
                ui = fileInput("boxToken", "Select Box token to use",
                               accept = c(".json", "JSON", "Json"))
            )
           
        } else {
            removeUI(selector = "div:has(> #boxToken-label)")
        }

    })
    
    observeEvent(input$go, {
        output$template <- renderText ({
            print(input$template$name)
        }) #END output$template
    }) #END observeEvent
}

shinyApp(ui = ui, server = server)
1 Like

This worked wonderfully. Thank you. Like I said, I'm just learning SHiny so I didn't even know about the existance of insertUI. Very clear and helpful answer. Thank you.

Luke

1 Like

Great, I'm glad to hear that! As you learn more about R Shiny, as an alternative approach, the package shinyjs may be of interest to you. It can be used to hide() and show() or toggle() visibility of UI elements. From my understanding, that would technically be more efficient compared to generating/removing new elements everytime with insertUI() and removeUI(). See more details at Chapter 15 Optimize your apps with custom handlers | Outstanding User Interfaces with Shiny

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.