R中是否有一个函数/包允许我们选择输出文件夹的存储位置?我写了一个脚本,它读取了几个xlsx文件,然后将它们存储在我的计算机的文件夹中。但是我会在几台计算机上使用这个脚本,所以我不能使用
path <- "C:/Users/Dev/Desktop/test/"
因为它每次都会改变。我试过了
path <- choose.dir(default = "", "Choose the output path")
但我没有得到任何输出。
这是代码:
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
library(xlsx)
library(tcltk)
#get file names
f = list.files("./")
#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, Filters = Filters[c("xlsx")])
#return columns with names and colors
x <- x[, c(2, 5, 7, 9, 11, 13, 15, 17, 19), drop = FALSE]
#return the data
x
})
library(plyr)
df1 <- ldply(dat, data.frame) ## convert list into a dataframe
#remove NA's
complete.cases(df1)
x <- df1[complete.cases(df1),]
str(x)
#show specific rows
df1$X2 <- gsub("[[:punct:]]","-",df1$X2)
df <-df1[grepl("^[0-9]|^[a-zA-Z][0-9].*", df1$X2), ]
#output path
pth <- choose.dir(default = "", "Choose the output path")
# Iteratate within each row of df
for(i in 1:nrow(df)){
if (!file.exists(pth))
# Create 1st path
dir.create(paste0(pth , df$X2[i]), na.omit(df$X2[i]))
# Create 2nd and 3rd paths
dir.create(paste0(pth, df$X2[i], "/",df$X5[i]), na.omit(df$X2[i]))
dir.create(paste0(pth, df$X2[i], "/",df$X7[i]), na.omit(df$X2[i]))
dir.create(paste0(pth, df$X2[i], "/",df$X9[i]), na.omit(df$X2[i]))
dir.create(paste0(pth, df$X2[i], "/",df$X11[i]), na.omit(df$X2[i]))
dir.create(paste0(pth, df$X2[i], "/",df$X13[i]), na.omit(df$X2[i]))
dir.create(paste0(pth, df$X2[i], "/",df$X15[i]), na.omit(df$X2[i]))
dir.create(paste0(pth, df$X2[i], "/",df$X17[i]), na.omit(df$X2[i]))
dir.create(paste0(pth, df$X2[i], "/",df$X19[i]), na.omit(df$X2[i]))
}
&#13;