halpo / lint Goto Github PK
View Code? Open in Web Editor NEWLint R Style Checking
Lint R Style Checking
Hi,
When I run multiple tests on a file, a lot of time is wasted running the same things for each test. find_region is the main culprit.
I'd like to suggest that the lint package make use of the memoise package. For example, add memoize(find_region). Doing this made my process run four times faster but I had to insert memoize in an unconventional and hacky way (via unlockBinding, assignInNamespace, etc.).
Thanks for considering it.
I am attempting to write a wrapper script to call lint on an R code file and am running into and error when I call things from the command line using Rscript, while things work in the R console. See the example script in this gist:
https://gist.github.com/cameronbracken/6176642
on the command line I get:
$ ./Rlint.R lint_test.R
Lint checking: lint_test.R
Error in valid_find(result) : could not find function "is"
Calls: lint ... FUN -> do.call -> <Anonymous> -> stopifnot -> valid_find
Execution halted
while in the R interpreter I get:
> lint('lint_test.R')
Lint checking: lint_test.R
Lint: Equal sign assignments: found on lines 1
Lint: Assignments are not allowed to be nested in function calls.: found on lines 1
P.S. that second lint comment in the R interpreter may be a separate issue
In R, it is recommended to break long lines after a binary operator, so that in script mode the interpreter knows that the statement is not complete and that the rest of it is on the next line. For example,
x <- 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1
should be broke as follows:
x <- 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 +
1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1
If the linebreak is put right after the +
operator on the first of the two lines above, spacing.spacearoundinfix
will complain. I wish spacing.spacearoundinfix
treated linebreaks like any other white space when checking after a binary operator.
Thanks.
I suppose the text
argument to the lint
function is for testing things like:
lint(text = "a=1+1")
I get
Error in inherits(.data, "split") : object 'files' not found
Quick look at the code for lint
and it seems that the file
variable defined as file <- textConnection(text)
should be named files
.
pointing to this repo
code to reproduce the issue; the first raises a flag, the second passes. The only difference between the two texts is the order of the arguments.
mytest <- list(pattern = "^( )*( )\\S",
message = "indentation should be by 3 spaces.",
exclude.region = c("find_function_args", "find_call_args")
)
mytext1 <- "
foo <- function(x,
y = attr(x, 'bar'),
z){
return(x)
}
"
mytext2 <- "
foo <- function(x,
z,
y = attr(x, 'bar')){
return(x)
}
"
writeLines(text = mytext1, "file1.R")
writeLines(text = mytext2, "file2.R")
# flagged
check1 <- lint(file = "file1.R",
style = list(mytest),
recurse = FALSE
)
# passes
check2 <- lint(file = "file2.R",
style = list(mytest),
recurse = FALSE
)
spacing.spacearoundinfix
warns about these two examples:
a <- 1e-3
b <- 2E+4
I'm seeing
checking R code for possible problems ... NOTE
autotest_style: no visible global function definition for ‘test_that’
test_style: no visible global function definition for
‘expect_equivalent’
Add support for functional form styles.
Certain styles need specific exclusions for specific functions. These will require a generic function finder generator.
Add Environment checking for supporting naming conventions. Functions can be checked at runtime. There may need to be a way to examine within functions for naming conventions of inside of functions. At the global level everything that is not a function could be considered a constant.
This may be related to halpo's issue #5.
system.time
, within
, local
are some of R's function where using assignments inside the arguments are perfectly ok, e.g:
system.time(a <- sleep(1))
system.time(a = sleep(1))
within(cars, speed.dist <- speed * dist)
within(cars, speed.dist = speed * dist)
Would it be possible to modify styles.assignment.notinfcall
to handle such exceptions? If not, I would consider removing styles.assignment.notinfcall
from lint
's default list of tests: lint.style
.
I can't put a finger on them, but there seems to be minimum file content requirements (other than syntactically valid R code) for the function lint
to work fine.
For example, lint
dies if provided an empty file, or a file with the following contents:
a <- "HELLO"
mysum <- sum
z <- 1:12
Here is the error message I am seeing:
lint(file = "/tmp/Rtmpc5W4Xl/file4c7f408cd504")
Lint checking: /tmp/Rtmpc5W4Xl/file4c7f408cd504
Error in[.data.frame
(ldply(parse.data, parse2find), names(empty.find)) :
undefined columns selected
15: lint(file = "/tmp/Rtmpc5W4Xl/file4c7f408cd504")
14: llply(files, lint_file, style = style)
13: structure(lapply(pieces, .fun, ...), dim = dim(pieces))
12: lapply(pieces, .fun, ...)
11: FUN(X[[1L]], ...)
10: llply(style, dispatch_test, file = file, parse.data = parse.data,
lines = lines)
9: structure(lapply(pieces, .fun, ...), dim = dim(pieces))
8: lapply(pieces, .fun, ...)
7: FUN(X[[3L]], ...)
6: find_region(exclude.region, file = file, lines = lines, parse.data = parse.data)
5: fun.region[[i]](file = file, lines = lines, parse.data = parse.data)
4: parse2find(call.args)
3: ldply(parse.data, parse2find)[names(empty.find)]
2:[.data.frame
(ldply(parse.data, parse2find), names(empty.find))
1: stop("undefined columns selected")
https://cran.r-project.org/web/packages/lint/index.html says:
Package ‘lint’ was removed from the CRAN repository.
Formerly available versions can be obtained from the archive.
Archived on 2016-08-23 as requested by the maintainer [email protected].
If this package is no longer maintained, is there a recommended replacement?
Thanks
spacing.spacearoundequals
is ok with this:
print(paste("x =", x))
... but not with this:
print(paste("x =",
x))
I feel both styles should be accepted.
As reported by Adam Wolenc:
I find that lint bails out on the following 2-line input:
which(a[,'b'])
print('a =')
I get the following stack trace:
> lint('bubbles/pathological.R')
Error in apply(overlaps, 1, any) : dim(X) must have a positive length
Enter a frame number, or 0 to exit
1: lint("bubbles/pathological.R")
2: llply(lint.tests, dispatch_test, file = file, parse.data = parse.data, line
3: structure(lapply(pieces, .fun, ...), dim = dim(pieces))
4: lapply(pieces, .fun, ...)
5: FUN(X[[2]], ...)
6: span_difference(test.result, exclude.spans)
7: apply(overlaps, 1, any)
This will require a new finder function for finding unary operators. There was a possible fix in the development of parser that was an identifier for unary operators. The patches for parser need to be more thoroughly tested for integration as the class attribute in the data has changed.
Using this example:
r1 <- x %*% y
r2 <- x %/% y
r3 <- x %o% y
r4 <- x %in% y
r5 <- x %myownoperator% y
r6 <- x %% y
spacing.spacearoundinfix
warns about the first two (a problem), but not all the others (as expected).
To fill the promise of creating a full documentation solution there needs to be a function that creates the Rd files from all the objects in a package.
This will have to
Then run the format_Rd function for each object to create the resulting files.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.