@@ -211,86 +211,86 @@ extract_occurrence <- function(pd) {
211211 }
212212 }
213213
214- # Handle data(object)/data("object")/data(object, envir = ) independently.
215- data_call <- find_call(pd , " data" )
216- if (data_call ) {
217- sym <- pd [data_call + 1 , " text" ]
218- return (c(gsub(" ^['\" ]|['\" ]$" , " " , sym ), " <-" ))
219- }
220- # Handle assign(x = ).
221- assign_call <- find_call(pd , " assign" )
222- if (assign_call ) {
223- # Check if parameters were named.
224- # "','" is for unnamed parameters, where "SYMBOL_SUB" is for named.
225- # "EQ_SUB" is for `=` appearing after the name of the named parameter.
226- if (any(pd $ token == " SYMBOL_SUB" )) {
227- params <- pd [pd $ token %in% c(" SYMBOL_SUB" , " ','" , " EQ_SUB" ), " text" ]
228- # Remove sequence of "=", ",".
229- if (length(params > 1 )) {
230- remove <- integer(0 )
231- for (i in 2 : length(params )) {
232- if (params [i - 1 ] == " =" & params [i ] == " ," ) {
233- remove <- c(remove , i - 1 , i )
234- }
235- }
236- if (length(remove )) params <- params [- remove ]
237- }
238- pos <- match(" x" , setdiff(params , " ," ), nomatch = match(" ," , params , nomatch = 0 ))
239- if (! pos ) {
240- return (character (0L ))
214+ # Handle data(object)/data("object")/data(object, envir = ) independently.
215+ data_call <- find_call(pd , " data" )
216+ if (data_call ) {
217+ sym <- pd [data_call + 1 , " text" ]
218+ return (c(gsub(" ^['\" ]|['\" ]$" , " " , sym ), " <-" ))
219+ }
220+ # Handle assign(x = ).
221+ assign_call <- find_call(pd , " assign" )
222+ if (assign_call ) {
223+ # Check if parameters were named.
224+ # "','" is for unnamed parameters, where "SYMBOL_SUB" is for named.
225+ # "EQ_SUB" is for `=` appearing after the name of the named parameter.
226+ if (any(pd $ token == " SYMBOL_SUB" )) {
227+ params <- pd [pd $ token %in% c(" SYMBOL_SUB" , " ','" , " EQ_SUB" ), " text" ]
228+ # Remove sequence of "=", ",".
229+ if (length(params > 1 )) {
230+ remove <- integer(0 )
231+ for (i in 2 : length(params )) {
232+ if (params [i - 1 ] == " =" & params [i ] == " ," ) {
233+ remove <- c(remove , i - 1 , i )
241234 }
242- # pos is indicator of the place of 'x'
243- # 1. All parameters are named, but none is 'x' - return(character(0L))
244- # 2. Some parameters are named, 'x' is in named parameters: match("x", setdiff(params, ","))
245- # - check "x" in params being just a vector of named parameters.
246- # 3. Some parameters are named, 'x' is not in named parameters
247- # - check first appearance of "," (unnamed parameter) in vector parameters.
248- } else {
249- # Object is the first entry after 'assign'.
250- pos <- 1
251235 }
252- sym <- pd [assign_call + pos , " text" ]
253- return (c(gsub(" ^['\" ]|['\" ]$" , " " , sym ), " <-" ))
236+ if (length(remove )) params <- params [- remove ]
254237 }
255-
256- # What occurs in a function body is not tracked.
257- x <- pd [! is_in_function(pd ), ]
258- sym_cond <- which(x $ token %in% c(" SPECIAL" , " SYMBOL" , " SYMBOL_FUNCTION_CALL" ))
259-
260- if (length(sym_cond ) == 0 ) {
238+ pos <- match(" x" , setdiff(params , " ," ), nomatch = match(" ," , params , nomatch = 0 ))
239+ if (! pos ) {
261240 return (character (0L ))
262241 }
263- # Watch out for SYMBOLS after $ and @. For x$a x@a: x is object, a is not.
264- # For x$a, a's ID is $'s ID-2 so we need to remove all IDs that have ID = $ID - 2.
265- dollar_ids <- x [x $ token %in% c(" '$'" , " '@'" ), " id" ]
266- if (length(dollar_ids )) {
267- object_ids <- x [sym_cond , " id" ]
268- after_dollar <- object_ids [(object_ids - 2 ) %in% dollar_ids ]
269- sym_cond <- setdiff(sym_cond , which(x $ id %in% after_dollar ))
270- }
242+ # pos is indicator of the place of 'x'
243+ # 1. All parameters are named, but none is 'x' - return(character(0L))
244+ # 2. Some parameters are named, 'x' is in named parameters: match("x", setdiff(params, ","))
245+ # - check "x" in params being just a vector of named parameters.
246+ # 3. Some parameters are named, 'x' is not in named parameters
247+ # - check first appearance of "," (unnamed parameter) in vector parameters.
248+ } else {
249+ # Object is the first entry after 'assign'.
250+ pos <- 1
251+ }
252+ sym <- pd [assign_call + pos , " text" ]
253+ return (c(gsub(" ^['\" ]|['\" ]$" , " " , sym ), " <-" ))
254+ }
271255
272- ass_cond <- grep(" ASSIGN" , x $ token )
273- if (! length(ass_cond )) {
274- return (c(" <-" , unique(x [sym_cond , " text" ])))
275- }
256+ # What occurs in a function body is not tracked.
257+ x <- pd [! is_in_function(pd ), ]
258+ sym_cond <- which(x $ token %in% c(" SPECIAL" , " SYMBOL" , " SYMBOL_FUNCTION_CALL" ))
276259
277- sym_cond <- sym_cond [sym_cond > ass_cond ] # NOTE 1
278- # If there was an assignment operation detect direction of it.
279- if (unique(x $ text [ass_cond ]) == " ->" ) { # NOTE 2
280- sym_cond <- rev(sym_cond )
281- }
260+ if (length(sym_cond ) == 0 ) {
261+ return (character (0L ))
262+ }
263+ # Watch out for SYMBOLS after $ and @. For x$a x@a: x is object, a is not.
264+ # For x$a, a's ID is $'s ID-2 so we need to remove all IDs that have ID = $ID - 2.
265+ dollar_ids <- x [x $ token %in% c(" '$'" , " '@'" ), " id" ]
266+ if (length(dollar_ids )) {
267+ object_ids <- x [sym_cond , " id" ]
268+ after_dollar <- object_ids [(object_ids - 2 ) %in% dollar_ids ]
269+ sym_cond <- setdiff(sym_cond , which(x $ id %in% after_dollar ))
270+ }
282271
283- after <- match(min(x $ id [ass_cond ]), sort(x $ id [c(min(ass_cond ), sym_cond )])) - 1
284- ans <- append(x [sym_cond , " text" ], " <-" , after = max(1 , after ))
285- roll <- in_parenthesis(pd )
286- if (length(roll )) {
287- c(setdiff(ans , roll ), roll )
288- } else {
289- ans
290- }
272+ ass_cond <- grep(" ASSIGN" , x $ token )
273+ if (! length(ass_cond )) {
274+ return (c(" <-" , unique(x [sym_cond , " text" ])))
275+ }
276+
277+ sym_cond <- sym_cond [sym_cond > ass_cond ] # NOTE 1
278+ # If there was an assignment operation detect direction of it.
279+ if (unique(x $ text [ass_cond ]) == " ->" ) { # NOTE 2
280+ sym_cond <- rev(sym_cond )
281+ }
291282
292- # ## NOTE 2: What if there are 2 assignments: e.g. a <- b -> c.
293- # ## NOTE 1: For cases like 'eval(expression(b <- b + 2))' removes 'eval(expression('.
283+ after <- match(min(x $ id [ass_cond ]), sort(x $ id [c(min(ass_cond ), sym_cond )])) - 1
284+ ans <- append(x [sym_cond , " text" ], " <-" , after = max(1 , after ))
285+ roll <- in_parenthesis(pd )
286+ if (length(roll )) {
287+ c(setdiff(ans , roll ), roll )
288+ } else {
289+ ans
290+ }
291+
292+ # ## NOTE 2: What if there are 2 assignments: e.g. a <- b -> c.
293+ # ## NOTE 1: For cases like 'eval(expression(b <- b + 2))' removes 'eval(expression('.
294294}
295295
296296# ' Extract side effects
@@ -389,7 +389,7 @@ detect_libraries <- function(graph) {
389389 which(
390390 unlist(
391391 lapply(
392- graph , function (x ){
392+ graph , function (x ) {
393393 any(grepl(pattern = paste(defaults , collapse = " |" ), x = x ))
394394 }
395395 )
@@ -478,4 +478,3 @@ split_code <- function(code) {
478478 # semicolon is treated by R parser as a separate call.
479479 gsub(" ^([[:space:]])*;(.+)$" , " \\ 1\\ 2" , new_code , perl = TRUE )
480480}
481-
0 commit comments