@@ -31,38 +31,42 @@ detect_dependencies <- function(file_path) {
3131 tolower(
3232 regmatches(
3333 file_path ,
34- regexpr(" \\ .[A-Za-z0-9]{1,3}$" , file_path )))
34+ regexpr(" \\ .[A-Za-z0-9]{1,3}$" , file_path )
35+ )
36+ )
3537
3638 if (! (file_type %in% c(" .r" , " .rmd" ))) stop(" detect_dependencies only supported for .R and .Rmd" )
3739
3840 deps <- switch (file_type ,
39- .r = parse_detect_deps(file_path , parse ),
40- .rmd = parse_detect_deps(file_path , parse_rmd ),
41- NULL )
41+ .r = parse_detect_deps(file_path , parse ),
42+ .rmd = parse_detect_deps(file_path , parse_rmd ),
43+ NULL
44+ )
4245
4346 deps [! duplicated(deps ), ]
4447}
4548
4649parse_detect_deps <- function (file_path , file_parser ) {
47-
4850 syntax_tree <- tryCatch(file_parser(file = file_path ),
49- error = function (e ) stop(" Could not detect usage of using::pkg in due to invalid R code. The parser returned: \n " , e $ message ))
51+ error = function (e ) stop(" Could not detect usage of using::pkg in due to invalid R code. The parser returned: \n " , e $ message )
52+ )
5053
5154 get_using(syntax_tree )
5255}
5356
5457parse_rmd <- function (file_path ) {
55-
56- R_temp <- tempfile(fileext = " .R" )
58+ R_temp <- tempfile(fileext = " .R" )
5759 on.exit(unlink(R_temp ))
5860
59- withr :: with_options(list (knitr.purl.inline = TRUE ),
60- knitr :: purl(file_path ,
61- output = R_temp ,
62- quiet = TRUE ))
63-
64- parse(file = R_temp )
61+ withr :: with_options(
62+ list (knitr.purl.inline = TRUE ),
63+ knitr :: purl(file_path ,
64+ output = R_temp ,
65+ quiet = TRUE
66+ )
67+ )
6568
69+ parse(file = R_temp )
6670}
6771
6872
@@ -79,46 +83,53 @@ is_using_node <- function(ast_node) {
7983extract_using_data <- function (ast_node ) {
8084 node_list <- as.list(ast_node )
8185 node_list <- node_list [- 1 ]
82- char_nodes <- stats :: setNames(lapply(node_list , as.character ),
83- names(node_list ))
86+ char_nodes <- stats :: setNames(
87+ lapply(node_list , as.character ),
88+ names(node_list )
89+ )
8490
8591
8692 do.call(
8793 function (package = NA_character_ ,
8894 min_version = NA_character_ ,
8995 repo = NA_character_ ) {
90- data.frame (package = package ,
91- min_version = min_version ,
92- repo = repo ,
93- stringsAsFactors = FALSE )
96+ data.frame (
97+ package = package ,
98+ min_version = min_version ,
99+ repo = repo ,
100+ stringsAsFactors = FALSE
101+ )
94102 },
95- char_nodes )
96-
103+ char_nodes
104+ )
97105}
98106
99107get_using <- function (syntax_tree ) {
100-
101108 get_using_recurse <- function (syntax_tree_expr ) {
102109 if (is.call(syntax_tree_expr )) {
103- if (is_using_node(syntax_tree_expr ))
110+ if (is_using_node(syntax_tree_expr )) {
104111 return (extract_using_data(syntax_tree_expr ))
105- else
112+ } else {
106113 make_data_frame(lapply(syntax_tree_expr , get_using_recurse ))
114+ }
107115 } else {
108116 return (NULL )
109117 }
110118 }
111119
112120 make_data_frame(lapply(syntax_tree , get_using_recurse ))
113-
114121}
115122
116- make_data_frame <- function (x )
117- {
118- result_dfs <- x [! unlist(lapply(x ,is.null ))]
119-
120- if (length(result_dfs ) > 0 ) do.call(rbind , c(result_dfs , stringsAsFactors = FALSE ))
121- else data.frame (package = character (0 ),
122- min_version = character (0 ),
123- repo = character (0 ), stringsAsFactors = FALSE )
123+ make_data_frame <- function (x ) {
124+ result_dfs <- x [! unlist(lapply(x , is.null ))]
125+
126+ if (length(result_dfs ) > 0 ) {
127+ do.call(rbind , c(result_dfs , stringsAsFactors = FALSE ))
128+ } else {
129+ data.frame (
130+ package = character (0 ),
131+ min_version = character (0 ),
132+ repo = character (0 ), stringsAsFactors = FALSE
133+ )
134+ }
124135}
0 commit comments