Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading