MangoTheCat / cyclocomp

Cyclomatic complexity of R functions and expressions
Other
47 stars 7 forks source link

How can i pass the variable in quote i.e cyclocomp(quote(variable)) or cyclocomp_q(variable) #23

Open pawan-panwar opened 1 year ago

pawan-panwar commented 1 year ago

variable <- for (var in seq) expr cyclocomp_q(variable) / cyclocomp(quote(variable))

the above method taking variable as a string not its value.

How can i pass the file instead of string in cyclocomp method.

gaborcsardi commented 1 year ago

You mean this?

for1 <- quote(for (var in seq) expr)
cyclocomp::cyclocomp(for1)
#> [1] 3
for2 <- quote(for (var in seq) for (var2 in seq2) expr)
cyclocomp::cyclocomp(for2)
#> [1] 6

Created on 2022-12-12 with reprex v2.0.2

pawan-panwar commented 1 year ago

Yes but its not working in case of Rscript arguments i.e. Rscript script.R "for (var in seq) expr" Content in script.R -

library(cyclocomp) args <- commandArgs(trailingOnly = TRUE) cyclocomp(args) @gaborcsardi thanks in advance !!

gaborcsardi commented 1 year ago

You need to parse() the script into an R expression:

script <- "

library(somepackage)

call_some_function()

for (var in seq) {
  for (var2 in seq2) {
    do_something
  }
}
"

writeLines(script, script_file <- tempfile())

expr <- parse(script_file)
cyclocomp::cyclocomp(expr)
#> [1] 6

Created on 2022-12-13 with reprex v2.0.2