Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

r - file.choose() customizing dialogue window

Tags:

import

r

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.

like image 655
Hugh Avatar asked Apr 26 '13 00:04

Hugh


People also ask

How to scope the file dialog to an R file?

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?

How to select a file interactively in R?

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.

What is default working directory in R studio?

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.

What is the difference between 'choose' and 'file' in the dialog?

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.


1 Answers

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.

like image 124
flodel Avatar answered Oct 19 '22 17:10

flodel