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.
For example, to scope the dialog to R files, one could use R Files (*.R) here. Boolean; should the file dialog limit itself to existing files on the filesystem, or allow the user to select the path to a new file?
Here’s how to do it. The following R code shows how to apply the file.choose function to choose a file interactively. After executing the previous R code, the following window appears: In this window, we can interactively select a file. Let’s choose the Microsoft Excel file called “my file” in this directory.
Default working directory — Startup directory for RStudio (when not in a project). The initial .RData and .Rprofile files (if any) will be read from this directory. The current working directory and Files pane will also be set to this directory.
A character vector giving zero or more file paths. Unlike file.choose, choose.files will always attempt to return a character vector giving a list of files. If the user cancels the dialog, then zero files are returned, whereas file.choose would signal an error. choose.dir chooses a directory.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With