2017-08-25 12 views
0

私が望むように、次のようなファイルを選択したいと思います。英語の文でR:特定の名前と最近更新されたファイルを選択してください。

which(substr(rownames(fInfo),1,8) == "mySource") & which.max(fInfo$mtime) 

、私は名前が「MYSOURCE」とし、選ばれた人たちの中に開始するファイルを選択したい、私は最近更新されたファイルを選択します。

以下のスクリプトで十分ですが、長すぎます。誰かが私のスクリプトを短縮することはできますか?

# create dummy files under Folder "scriptFld" 
ifelse(!dir.exists(file.path("scriptFld")), dir.create(file.path("scriptFld")), FALSE) 
strTime = format(Sys.time(), "%H%M") 
file.create(NA, paste0("scriptFld/mySource1_", strTime,".R")); Sys.sleep(1) 
file.create(NA, paste0("scriptFld/mySource2_", strTime,".R")); Sys.sleep(1) 
file.create(NA, paste0("scriptFld/notMySource3_", strTime,".R")) 


# read source R files 
setwd("scriptFld") 
fInfo = file.info(list.files()) # find all files under the folder "scriptFld" 
iCandidate = which(substr(rownames(fInfo),1,8) == "mySource") # focus on file names starting with "source" 
iCandidateMax = iCandidate[ which.max(fInfo$mtime[iCandidate]) ] # choose the most recent file 
fSourceName = rownames(fInfo)[iCandidateMax] 
source(file = fSourceName) # This is what I want, except the script is too long. 
setwd("..") 
(fSourceName) 
+2

どのようにX < 'について - list.files(パターン= "^ MYSOURCE");この前にwdを正しく設定していると仮定して、これは –

+0

です。これはまさに私が欲しかったものです。ありがとう! – stok

答えて

0

あなたは行うことができます。

library(dplyr) 
files <- list.files("scriptFld", pattern = "^mySource", full.names = TRUE) 
files %>% 
    file.info() %>% 
    pull(mtime) %>% 
    which.max() %>% 
    files[.] %>% 
    source() 
関連する問題