LivingstoneM
LivingstoneM

Reputation: 1088

Use user input to select sub directory

I have a directory with multiple sub-directories within them and I did like to list the sub-directories then let the user choose the sub-directory he wants to read files within it.

I want the use to use the index to input his selection before the code continues to read files inside. Here is what I tried to list all sub-directories within the parent directory but I am stuck at how to use user raw input to select a sub-directory before reading the files within:

path = setwd("path to parent directory")

sub_dirs <- list.dirs(path, full.names = TRUE, recursive = TRUE)

#User raw input to be used here before listing files within
.....

#list all files
files <- dir(path, pattern = '\\.csv', full.names = FALSE)

#read files
tables <- lapply(files, read.csv)

Expected output is a code where I can list sub-directories within the parent directory then allow a user to input an index of the sub-directory he wants to read files inside it. then after user input then code proceeds to list csv files inside then read the files.

Upvotes: 3

Views: 609

Answers (2)

LMc
LMc

Reputation: 18642

tcltk is a base R package. tk_choose.dir will open a modal dialog window for a user to select a folder:

tcltk::tk_choose.dir(default = "~/")

Upvotes: 0

Mikko Marttila
Mikko Marttila

Reputation: 11878

You can prompt the user to select a subdirectory with menu(choices = sub_dirs).

Upvotes: 3

Related Questions