forked from liao961120/collabin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_rmd_pkg.R
34 lines (31 loc) · 973 Bytes
/
check_rmd_pkg.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
library(magrittr)
## Helper
str_match <- function(...) stringr::str_match(...)
map <- function(...) purrr::map(...)
rmd2chr <- function(rmd_path) {
rscpt_path <- knitr::purl(rmd_path)
raw <- readLines(rscpt_path, encoding = 'utf-8')
file.remove(rscpt_path)
return(raw)
}
check_len <- function(chr) {
if (length(chr) == 0) return('no-pkg-detected')
else return(chr)
}
## Find out used pkgs from 'library'
used_pkgs <- list.files(pattern = '[Rr]md$', recursive = T) %>%
map(rmd2chr) %>%
unlist() %>%
str_match('library\\((.+)\\)') %>% .[,2] %>%
unique() %>%
na.omit() %>%
check_len()
cur_pkgs <- c(rownames(installed.packages()), 'no-pkg-detected')
writeLines(used_pkgs[!(used_pkgs %in% cur_pkgs)], 'need_install.txt')
pkg_lst <- readLines('need_install.txt')
if (length(pkg_lst) != 0) {
cat('Installing packages detected in Rmd ...',
'If errored, check `need_install.txt` for GitHub dep.',
sep = '\n')
install.packages(pkg_lst)
}