R 选择一个具有特定名称和最近更新的文件
我想选择一个像下面这行一样的文件,但它并没有按照我的意愿执行R 选择一个具有特定名称和最近更新的文件,r,R,我想选择一个像下面这行一样的文件,但它并没有按照我的意愿执行 which(substr(rownames(fInfo),1,8) == "mySource" ) & which.max(fInfo$mtime) 在一个英语句子中,我想选择名称以“mySource”开头的文件,在选择的文件中,我想选择最近更新的文件 我下面的脚本已经足够了,但是太长了。有人能缩短我的脚本吗 # create dummy files under Folder "scriptFld" ifelse(!dir.
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)
你可以做:
library(dplyr)
files <- list.files("scriptFld", pattern = "^mySource", full.names = TRUE)
files %>%
file.info() %>%
pull(mtime) %>%
which.max() %>%
files[.] %>%
source()
库(dplyr)
文件%
file.info()%%>%
拉动(mtime)%>%
哪个.max()%>%
文件[]%>%
资料来源()
x这正是我想要的。谢谢