Giter VIP home page Giter VIP logo

Comments (8)

AshesITR avatar AshesITR commented on September 3, 2024 1

Sorry for being unclear.
You should of course also define the global variable before writing to it.

from lintr.

AshesITR avatar AshesITR commented on September 3, 2024 1

@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.

MichaelChirico avatar MichaelChirico commented on September 3, 2024

is this part of a script or a package?

from lintr.

krishnakeshav avatar krishnakeshav commented on September 3, 2024

@MichaelChirico

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.

AshesITR avatar AshesITR commented on September 3, 2024

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.

krishnakeshav avatar krishnakeshav commented on September 3, 2024

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 -
image
It seems to me that <<- is not recognized as an operator to lint.

from lintr.

krishnakeshav avatar krishnakeshav commented on September 3, 2024

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 avatar krishnakeshav commented on September 3, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.