Search code examples
rshinysubdirectory

Error of object of type 'closure' is not subsettable when choosing sub-folder from R shiny list of file paths


How can I revise the code to allow to navigate sub-folders in Users? I can't find a sub-folder that I need to use under Users with the code as below. When I click 'Users' to find a sub-folders or get selected, Warning: Error in [: object of type 'closure' is not subsettable [No stack trace available] shows up.

enter image description here

ui <- fluidPage(
  shinyDirButton("Btn_GetFolder", "Choose a folder" ,
                 title = "Please select a folder:",
                 buttonType = "default", class = NULL),
  
  textOutput("txt_file")
)


server <- function(input,output,session){
  
  volumes = getVolumes()
  observe({
    
    shinyDirChoose(input, "Btn_GetFolder", roots = volumes, session = 
                     session)
    if(!is.null(input$Btn_GetFolder)){
      # browser()
      myInputDir1 <- parseDirPath(volumes, input$Btn_GetFolder)
      listBands <- list.files(myInputDir1, full.names = T)
      output$txt_file <- renderText(listBands)
      
      #Call function here..... 
   
    }
  })
}

shinyApp(ui = ui, server = server)

Solution

  • The help of getVolumes states in the "Value" section:

    A function returning a named vector of available volumes

    Therefore, volume is a function and needs to be evaluated to return the available volumes. This also explains the error you get: just using volumes means that you actually provide a function (basically a closure), and later somewhere in shinyDirChoose it gets subsetted - which doesn't work.

    library(shiny)
    library(shinyFiles)
    ui <- fluidPage(
      shinyDirButton("Btn_GetFolder", "Choose a folder" ,
                     title = "Please select a folder:",
                     buttonType = "default", class = NULL),
      
      textOutput("txt_file")
    )
    
    
    server <- function(input,output,session){
      
      volumes = getVolumes()
    
      observe({
        
        shinyDirChoose(input, "Btn_GetFolder", roots = volumes(), session = 
                         session)
        if(!is.null(input$Btn_GetFolder)){
          # browser()
          myInputDir1 <- parseDirPath(volumes, input$Btn_GetFolder)
          listBands <- list.files(myInputDir1, full.names = T)
          output$txt_file <- renderText(listBands)
          
          #Call function here..... 
          
        }
      })
    }
    
    shinyApp(ui = ui, server = server)