cannot add bindings to a locked environment

4273 views
Skip to first unread message

Timothy Bates

unread,
Sep 9, 2014, 10:27:54 AM9/9/14
to rdev...@googlegroups.com
another questions for the advanced R FAQ

What causes a package to lock the environment?

(context: I am getting an error (pasted below) on devtools::install() if-and-only-if I have also loaded another package I wrote which depends on this one. 


unloadNamespace("umx") not successful. Forcing unload.
Error in importIntoEnv(pkgenv, exports, nsenv, exports) : 
  cannot add bindings to a locked environment

Winston Chang

unread,
Sep 9, 2014, 12:03:30 PM9/9/14
to Timothy Bates, rdev...@googlegroups.com
When a package is loaded the normal way (not with devtools), R locks the package environment and the namespace environment, so you can't modify them.

-Winston


--
You received this message because you are subscribed to the Google Groups "devtools" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rdevtools+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Timothy Bates

unread,
Sep 11, 2014, 2:52:50 PM9/11/14
to rdev...@googlegroups.com, timothy...@gmail.com
thanks winston! didn't occur to me that loading my  package2 via devtools would re-load and lock package 1 (which it requires)... cheers, t

Donald Jackson

unread,
Apr 2, 2015, 9:49:16 AM4/2/15
to rdev...@googlegroups.com, timothy...@gmail.com
How does one fix this?  I'm trying to build and reload a package in RStudio and getting the same error.  I'm new to package building and can't figure out how to unlock the package I want to update.
I get the same error as in the original post if I try using RStudio's 'build and reload' command or use devtools load_all() function from the console.

I'm using RStudio 0.98.1103 with R 3.1.3 and Rtools 3.1.

Thanks!

Luca Cerone

unread,
Apr 11, 2015, 10:44:35 AM4/11/15
to rdev...@googlegroups.com, timothy...@gmail.com
There are two ways you can modify your package variables:

1) if you have operations to be done before  R seals the namespace define them in the .onLoad or .onAttach hooks
(see the help for them for a detailed explanation)

2) create an environment, environments are not sealed, so you can still add, remove  and modifies the values from them.
The cleanest way is to have them inside a function and define the setter and getters there (for a nice and simple example,
you can have a look at Hadley Wickham's memoise package, and how he creates the cache for its function)

Hope it helps,
Luca

RantingTalk

unread,
Mar 5, 2016, 7:03:11 PM3/5/16
to devtools, timothy...@gmail.com
Hey! I am having this same issue when creating my own R package.

here is more information on my issue.

I tried looking up your solution Luca, But really didn't see any direct applications to my problem.

(http://stackoverflow.com/questions/35819315/assign-error-when-building-custom-package-in-r-cant-add-bindings-to-locked-env)

Stef van Buuren

unread,
Jan 17, 2017, 4:38:17 AM1/17/17
to devtools
This error occurred when I accidentally placed an R-script into my /R directory. The script contained a library(mypackage) statement, and then it seemed it was in a deadlock. My solution was to move it to a directory /scripts.

Isabella Yu Zhou

unread,
Aug 13, 2017, 12:03:04 PM8/13/17
to devtools
Hi, thank you so much! My problem is solved by removing hanging Rscripts to other folders.

在 2017年1月17日星期二 UTC-5上午4:38:17,Stef van Buuren写道:

Rosa Bedoya

unread,
Feb 3, 2018, 11:47:00 PM2/3/18
to devtools
Hello , my name is Rosa, I enrolled in an edx harvard course, and I do not know how to install the
library (devtools)
install_github ("genomicsclass / tissuesGeneExpression")
I get error error please could you help me

Victor Ordu

unread,
Mar 17, 2018, 10:23:52 AM3/17/18
to devtools
What error do you get?

Robin Gape

unread,
Mar 18, 2018, 11:53:51 AM3/18/18
to rdev...@googlegroups.com

Rosa,

if you've not sorted it yet, searching on GitHub for genomicsclass gives 13 hits, and either of the last two might be what you want. The search box is well hidden in the black bar at the top of the GitHub welcome screen. The search is https://github.com/search?p=2&q=genomicsclass+&type=Repositories&utf8=%E2%9C%93.

HTH,

R.

Reply all
Reply to author
Forward
0 new messages