2

I need to display selected folder for Shiny users, and I asked here how to do this (Display selected folder path in Shiny). It works but now I can't figure out how to show the default folder (for example, current directory) before the selection was made.

library(shiny)
library(shinyFiles)

ui <- fluidPage( # Application title
  mainPanel(
    shinyDirButton("dir", "Input directory", "Upload"),
    verbatimTextOutput("dir", placeholder = TRUE)  
  ))

server <- function(input, output) {
  shinyDirChoose(
    input,
    'dir',
    roots = c(home = '~'),
    filetypes = c('', 'txt', 'bigWig', "tsv", "csv", "bw")
  )

  dir <- reactive(input$dir)
  output$dir <- renderText({  
    parseDirPath(c(home = '~'), dir())
  })
## change smth here... if output$dir is null, display getwd() but it doesn't work

  observeEvent(ignoreNULL = TRUE,
               eventExpr = {
                 input$dir
               },
               handlerExpr = {
                 home <- normalizePath("~")
                 datapath <<-
                   file.path(home, paste(unlist(dir()$path[-1]), collapse = .Platform$file.sep))
               })
}

# Run the application
shinyApp(ui = ui, server = server)

I can think only of a conditional panel displaying some text if the folder wasn't selected. But I guess there should a better way to do this. Thank you!

kintany
  • 531
  • 1
  • 6
  • 15

1 Answers1

3

Concerning "beyond" displaying you could save the datapath variable in a reactiveValue and set the working directory as the default:

global <- reactiveValues(datapath = getwd())

And the app:

library(shiny)
library(shinyFiles)

ui <- fluidPage( # Application title
  mainPanel(
    shinyDirButton("dir", "Input directory", "Upload"),
    verbatimTextOutput("dir", placeholder = TRUE)  
  ))

server <- function(input, output) {
  shinyDirChoose(
    input,
    'dir',
    roots = c(home = '~'),
    filetypes = c('', 'txt', 'bigWig', "tsv", "csv", "bw")
  )

  global <- reactiveValues(datapath = getwd())

  dir <- reactive(input$dir)

  output$dir <- renderText({
      global$datapath
  })

  observeEvent(ignoreNULL = TRUE,
               eventExpr = {
                 input$dir
               },
               handlerExpr = {
                 home <- normalizePath("~")
                 global$datapath <-
                   file.path(home, paste(unlist(dir()$path[-1]), collapse = .Platform$file.sep))
               })
}

# Run the application
shinyApp(ui = ui, server = server)
Tonio Liebrand
  • 17,189
  • 4
  • 39
  • 59
  • Thank you! It's actually not just for displaying, I meant it's default path and user can change it. Ok, I'll go read about reactiveValues(), thank you! – kintany Dec 30 '17 at 16:42
  • 2
    I know it's been a while but maybe you know the answer... This code stopped working with the recent shinyFiles update, do you know anything that now has to be fixed? Thank you!! – kintany Nov 15 '18 at 19:12
  • i think you already found an answer in your follow up, question, correct? – Tonio Liebrand Nov 15 '18 at 21:56
  • I did but the solution somehow doesn't work with Docker implementation, I now can't see the directories mounted with the docker container... – kintany Nov 15 '18 at 22:21