Comments (8)
Sorry for being unclear.
You should of course also define the global variable before writing to it.
from lintr.
@krishnakeshav I recommend you read the R Packages section on internal state for packages to see how you should define and use global state in Packages.
Here is a MWE copied from there:
the <- new.env(parent = emptyenv())
the$favorite_letters <- letters[1:3]
#' Report my favorite letters
#' @export
mfl2 <- function() {
the$favorite_letters
}
#' Change my favorite letters
#' @export
set_mfl2 <- function(l = letters[24:26]) {
old <- the$favorite_letters
the$favorite_letters <- l
invisible(old)
}
For plain scripts, the error "cannot change value of locked binding ..." will disappear.
from lintr.
is this part of a script or a package?
from lintr.
is this part of a script or a package?
Issue originally occurred in a package, but the above example is a script for the sake of reproducing.
The easiest way around this is to add return(g_var)
.
from lintr.
Your code doesn't do what you think it does, and the warning is justified.
If you want to overwrite g_var
in the outer scope from within the function, you need to use <<-
.
from lintr.
Your code doesn't do what you think it does, and the warning is justified.
If you want to overwrite
g_var
in the outer scope from within the function, you need to use<<-
.
using <<-
in conjunction to glovalVariables
also gives warning, see below -
It seems to me that <<-
is not recognized as an operator to lint.
from lintr.
Sorry for being unclear. You should of course also define the global variable before writing to it.
Thanks @AshesITR , it works well with scripts. In package, when I run lint active package from R studio, I don't get any notes. However, upon running R CMD check
, I get the note as below -
no visible binding for '<<-' assignment to ‘result_index_list’
Note that, I have different code in case of R script in package.
result_index_list <<- NULL
some_fun <- function() {
result_index_list <<- some_list_assignment
}
In above, if I use result_index_list <- NULL
, then I get following runtime error -
Error in FUN(X[[i]], ...) :
cannot change value of locked binding for 'result_index_list'
I have R/globals.R
with utils::globalVariables(c("result_index_list"))
Does lint behave differently in R CMD check
than the scripts ?
from lintr.
@krishnakeshav I recommend you read the R Packages section on internal state for packages to see how you should define and use global state in Packages.
Here is a MWE copied from there:
the <- new.env(parent = emptyenv()) the$favorite_letters <- letters[1:3] #' Report my favorite letters #' @export mfl2 <- function() { the$favorite_letters } #' Change my favorite letters #' @export set_mfl2 <- function(l = letters[24:26]) { old <- the$favorite_letters the$favorite_letters <- l invisible(old) }For plain scripts, the error "cannot change value of locked binding ..." will disappear.
This worked for me. Thanks again @AshesITR
from lintr.
Related Issues (20)
- Unrecognized R6 class HOT 4
- object_name_linter warning with R6 class HOT 1
- Lint `return()` usage with pipe in `return_linter()`? HOT 4
- indentation_linter reports "startColumn": 0 -> invalid SARIF file HOT 3
- Are roxy checks across different locales working in GHA?
- Make `cyclocomp_linter()` optional HOT 3
- Return an empty list with `<lints>` class in addin HOT 2
- Should we provide a linter to statically detect unsupported currying of base functions? HOT 2
- Cover explicit return for `invisible()` in `return_linter()` HOT 2
- Linter for partially matched arguments HOT 2
- lintr does not like for loop bodies with a single line and no braces HOT 1
- duplicate_argument_linter should not lint `abort()` calls HOT 1
- literal_coercion_linter surfaces coercion warnings
- False positive for inner_combine_linter() on timestamps with different formats HOT 1
- Bump minimum R version to >=4.0 HOT 6
- lint_dir fails to work in this repo using .lintr file HOT 3
- `paste_linter()` doesn't work as expected with pipes HOT 1
- pattern= should be a configurable setting in lint_dir()
- `min_r_version_linter()` to detect invalid or missing minimum R version in R package HOT 5
- Lint `seq_len(length(x))` HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from lintr.