Search code examples
rshinydirectoryupdates

R shiny : How to capture change in a specific directory?


I have a shinyApp, where I would like to capture the change in a specific directory.

i.e : the user click on the shinyDirButton, creates a sub-directory in a specific directory. I would like to capture any change in this directory (creation, deletion). I tried to use reactiveValues but I didn't succeed

    library(shiny)

ui = fluidPage(sidebarLayout(
  sidebarPanel(
    class = "sidebar_upload",
    id = "form",
    
    
    tags$h1("1- Choose a folder"),
    shinyFiles::shinyDirButton(
      id = 'choose_directory',
      label = 'Choose a folder',
      title = 'Choose a folder',
      multiple = F
    ),
    br(),
    br(),
    br(),
    actionButton("button", "Update")
    
  ),
  
  mainPanel(uiOutput(outputId = "test"))
))



server <- function(input, output, session) {
  r_global <- reactiveValues()
  
  observe({
    r_global$volumes = c(home = 'C:/')
    r_global$dossier = list.dirs(r_global$volumes,
                                 recursive = F,
                                 full.names = F)
    
    
    
    shinyFiles::shinyDirChoose(
      input = input,
      id = 'choose_directory',
      roots = r_global$volumes,
      session = session
    )
  })
  
  observeEvent(input$button, {
    print(r_global$dossier)
  })
}

shinyApp(ui, server)

Solution

  • You have to replace your first observe by an observeEvent:

       library(shiny)
    
    ui = fluidPage(sidebarLayout(
      sidebarPanel(
        class = "sidebar_upload",
        id = "form",
        
        
        tags$h1("1- Choose a folder"),
        shinyFiles::shinyDirButton(
          id = 'choose_directory',
          label = 'Choose a folder',
          title = 'Choose a folder',
          multiple = F
        ),
        br(),
        br(),
        br(),
        actionButton("button", "Update")
        
      ),
      
      mainPanel(uiOutput(outputId = "test"))
    ))
    
    
    
    server <- function(input, output, session) {
      r_global <- reactiveValues()
    
    #############################  
    ### here add observeEvent ###
    #############################
      observeEvent(input$button, {
        r_global$volumes = c(home = '~/project/SHINY/wedding/PROJET/')
        r_global$dossier = list.dirs(r_global$volumes,
                                     recursive = F,
                                     full.names = F)
        
        
        
        shinyFiles::shinyDirChoose(
          input = input,
          id = 'choose_directory',
          roots = r_global$volumes,
          session = session
        )
      })
      
      observeEvent(input$button, {
        print(r_global$dossier)
      })
    }
    
    shinyApp(ui, server)