lapply(tk_choose.files)自定义文件选择window

lapply(tk_choose.files) customize file selection window

我想创建一个对话框 window 将弹出,用户将能够选择多个 xlsx 文件和 return 数据列表。我发现一个可能的解决方案是使用 tk_choose.files,但它给了我一个错误。

choose.dir(getwd(), "Choose a suitable folder")

library(xlsx)
library(rJava)
library(xlsxjars)
library(tcltk)

#get file names
f = list.files("./")

#read files
dat = lapply(f, function(i){
  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)
  #return columns with names and colors
  x = x[, c(2, 5, 7, 9, 11, 13, 15, 17, 19), drop=FALSE]
  #return the data
  x
})

Error: unexpected ',' in: "dat = lapply(f, function(i){ x = tk_choose.files(caption="Choose your files"),"

我知道语法中有错误,但我是 R 的新手,不知道如何正确编写它。

有人可以帮忙吗?

你可以尝试一下

#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
})

这样,用户在 lapply 调用中单步执行文件名向量之前选择文件。

你得到这个错误是因为你在一行中有两个语句,用逗号分隔。您的代码:

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)

第一个语句是

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

将选定的文件名分配给变量 x。然后你有一个逗号和第二个语句

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

其结果根本没有存储