Interactive output directory r -
is there function/package in r allows choose output folders going stored? wrote script reads several xlsx files , store them folders in computer. use script in several computers, cannot use
path <- "c:/users/dev/desktop/test/"
since change every time. tried
path <- choose.dir(default = "", "choose output path")
but don't output.
this code:
setwd(dirname(rstudioapi::getactivedocumentcontext()$path)) library(xlsx) library(tcltk) #get file names f = list.files("./") #read files dat <- lapply(tk_choose.files(caption="choose 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 names , colors x <- x[, c(2, 5, 7, 9, 11, 13, 15, 17, 19), drop = false] #return data x }) library(plyr) df1 <- ldply(dat, data.frame) ## convert list 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 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 , 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])) }
Comments
Post a Comment