r - file.choose() customizing dialogue window

2020-02-09 08:21发布

问题:

Is there a way for the dialogue window that pops up after file.choose() is run to display a custom title, similar to X <- menu(files, graphics=TRUE, title="Choose file X")?

Right now my code requires several files be loaded.

X <- read.csv(file.choose())
Y <- read.csv(file.choose())
Z <- read.csv(file.choose())

At the moment I'm just using my (human) memory to know which files to choose for the first window, the second window, and the third window, but I'd like the window to show which object X Y or Z the current window's file will be imported to. I can move the window aside to see which line of code the console is up to, but that seems pretty inelegant.

I've tried X <- read.csv(file.choose(new=c("Choose X"))) for example but that doesn't seem to do anything.

回答1:

An alternative:

library(tcltk)
X <- read.csv(tk_choose.files(caption = "Choose X"))

See that the function can also be used to select multiple files in one call. For that, hold CTRL when selecting more than one file:

XYZ.list <- lapply(tk_choose.files(caption = "Choose X, Y, and Z"), read.csv)

but the selection order is not preserved so you might want to keep three separate calls if that works better for you.



回答2:

On Windows, you can use choose.files, which allows for custom title and also default file name (default), file type filtering (filters) and multifile selection (multi):

choose.files(default = "", caption = "Select files",
             multi = TRUE, filters = Filters,
             index = nrow(Filters))

check the help ?choose.files ;)



标签: r import