R Error - cannot change value of locked binding for 'df'

前端 未结 4 1243
日久生厌
日久生厌 2021-02-14 06:00

I\'m trying to filter some data, with functions in R relatives to data frames. But in the second function, it gives me the following error: cannot change the value of locked bin

4条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-14 06:53

    I had this problem when creating function inside a package, and I was using <<- inside exported function to overwrite some functions from different package. I've created hack with env <- parent.env() and adding this to parent env instead since the function was always called at top level. So env will be scope of the code that call the function not the package.

    here is complete code of this function, it overwrite shiny and current package functions:

    #' @export
    useMocks <- function() {
      observeEvent <- battery::observeEventMock
      assignInNamespace("observeEvent", observeEvent, "battery")
      assignInNamespace("observeEvent", observeEvent, "shiny")
      isolate <- battery::isolate
      assignInNamespace("isolate", isolate, "shiny")
      makeReactiveBinding <- battery::makeReactiveBinding
      assignInNamespace("makeReactiveBinding", makeReactiveBinding, "shiny")
      renderUI <- battery::renderUI
      assignInNamespace("renderUI", renderUI, "shiny")
      ## we modify the parent frame so it update environment when function is called not the package
      env <- parent.frame()
      env$observeEvent <- observeEvent
      env$isolate <- isolate
      env$renderUI <- renderUI
    
    }
    

    using <<- throw this error here because you can't modify the package scope.

    observeEvent <<- battery::observeEventMock
    

    this don't work

提交回复
热议问题