R - File.Choose() Customizing Dialogue Window

r - file.choose() customizing dialogue window

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.

R command-line file dialog? similar to file.choose

I have kind of what you want made that I keep in my .Rprofile. It has a menu interface as it's default for looking through the working directory. If you want it extended to start with he root directory and work out with menus from there you'd have to do a lot of modifying of the function.

The function finds only .txt .R and .Rnw files in the menu.

Open <- function(method = menu) {
wd<-getwd()
on.exit(setwd(wd))

x <- dir()
x2 <- subset(x, substring(x, nchar(x) - 1, nchar(x)) == ".R" |
substring(x, nchar(x) - 3, nchar(x)) %in%c(".txt", ".Rnw"))

if (is.numeric(method)) {
x4 <- x2[method]
x5 <- as.character(x4)
file.edit(x5)
} else {
switch(method,
menu = { x3 <- menu(x2)
x4 <- x2[x3]
x5 <- as.character(x4)
file.edit(x5)
},
look = file.edit(file.choose()))
}
}

##########
#Examples
#########
Open()
Open("L")

File.choose for multiple files R

One option is to install the tcltk library and use tk_choose.files

library(tcltk)
flist <- tk_choose.files()

## Note: to choose multiple files that are not adjacent in the dialog,
## you may have to hold down the "control" key or some other key.

There are a number of options for this and it will pop up an x-windows dialog box (you have to have x-windows installed, which would be something like XQuartz on Mac OS X).

EDIT

Note, this is not quite the same question as this post but the answer is pretty much the same.

lapply(tk_choose.files) customize file selection window

You can try something along

#read files
dat <- lapply(tk_choose.files(caption="Choose your files"), function(i) {
x <- read.xlsx(i, sheetIndex = 1, sheetName = NULL, startRow = 24,
endRow = NULL, as.data.frame = TRUE, header = FALSE)
#return columns with names and colors
x <- x[, c(2, 5, 7, 9, 11, 13, 15, 17, 19), drop = FALSE]
#return the data
x
})

By this, the user selects a the files before stepping through the vector of file names in the lapply call.

You got the error because you have two statements on one line separated by a comma. Your code:

x = tk_choose.files(caption="Choose your files"), read.xlsx(i, sheetIndex = 1, sheetName = NULL, startRow = 24,
endRow = NULL, as.data.frame = TRUE, header = FALSE)

The first statement is

x = tk_choose.files(caption="Choose your files")

which assigns the chosen file names(s) to variable x. Then you have a comma and a second statement

read.xlsx(i, sheetIndex = 1, sheetName = NULL, startRow = 24,
endRow = NULL, as.data.frame = TRUE, header = FALSE)

whose result is not stored at all.

Why doesn't choose.files() open in the current working directory?

Try adding this mask to the default argument:

choose.files(default=paste0(getwd(), "/*.*")) 

From the help for ?choose.files:

If you would like to display files in a particular directory, give a
fully qualified file mask (e.g., "c:\*.*") in the default argument.



Related Topics



Leave a reply



Submit