-
Notifications
You must be signed in to change notification settings - Fork 0
/
getMetadata.R
49 lines (39 loc) · 1.36 KB
/
getMetadata.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# Get metadta for .md files in a single directory
# Input: the directory path
# Output: a data.table with filename and metadata.
# Currently, adding only ms.author and ms.reviewer
getMetadata <- function(path){
library(data.table)
# get all the markdown files in the directory
files <- list.files(path=path, pattern=".md$", all.files=FALSE,
full.names=FALSE)
metadata <- data.table()
# loop through files to get metadata and append to data.table
for (i in 1:length(files)) {
fn <- paste(myrepo, files[i], sep="/") # get the filename
tryCatch(
# Specifying expression
expr = {
yml_metadata <- rmarkdown::yaml_front_matter(fn) # read the metadata for this file
},
# Specifying error message
error = function(e){
print(fn)
print(e)
},
warning = function(w){
print(fn)
print(w)
},
finally = {
# I'm appending the ms.author and ms.reviewer here - customize as you see fit!
newrow <- data.table(filename =files[i],
ms.author=yml_metadata[["ms.author"]],
ms.reviewer=yml_metadata[["ms.reviewer"]],
ms.custom=yml_metadata[["ms.custom"]])
metadata <- rbind(metadata, newrow, fill=T)
}
)
}
return (metadata)
}