여러 xlsx 파일에서 데이터를 읽고 데이터 프레임으로 변환 한 후 데이터 프레임의 행 값을 기반으로 내 컴퓨터의 디렉터리에 폴더를 만드는 R 스크립트가 있습니다. 로,상대 출력 디렉터리를 R으로 설정
choose.dir(getwd(), "Choose a suitable folder")
그리고 폴더가 생성되는 출력 디렉토리 :
pth <- "C:/Users/Dev/Desktop/test/"
내가 실행하려면 나는 그런 내 코드의 시작 부분에 주요 디렉토리를 설정 한 여러 대의 컴퓨터에있는 스크립트로, pth이라는 출력 디렉토리가 항상 같은 것은 아닙니다. 상대 결과 출력 경로를 설정하는 방법이 있습니까? 스크립트를 실행할 때마다 내 결과를 원하는 위치를 지정하도록 할 수 있습니까? 나는 대화 창이 팝업 내가 원하는 디렉토리를 선택하지만, 내가 거기에 폴더 중 하나를 볼 수 없습니다
pth <- choose.dir(default = "", caption = "Choose the output path")
이 지금까지 내 코드입니다 : 당신이 할 수있는
#choose working directory
choose.dir(getwd(), "Choose a suitable folder")
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), ]
#print(df)
##pth <- "C:/Users/Dev/Desktop/test/"
pth = choose.dir(default = "", caption = "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]))
}
당신은 홈 디렉토리를 사용할 수에 해당합니다 (Sys.getenv'''을 통해 R에 의해 접근 HOME 환경 변수로 저장 ("HOME")''') '''pth <- "~/Desktop/test /"''' – denise
감사합니다. 그러나'Sys.getenv ("HOME")'은 C :를 리턴합니다. Users/Dev/Documents "_와 출력이 표시되지 않았습니다. 또한 다른 컴퓨터에서는 '~/Desktop/test /'가 아닌 다른 디렉토리에 출력 결과를 표시해야 할 수도 있습니다. 거기에 사용할 수있는보다 일반적인 문이 있기 때문에 누군가가 그것을 실행할 때마다 데이터를 출력 할 위치를 선택할 수 있습니까? – joasa
정확히 무엇을 개발하고 있는지 잘 모르겠지만 스크립트를 함수로 작성하고 로컬 디렉토리의 경로를 함수 입력으로 제공하고 각 시스템에서 함수를 "MyFunction ("path/to/로컬/디렉토리/")'''. 그것이 적절하지 않다면,'''LOCAL_DIRECTORY'''와는 다른 환경 변수를 설정하고 (또는 호출하고 싶을 수도 있습니다) 스크립트 내부의 홈 디렉토리와 같은 방식으로 접근 할 수 있습니다. 단점은 R 코드를 실행하기 전에 모든 컴퓨터에서이 환경 변수를 설정하십시오. – denise