Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

object_usage_linter not working for S4 code? #24

Closed
fabian-s opened this issue Dec 2, 2014 · 3 comments
Closed

object_usage_linter not working for S4 code? #24

fabian-s opened this issue Dec 2, 2014 · 3 comments

Comments

@fabian-s
Copy link
Contributor

fabian-s commented Dec 2, 2014

Thanks for the great package, it's a nice help with grading programming assignments in addition to its general usefulness.

However, if I try to run the default linters on S4 code (here I just use some examples from the methods-package to make this reproducible) I get:

tmpfile <- tempfile(fileext = ".R")
writeLines(example(setClass, give.lines=TRUE), con=tmpfile)
lintr::lint(tmpfile)
## Error in typeof(fun) : object 'fun' not found
## Error in body(fun) : object 'fun' not found
writeLines(example(Methods, give.lines=TRUE), con=tmpfile)
lintr::lint(tmpfile)
## Error in typeof(fun) : object 'fun' not found
## Error in body(fun) : object 'fun' not found
traceback()
## 12: body(fun)
## 11: findLocalsList(c(list(body), dropMissings(formals)))
## 10: codetools::findFuncLocals(formals(fun), body(fun))
## 9: FUN(19L[[1L]], ...)
## 8: lapply(ids_with_token(source_file, rex(start, "FUNCTION"), fun = re_matches), 
##       function(loc) {
##           id <- source_file$parsed_content$id[loc]
##         [...]
## 7: linters[[linter]](expr)
## [...]
lintr::lint(tmpfile, 
     linters=default_linters[names(default_linters) != "object_usage_linter"])
## works as expected

sessionInfo():

R version 3.1.2 (2014-10-31)
Platform: x86_64-pc-linux-gnu (64-bit)

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8       
 [4] LC_COLLATE=en_US.UTF-8     LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                  LC_ADDRESS=C              
[10] LC_TELEPHONE=C             LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] lintr_0.2.0

loaded via a namespace (and not attached):
 [1] codetools_0.2-8  crayon_1.1.0     digest_0.6.4     htmltools_0.2.6  igraph_0.7.1    
 [6] lazyeval_0.1.9   magrittr_1.5     packrat_0.4.1-1  parallel_3.1.2   rex_0.2.0       
[11] rmarkdown_0.3.3  stringdist_0.8.1 tools_3.1.2      yaml_2.1.13     
@jimhester
Copy link
Member

1ba3810 should close this.

There were actually two separate issues going on here.

  1. S4 classes need the methods package to be loaded and the default environment the expressions were evaulated in was baseenv(), which does not load methods.
  2. If there was a error in evaluating no expression would be assigned to fun, so it would break when later body(fun) was run.

So the fix was to use globalenv() rather than baseenv() and to only continue the object usage checks if the evaluation did not error.

@jimhester
Copy link
Member

Also using this package for homework grading is something I had never thought of, great idea!

@fabian-s
Copy link
Contributor Author

fabian-s commented Dec 2, 2014

Awesome, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants