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)
怎麼樣'X < - list.files(模式= 「^ MYSOURCE」); source(x [which.max(file.info(x)$ mtime)])'假設你在此之前正確設置了wd –
這正是我想要的。謝謝! – stok