commas_linter does not work in lintr - r

I am using lintr to ensure my code meets good R style guidelines, but for some reason, the commas_linter does not catch it when I do not put a space after a comma if there is a blank line preceding the erroneous line. Am I doing something wrong, or is this a bug in the package?
Here's a MWE
file: Test.r
#random comment
a <- c(4,5)
b = 7
Then, I run the following:
lintr::lint("./Test.r", linters = list(spacesAfterCommas = commas_linter,
useArrowForAssignments = assignment_linter))
The only error I get is the following:
./Test.r:4:3: style: Use <-, not =, for assignment.
b = 7
^
Why is the commas_linter not catching my error?

Related

Using a $ symbol within RStudio snippets without the $ sign being evaluated as a variable creator

When I create an R snippet in RStudio the following case, the $ sign from the snippet variable creates problems when I want a literal $sign to appear for use in my snippet (e.g., mtcars$cyl):
snippet cor_binary
ltm::biserial.cor(${1:df}\$ ${2:continuous_field}, ${1:df}\$ ${3:binary_field}, use = c("complete.obs"), level = 2)
The above snippet produces an unwanted white space between df & field:
# output
ltm::biserial.cor(df$ continuous_field, df$ binary_field, use = c("complete.obs"), level = 2)
No space in my snippet produces a different problem in the output:
snippet cor_binary
ltm::biserial.cor(${1:df}\$${2:continuous_field}, ${1:df}\$${3:binary_field}, use = c("complete.obs"), level = 2)
# output
ltm::biserial.cor(df\{2:continuous_field}, df\{3:binary_field}, use = c("complete.obs"), level = 2)
How can I include a $ sign in the context of mtcars$cyl without the snippet
The recommended approach would be to use [[ instead of $:
snippet test
${1:df}[["${2:abc}"]]
Which resolves to:
df[["abc"]]
If $ must be used here's a hacky way to get it working that takes advantage of the fact that snippets can run code using `r ... ` which can be used to print a zero width element between the escaped $ and the second variable. This probably shouldn't be relied upon though.
snippet test2
${1:df}\$`r ""`${2:abc}
Which resolves to:
df$abc
Use backticks:
data.table::data.table(`a$b` = 1)
a$b
1: 1

R: creating a function which prints the line number at which it is executed

I'm having issues debugging some code in R ... And so I thought it might be good practice for me to put in print statements in the code to see which function is running before an error occurs. And so I thought an even nicer feature would be to include in the print statement the line that is currently being executed. Here is an example of function that I am thinking of:
custom_print <- function(output_str){
line_number <- line_number_function()
print(paste0(line_number, " - ", output_str))
}
Then I could just use custom_print("Function A running") in my
code. How can I achieve this? I am looking for a
line_number_function() which returns the line number (when the
code is interpreted !).
EDIT (more details):
Here is a specific example of how I would use this
# test.R
# At the beginning of the R script I have this:
custom_print <- function(output_str){
line_number <- line_number_function()
print(paste0(line_number, " - ", output_str))
}
# ... some code here ...
# example function
# NOTE: assume this is line 42 in my code
add_stuff <- function(x, y){
custom_print("Hey I'm adding !")
return(x+y)
}
# use my adding function
add_stuff(1, 1)
To be super specific, I would run this from the console in Rstudio server using:
system('Rscript test.R')
which I believe is the same as running it from the Unix/Linux command line. Then I would expect some output like this:
[1] 44 - Hey I'm adding !
The 44 here is the line in the function of the interpreted code.

Line continuation operator not working python

I am new to Python and I am having issues when breaking a line of code. I am using Python 3.6 on Spyder.
When I try to break any line and I run the code, Python reads only until before the backslash and retrieves and error. For example, I want to break
a = 1 + 2 + 3
into
a = (a + b \
+ 3)
It gives me the error SyntaxError: unexpected EOF while parsing.
I have checked with and without the backslash, and with different indentations in the second line.
I have read the way of breaking a line of code is this one...is there anything I have to activate on my IDE or something like that?
If you use parenthesis the line break is implicit, so you could break your line after the plus sign like so:
a = (a + b +
3)
Or like so:
a = (a + b
+ 3)
By the guidelines the backslash needs only to be used if necessary, otherwise please use the implicit one.

Deprecation of isblank("string") in Julia 0.4

I'm doing text parsing in Julia and need to test if certain strings are blank (in order to parse into floats). I have been using isblank() booleans in <0.4 Julia, but after upgrading to 0.4, I get the following warning:
julia> isblank(q)
WARNING: isblank(s::AbstractString) is deprecated, use all((c->begin
c == ' ' || c == '\t'
end),s) instead.
in depwarn at deprecated.jl:73
in isblank at deprecated.jl:50
while loading no file, in expression starting on line 0
true
What replaced isblank()? Is the above really the replacement? My googling didn't turn up anything useful.
Yes, that is the replacement.
Basically in #5939 and related issues, it was revealed that answering isblank is a lot harder than it might first appear given Unicode complexities.
The deprecation occurred in #8233.
Everything looks natural, it must be a replacement in new version, you can add the line bellow in your code and replace (using Ctrl+H) all isblank( with isblk(. I wish it works for you.
isblk(s)=all((c->begin c == ' ' || c == '\t' end),s)
now isblk([]) # => true

Retrieving Variable Declaration

How can I find how did I first declare a certain variable when I am a few hundred
lines down from where I first declared it. For example, I have declared the following:
a <- c(vectorA,vectorB,vectorC)
and now I want to see how I declared it. How can I do that?
Thanks.
You could try using the history command:
history(pattern = "a <-")
to try to find lines in your history where you assigned something to the variable a. I think this matches exactly, though, so you may have to watch out for spaces.
Indeed, if you type history at the command line, it doesn't appear to be doing anything fancier than saving the current history in a tempfile, loading it back in using readLines and then searching it using grep. It ought to be fairly simple to modify that function to include more functionality...for example, this modification will cause it to return the matching lines so you can store it in a variable:
myHistory <- function (max.show = 25, reverse = FALSE, pattern, ...)
{
file1 <- tempfile("Rrawhist")
savehistory(file1)
rawhist <- readLines(file1)
unlink(file1)
if (!missing(pattern))
rawhist <- unique(grep(pattern, rawhist, value = TRUE,
...))
nlines <- length(rawhist)
if (nlines) {
inds <- max(1, nlines - max.show):nlines
if (reverse)
inds <- rev(inds)
}
else inds <- integer()
#file2 <- tempfile("hist")
#writeLines(rawhist[inds], file2)
#file.show(file2, title = "R History", delete.file = TRUE)
rawhist[inds]
}
I will assume you're using the default R console. If you're on Windows, you can File -> Save history and open the file in your fav text browser, or you can use function savehistory() (see help(savehistory)).
What you need to do is get a (good) IDE, or at least a decent text editor. You will benevit from code folding, syntax coloring and much more. There's a plethora of choices, from Tinn-R, VIM, ESS, Eclipse+StatET, RStudio or RevolutionR among others.
You can run grep 'a<-' .Rhistory from terminal (assuming that you've cdd to your working directory). ESS has several very useful history-searching functions, like (comint-history-isearch-backward-regexp) - binded to M-r by default.
For further info, consult ESS manual: http://ess.r-project.org/Manual/ess.html
When you define a function, R stores the source code of the function (preserving formatting and comments) in an attribute named "source". When you type the name of the function, you will get this content printed.
But it doesn't do this with variables. You can deparse a variable, which generates an expression that will produce the variable's value but this doesn't need to be the original expression. For example when you have b <- c(17, 5, 21), deparse(b) will produce the string "c(17, 5, 21)".
In your example, however, the result wouldn't be "c(vectorA,vectorB,vectorC)", it would be an expression that produces the combined result of your three vectors.

Resources